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-06-25 03:04:47 +00:00
|
|
|
# ${selected_files}: selected files to stash
|
2020-05-01 02:51:05 +00:00
|
|
|
# ${selected_stash}: selected stash to apply
|
2020-06-25 03:04:47 +00:00
|
|
|
# ${confirm}: user confirm status
|
2020-04-30 23:44:57 +00:00
|
|
|
# Arguments
|
2020-06-25 03:04:47 +00:00
|
|
|
# -h|--help: show help message
|
|
|
|
# -s|--select: select individual files and stash
|
|
|
|
# -d|--delete: delete selected stash
|
|
|
|
# -p|--pop: 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-06-25 03:04:47 +00:00
|
|
|
echo -e "Usage: dotbare fstash [-h] [-s] [-d] [-p] ...\n"
|
2020-05-19 08:01:01 +00:00
|
|
|
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"
|
2020-04-30 23:44:57 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-06-25 03:04:47 +00:00
|
|
|
echo -e " -h, --help\t\tshow this help message and exit"
|
|
|
|
echo -e " -s, --select\t\tselect modified files through fzf and stash them"
|
|
|
|
echo -e " -d, --delete\t\tdelete the selected stash from stash list"
|
|
|
|
echo -e " -p, --pop\t\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-06-25 03:04:47 +00:00
|
|
|
selected_files=()
|
|
|
|
selected_stash=""
|
2020-04-30 23:44:57 +00:00
|
|
|
|
2020-06-25 03:04:47 +00:00
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
|
|
case "$1" in
|
|
|
|
-p|--pop)
|
2020-05-01 04:02:23 +00:00
|
|
|
stash_command="pop"
|
2020-06-25 03:04:47 +00:00
|
|
|
shift
|
2020-05-01 04:02:23 +00:00
|
|
|
;;
|
2020-06-25 03:04:47 +00:00
|
|
|
-s|--select)
|
|
|
|
stash_command="select"
|
|
|
|
shift
|
2020-05-01 02:51:12 +00:00
|
|
|
;;
|
2020-06-25 03:04:47 +00:00
|
|
|
-d|--delete)
|
2020-05-15 08:19:22 +00:00
|
|
|
stash_command="delete"
|
2020-06-25 03:04:47 +00:00
|
|
|
shift
|
2020-05-15 08:19:22 +00:00
|
|
|
;;
|
2020-06-25 03:04:47 +00:00
|
|
|
-y|--yes)
|
|
|
|
confirm="y"
|
|
|
|
shift
|
2020-04-30 23:44:57 +00:00
|
|
|
;;
|
2020-06-25 03:04:47 +00:00
|
|
|
-h|--help)
|
2020-04-30 23:44:57 +00:00
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
2020-05-01 02:33:00 +00:00
|
|
|
*)
|
2020-06-25 03:04:47 +00:00
|
|
|
echo "Invalid option: $1" >&2
|
2020-05-01 02:33:00 +00:00
|
|
|
usage
|
2020-06-25 03:08:24 +00:00
|
|
|
exit 1
|
2020-05-01 02:33:00 +00:00
|
|
|
;;
|
2020-04-30 23:44:57 +00:00
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-06-25 03:04:47 +00:00
|
|
|
if [[ "${stash_command}" == "select" ]]; then
|
|
|
|
while IFS= read -r line; do
|
|
|
|
selected_files+=("${line}")
|
|
|
|
done < <(get_modified_file "select files to add to a stash")
|
|
|
|
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash -- "${selected_files[@]}"
|
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
|
2020-06-25 03:04:47 +00:00
|
|
|
[[ -z "${confirm}" ]] && \
|
|
|
|
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-06-25 03:04:47 +00:00
|
|
|
[[ -z "${confirm}" ]] && echo "(dryrun) "${stash_command}" ${selected_stash}"
|
|
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
|
|
|
|
[[ "${confirm}" != 'y' ]] && 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
|