2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-10 01:10:25 +00:00
dotbare/scripts/fstash
2020-05-19 18:01:01 +10:00

85 lines
2.5 KiB
Bash
Executable File

#!/usr/bin/env bash
#
# stash operation using fzf
#
# @params
# Globals
# ${mydir}: current dir of the script, source purpose
# ${stash_command}: stash command, pop, apply or file/delete to stash file or delete 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="$( 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 fstash [-h] [-f] [-d] [-p] ...\n"
echo -e "save/apply/delect stash using fzf, also supports stashing individual file\n"
echo -e "Default: running fstash will apply the selected stash\n"
echo -e "optional arguments:"
echo -e " -h\t\tshow this help message and exit"
echo -e " -f\t\tselect modified files through fzf and stash them"
echo -e " -d\t\tdelete the selected stash from stash list"
echo -e " -p\t\tuse pop instead of apply when retrieving stash"
}
stash_command="apply"
while getopts ":hdfpy" opt; do
case "$opt" in
p)
stash_command="pop"
;;
f)
stash_command="file"
;;
d)
stash_command="delete"
;;
y)
confirm='y'
;;
h)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
usage
exit 1
;;
esac
done
if [[ "${stash_command}" == "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 [[ "${stash_command}" == "delete" ]]; 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}"
[[ -z "${confirm}" ]] && 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