You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dotbare/scripts/fstat

74 lines
2.1 KiB
Plaintext

4 years ago
#!/usr/bin/env bash
5 years ago
#
# interactive git status menu
# toggle stage and unstage
5 years ago
#
# @params
# Globals
4 years ago
# ${mydir}: current directory of where the script is running
# ${selected_files}: raw selected file (with current git status prepend)
# ${selected_filenames}: bash array of names for the selected_files
4 years ago
# ${stage_file}: determine if current operation should be staging file or unstage
# Arguments
# -h|--help: show help message and exit
5 years ago
set -e
set -f
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
5 years ago
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"
echo -e "Toggle file stage/unstage interactively\n"
5 years ago
echo -e "optional arguments:"
echo -e " -h, --help\t\tshow this help message and exit"
5 years ago
}
selected_filenames=()
selected_files=""
stage_file=""
while [[ "$#" -gt 0 ]]; do
case "$1" in
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: $1" >&2
usage
exit 1
;;
esac
done
while :; do
selected_files=$(get_modified_file "select files to stage/unstage" "all" "raw")
5 years ago
[[ -z "${selected_files}" ]] && break
4 years ago
# check if current operation should stage file or unstage file
# 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=$(echo "${selected_files}" | awk '{
if ($0 ~ /^[A-Za-z][A-Za-z].*$/) {
print "stage"
} else if ($0 ~ /^[ \t].*$/) {
print "stage"
}
}')
while IFS= read -r line; do
selected_filenames+=("${line}")
done < <(echo "${selected_files}" | awk -v home="${DOTBARE_TREE}" '{print home "/" $2}')
if [[ -z "${stage_file}" ]]; then
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset --quiet HEAD "${selected_filenames[@]}"
5 years ago
else
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${selected_filenames[@]}"
5 years ago
fi
5 years ago
done