#!/usr/bin/env bash # # unstage the selected staged file # or reset the commit to certain point # # @params # Globals # ${mydir}: current directory of the script # ${reset_type}: reset type, modified files, all files or commit # ${reset_option}: git reset flag, --mixed | --soft | --hard # ${selected_files}: selected file to unstage # ${selected_commit}: selected commit to reset # ${confirm}: confirmation status of the user # Arguments # -h: show help message and quit # -a: select files and select a commit to reset the fiel back to selected commit # -c: reset commit # -S: use --soft flag # -H: use --hard flag # -y: confirm action by default and skip confirmation set -e set -f mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" source "${mydir}"/../helper/set_variable.sh source "${mydir}"/../helper/get_confirmation.sh source "${mydir}"/../helper/git_query.sh function usage() { echo -e "Usage: dotbare freset [-h] [-a] [-c] [-S] [-H] ...\n" echo -e "Reset/Unstage the selected staged file" echo -e "Or reset the HEAD to certain commits by using -c flag\n" echo -e "Default: unstage the selected files from\n" echo -e "optional arguments:" echo -e " -h\t\tshow this help message and exit" echo -e " -c\t\treset commit to certain commit, default --mixed flag, reset HEAD to certain commit put all changes into modified states" echo -e " -S\t\treset commit using --soft flag, reset HEAD to certain commit without modify working tree" echo -e " -H\t\treset commit using --hard flag, reset HEAD to certain commit dicard all changes from the working tree" echo -e " -y\t\tconfirm action by default and skip confirmation" } reset_option="--mixed" reset_type="modified" selected_files=() while getopts ":hacSHy" opt do case "$opt" in c) reset_type="commit" ;; S) reset_option="--soft" ;; H) reset_option="--hard" ;; y) confirm='y' ;; h) usage exit 0 ;; *) echo "Invalid option: ${OPTARG}" >&2 usage exit 1 ;; esac done if [[ "${reset_type}" == "commit" ]]; then selected_commit=$(get_commit) [[ -z "${selected_commit}" ]] && exit 0 [[ -z "${confirm}" ]] && confirm=$(get_confirmation "Reset HEAD to ${selected_commit} ${reset_option}?") [[ "${confirm}" != 'y' ]] && exit 1 /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commit}" "${reset_option}" else while IFS= read -r line; do selected_files+=("${line}") done < <(get_modified_file 'select files to unstage' 'staged') [[ "${#selected_files}" -eq 0 ]] && exit 1 /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset HEAD "${selected_files[@]}" fi