2020-04-10 06:09:08 +00:00
|
|
|
#!/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
|
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
mydir="${0%/*}"
|
|
|
|
source "${mydir}"/../helper/set_variable
|
|
|
|
|
|
|
|
function usage() {
|
|
|
|
echo -e "Usage: dotbare freset [-h] [-f] [-d] ...\n"
|
|
|
|
echo -e "Reset/Unstage the selected staged file"
|
|
|
|
echo -e "Or reset the commit to certain point\n"
|
|
|
|
echo -e "optional arguments:"
|
|
|
|
echo -e " -h\t\tshow this help message and exit"
|
|
|
|
}
|
|
|
|
|
2020-04-10 06:41:12 +00:00
|
|
|
search_commits=""
|
2020-04-10 07:20:13 +00:00
|
|
|
reset_option="--mixed"
|
2020-04-10 06:41:12 +00:00
|
|
|
|
2020-04-10 07:20:13 +00:00
|
|
|
while getopts ":hcSH" opt
|
2020-04-10 06:09:08 +00:00
|
|
|
do
|
|
|
|
case "$opt" in
|
2020-04-10 06:41:12 +00:00
|
|
|
c)
|
|
|
|
search_commits="1"
|
|
|
|
;;
|
2020-04-10 07:20:13 +00:00
|
|
|
S)
|
|
|
|
reset_option="--soft"
|
|
|
|
;;
|
|
|
|
H)
|
|
|
|
reset_option="--hard"
|
|
|
|
;;
|
2020-04-10 06:09:08 +00:00
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-04-10 06:41:12 +00:00
|
|
|
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 --header='select which commit to reset' --preview "echo {} | awk '{print \$1}' | \
|
2020-04-10 07:20:13 +00:00
|
|
|
xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} show --color=always __" | \
|
|
|
|
awk '{print $1}')
|
|
|
|
[[ -z "${selected_commites}" ]] && exit 0
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" reset "${selected_commites}" "${reset_option}"
|
2020-04-10 06:41:12 +00:00
|
|
|
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
|