#!/usr/bin/env bash # # interactive git status menu # toggle stage and unstage # # @params # Globals # ${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 # ${stage_file}: determine if current operation should be staging file or unstage # Arguments # -h|--help: show help message and exit set -e set -f mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" 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" echo -e "optional arguments:" echo -e " -h, --help\t\tshow this help message and exit" } 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") [[ -z "${selected_files}" ]] && break # 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[@]}" else /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" add "${selected_filenames[@]}" fi done