#!/bin/bash # # unstage the selected staged file # or reset the commit to certain point # # @params # Globals # ${mydir}: current directory of the script # ${selected_files}: selected file to unstage # ${search_commits}: search commits and reset commits instead of files # #{reset_option}: git reset flag, --mixed | --soft | --hard set -e mydir="${0%/*}" source "${mydir}"/../helper/set_variable source "${mydir}"/../helper/get_confirmation function usage() { echo -e "Usage: dotbare freset [-h] [-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 "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" } search_commits="" reset_option="--mixed" while getopts ":hcSH" opt do case "$opt" in c) search_commits="true" ;; S) reset_option="--soft" ;; H) reset_option="--hard" ;; h) usage exit 0 ;; *) echo "Invalid option: ${OPTARG}" >&2 usage exit 1 ;; esac done if [[ -n "${search_commits}" ]]; then selected_commites=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" log --oneline --color=always --decorate=short | \ fzf --no-multi --header='select which commit to reset' --preview "echo {} | awk '{print \$1}' | \ xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} show --color=always __" | \ awk '{print $1}') [[ -z "${selected_commites}" ]] && exit 0 confirm=$(get_confirmation "Reset HEAD to ${selected_commites} ${reset_option}?") [[ "${confirm}" != 'y' ]] && exit 0 /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commites}" "${reset_option}" else selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --name-status --cached | \ awk '{print "\033[32m" $1 " " $2}' | fzf --header='select files to unstage' --multi --preview "echo {} | awk '{print \$2}' | \ xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} diff --staged --color=always ${DOTBARE_TREE}/__" | \ awk -v home="${DOTBARE_TREE}" '{print home "/" $2}') [[ -z "${selected_files}" ]] && exit 0 while IFS= read -r line; do /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset HEAD "${line}" 1>/dev/null echo "${line} unstaged successfully" done <<< "${selected_files}" fi