mirror of
https://github.com/kazhala/dotbare
synced 2024-11-04 06:00:45 +00:00
98 lines
2.9 KiB
Bash
Executable File
98 lines
2.9 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_files}: selected files to stash
|
|
# ${selected_stash}: selected stash to apply
|
|
# ${confirm}: user confirm status
|
|
# Arguments
|
|
# -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
|
|
|
|
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] [-s] [-d] [-p] ...\n"
|
|
echo -e "View and manage stash interactively.\n"
|
|
echo -e "Default: list all stashes and apply the selected stash.\n"
|
|
echo -e "optional arguments:"
|
|
echo -e " -h, --help\t\tshow this help message and exit."
|
|
echo -e " -s, --select\t\tlist modified files and stash the selected files."
|
|
echo -e " -d, --delete\t\tlist all stashes and delete the selected stash from stash list."
|
|
echo -e " -p, --pop\t\tuse 'stash pop' instead of 'stash apply'."
|
|
}
|
|
|
|
stash_command="apply"
|
|
selected_files=()
|
|
selected_stash=""
|
|
confirm=""
|
|
|
|
while [[ "$#" -gt 0 ]]; do
|
|
case "$1" in
|
|
-p|--pop)
|
|
stash_command="pop"
|
|
shift
|
|
;;
|
|
-s|--select)
|
|
stash_command="select"
|
|
shift
|
|
;;
|
|
-d|--delete)
|
|
stash_command="delete"
|
|
shift
|
|
;;
|
|
-y|--yes)
|
|
confirm="y"
|
|
shift
|
|
;;
|
|
-h|--help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
echo "Invalid option: $1" >&2
|
|
usage
|
|
exit 1
|
|
;;
|
|
esac
|
|
done
|
|
|
|
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
|
|
git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash -- "${selected_files[@]}"
|
|
elif [[ "${stash_command}" == "delete" ]]; then
|
|
selected_stash=$(get_stash "select stash to delete")
|
|
[[ -z "${selected_stash}" ]] && exit 1
|
|
[[ -z "${confirm}" ]] && \
|
|
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
|
|
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
|
|
[[ -z "${confirm}" ]] && echo "(dryrun) ${stash_command} ${selected_stash}"
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation)
|
|
[[ "${confirm}" != 'y' ]] && exit 1
|
|
git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" stash "${stash_command}" "${selected_stash}"
|
|
fi
|