2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-04-30 23:44:57 +00:00
|
|
|
#
|
|
|
|
# stash operation using fzf
|
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-05-01 02:51:05 +00:00
|
|
|
# ${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
|
2020-04-30 23:44:57 +00:00
|
|
|
# Arguments
|
2020-05-02 22:40:01 +00:00
|
|
|
# -h: show help message
|
|
|
|
# -f: select individual files and stash
|
|
|
|
# -d: delete selected stash
|
|
|
|
# -p: use pop instead of apply on the selected stash
|
2020-04-30 23:44:57 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
set -f
|
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-04-30 23:44:57 +00:00
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/get_confirmation.sh
|
|
|
|
source "${mydir}"/../helper/git_query.sh
|
|
|
|
|
|
|
|
function usage() {
|
2020-05-13 03:41:58 +00:00
|
|
|
echo -e "Usage: dotbare fstash [-h] [-f] [-d] [-p] ...\n"
|
2020-05-01 02:51:05 +00:00
|
|
|
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"
|
2020-04-30 23:44:57 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-05-01 02:51:05 +00:00
|
|
|
echo -e " -h\tshow this help message and exit"
|
|
|
|
echo -e " -f\tselect modified files through fzf and stash them"
|
2020-05-01 04:02:23 +00:00
|
|
|
echo -e " -d\tdelete the selected stash from stash list"
|
|
|
|
echo -e " -p\tuse pop instead of apply when retrieving stash"
|
2020-04-30 23:44:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
stash_file=""
|
2020-05-01 02:51:12 +00:00
|
|
|
delete_stash=""
|
2020-05-01 04:02:23 +00:00
|
|
|
stash_command="apply"
|
2020-04-30 23:44:57 +00:00
|
|
|
|
2020-05-01 04:02:23 +00:00
|
|
|
while getopts ":hdfp" opt; do
|
2020-04-30 23:44:57 +00:00
|
|
|
case "$opt" in
|
2020-05-01 04:02:23 +00:00
|
|
|
p)
|
|
|
|
stash_command="pop"
|
|
|
|
break
|
|
|
|
;;
|
2020-04-30 23:44:57 +00:00
|
|
|
f)
|
2020-05-01 04:02:23 +00:00
|
|
|
stash_file="true"
|
2020-05-01 02:51:12 +00:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
d)
|
2020-05-01 04:02:23 +00:00
|
|
|
delete_stash="true"
|
2020-05-01 02:51:12 +00:00
|
|
|
break
|
2020-04-30 23:44:57 +00:00
|
|
|
;;
|
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2020-05-01 02:33:00 +00:00
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
2020-04-30 23:44:57 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-05-01 02:33:00 +00:00
|
|
|
if [[ -n "${stash_file}" ]]; then
|
2020-05-01 02:51:12 +00:00
|
|
|
selected_file=$(get_modified_file "select files to add to a stash")
|
|
|
|
[[ -z "${selected_file}" ]] && exit 1
|
2020-05-01 04:02:23 +00:00
|
|
|
# shellcheck disable=SC2086
|
2020-05-01 02:51:12 +00:00
|
|
|
/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}"
|
2020-05-01 02:33:00 +00:00
|
|
|
else
|
2020-05-01 02:51:12 +00:00
|
|
|
selected_stash=$(get_stash "select stash to apply" "true")
|
2020-05-01 02:33:00 +00:00
|
|
|
[[ -z "${selected_stash}" ]] && exit 1
|
2020-05-01 04:02:23 +00:00
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash "${stash_command}" "${selected_stash}"
|
2020-05-01 02:33:00 +00:00
|
|
|
fi
|