2
0
mirror of https://github.com/kazhala/dotbare synced 2024-11-02 09:40:27 +00:00
dotbare/scripts/fstash

86 lines
2.5 KiB
Plaintext
Raw Normal View History

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
2020-05-15 08:19:22 +00:00
# ${stash_command}: stash command, pop, apply or file/delete to stash file or delete stash
2020-05-01 02:51:05 +00:00
# ${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
}
2020-05-01 04:02:23 +00:00
stash_command="apply"
2020-04-30 23:44:57 +00:00
2020-05-15 08:19:22 +00:00
while getopts ":hdfpy" 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"
;;
2020-04-30 23:44:57 +00:00
f)
2020-05-15 08:19:22 +00:00
stash_command="file"
2020-05-01 02:51:12 +00:00
;;
d)
2020-05-15 08:19:22 +00:00
stash_command="delete"
;;
y)
confirm='y'
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-15 08:19:22 +00:00
if [[ "${stash_command}" == "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}
2020-05-15 08:19:22 +00:00
elif [[ "${stash_command}" == "delete" ]]; then
2020-05-01 02:51:12 +00:00
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}"
2020-05-15 08:19:22 +00:00
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
2020-05-01 02:51:12 +00:00
[[ "${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