2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00
dotbare/scripts/fstat

85 lines
2.3 KiB
Plaintext
Raw Normal View History

2020-05-11 01:56:27 +00:00
#!/usr/bin/env bash
2020-04-21 05:25:02 +00:00
#
# interactive git status menu
2020-06-26 02:58:32 +00:00
# toggle stage and unstage
2020-04-21 05:25:02 +00:00
#
# @params
# Globals
2020-04-24 06:02:29 +00:00
# ${mydir}: current directory of where the script is running
2020-06-26 02:58:32 +00:00
# ${selected_files}: raw selected file (with current git status prepend)
# ${selected_filenames}: bash array of names for the selected_files
2020-04-24 06:02:29 +00:00
# ${stage_file}: determine if current operation should be staging file or unstage
2020-05-07 02:08:07 +00:00
# Arguments
2020-06-26 02:58:32 +00:00
# -h|--help: show help message and exit
2020-04-21 05:25:02 +00:00
set -e
set -f
2020-05-13 07:30:54 +00:00
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
2020-04-21 05:25:02 +00:00
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fstat [-h] ...\n"
echo -e "Display interactive git status menu"
2020-06-26 02:58:32 +00:00
echo -e "Toggle file stage/unstage interactively\n"
2020-04-21 05:25:02 +00:00
echo -e "optional arguments:"
2020-06-26 02:58:32 +00:00
echo -e " -h, --help\t\tshow this help message and exit"
2020-04-21 05:25:02 +00:00
}
2020-06-26 02:58:32 +00:00
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h|--help)
2020-05-07 02:08:07 +00:00
usage
exit 0
;;
*)
2020-06-26 02:58:32 +00:00
echo "Invalid option: $1" >&2
2020-05-07 02:08:07 +00:00
usage
exit 1
;;
esac
done
2020-05-17 11:20:57 +00:00
while :; do
# reset all variable and arrays for each loop
selected_files=()
selected_filenames=()
stage_file=""
while IFS= read -r line; do
selected_files+=("${line}")
done < <(get_modified_file "select files to stage/unstage" "all" "raw")
[[ "${#selected_files[@]}" -eq 0 ]] && break
2020-06-26 02:58:32 +00:00
2020-04-24 06:02:29 +00:00
# check if current operation should stage file or unstage file
2020-06-26 02:58:32 +00:00
# if any file start with M but has char immediately follow it, new changes are made, stage file
# if any file start with a space or tab, the file is not staged, stage file
# otherwise, we unstage
stage_file=$(printf '%s\n' "${selected_files[@]}" | awk '{
2020-04-21 06:42:00 +00:00
if ($0 ~ /^[A-Za-z][A-Za-z].*$/) {
print "stage"
} else if ($0 ~ /^[ \t].*$/) {
print "stage"
}
}')
2020-06-26 02:58:32 +00:00
while IFS= read -r line; do
selected_filenames+=("${line}")
done < <(
printf '%s\n' "${selected_files[@]}" \
| awk -v home="${DOTBARE_TREE}" '{
$1=""
gsub(/^[ \t]/, "", $0)
gsub(/"/, "", $0)
print home "/" $0
}'
)
2020-06-26 02:58:32 +00:00
2020-04-21 06:42:00 +00:00
if [[ -z "${stage_file}" ]]; then
2020-06-26 02:58:32 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset --quiet HEAD "${selected_filenames[@]}"
2020-04-21 06:07:23 +00:00
else
2020-06-26 02:58:32 +00:00
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${selected_filenames[@]}"
2020-04-21 06:07:23 +00:00
fi
2020-04-21 05:25:02 +00:00
done