2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-06 09:20:25 +00:00
dotbare/scripts/fstash
2020-05-13 13:41:58 +10:00

88 lines
2.4 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# stash operation using fzf
#
# @params
# Globals
# ${mydir}: current dir of the script, source purpose
# ${stash_file}: select modified files and stash
# ${selected_file}: selected files to stash
# ${selected_stash}: selected stash to apply
# Arguments
# -h: show help message
# -f: select individual files and stash
# -d: delete selected stash
# -p: use pop instead of apply on the selected stash
set -e
set -f
mydir="${0%/*}"
source "${mydir}"/../helper/set_variable.sh
source "${mydir}"/../helper/get_confirmation.sh
source "${mydir}"/../helper/git_query.sh
function usage() {
echo -e "Usage: dotbare fstash [-h] [-f] [-d] [-p] ...\n"
echo -e "save/apply stash using fzf"
echo -e "By default, running fstash will apply the selected stash"
echo -e "To stash only some files, add -f flag\n"
echo -e "optional arguments:"
echo -e " -h\tshow this help message and exit"
echo -e " -f\tselect modified files through fzf and stash them"
echo -e " -d\tdelete the selected stash from stash list"
echo -e " -p\tuse pop instead of apply when retrieving stash"
}
stash_file=""
delete_stash=""
stash_command="apply"
while getopts ":hdfp" opt; do
case "$opt" in
p)
stash_command="pop"
break
;;
f)
stash_file="true"
break
;;
d)
delete_stash="true"
break
;;
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
if [[ -n "${stash_file}" ]]; then
selected_file=$(get_modified_file "select files to add to a stash")
[[ -z "${selected_file}" ]] && exit 1
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash -- ${selected_file}
elif [[ -n "${delete_stash}" ]]; then
selected_stash=$(get_stash "select stash to delete")
[[ -z "${selected_stash}" ]] && exit 1
while IFS= read -r line; do
echo "(dryrun) Drop ${line}"
done <<< "${selected_stash}"
confirm=$(get_confirmation)
[[ "${confirm}" != 'y' ]] && exit 1
while IFS= read -r line; do
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash drop "${line}"
done <<< "${selected_stash}"
else
selected_stash=$(get_stash "select stash to apply" "true")
[[ -z "${selected_stash}" ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash "${stash_command}" "${selected_stash}"
fi