2020-05-11 01:56:27 +00:00
|
|
|
#!/usr/bin/env bash
|
2020-04-11 06:38:49 +00:00
|
|
|
#
|
2020-04-24 07:26:45 +00:00
|
|
|
# checkout files/commit/branches using fzf
|
2020-04-11 06:38:49 +00:00
|
|
|
#
|
|
|
|
# @params
|
|
|
|
# Globals
|
2020-04-14 07:17:42 +00:00
|
|
|
# ${mydir}: current directory of the script, used for imports
|
2020-05-15 08:04:43 +00:00
|
|
|
# ${action_type}: what type of git commands to use, branch, commit files, modified files
|
2020-04-14 07:17:42 +00:00
|
|
|
# ${selected_branch}: selected_branch to switch
|
|
|
|
# ${selected_files}: selected_files to checkout to the version in HEAD
|
2020-04-17 07:55:22 +00:00
|
|
|
# ${selected_commit}: selected commit to checkout
|
2020-04-24 07:26:45 +00:00
|
|
|
# ${confirm}: confirm status of the user
|
2020-04-11 06:38:49 +00:00
|
|
|
# Arguments
|
2020-04-14 08:16:14 +00:00
|
|
|
# -h: show help message
|
2020-04-14 07:00:37 +00:00
|
|
|
# -a: search all files instead of just the modified files
|
|
|
|
# -b: search branch and checkout branch
|
2020-04-24 07:26:45 +00:00
|
|
|
# -c: search commit and checkout commit
|
2020-04-11 06:38:49 +00:00
|
|
|
|
|
|
|
set -e
|
2020-04-24 07:26:45 +00:00
|
|
|
set -f
|
2020-04-11 06:38:49 +00:00
|
|
|
|
2020-05-13 07:30:54 +00:00
|
|
|
mydir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
|
2020-04-14 07:17:42 +00:00
|
|
|
source "${mydir}"/../helper/set_variable.sh
|
|
|
|
source "${mydir}"/../helper/get_confirmation.sh
|
2020-04-16 05:43:33 +00:00
|
|
|
source "${mydir}"/../helper/git_query.sh
|
2020-04-11 06:38:49 +00:00
|
|
|
|
|
|
|
function usage() {
|
2020-04-14 08:16:14 +00:00
|
|
|
echo -e "Usage: dotbare fcheckout [-h] [-a] [-b] [-c] ...\n"
|
2020-05-19 07:51:13 +00:00
|
|
|
echo -e "Checkout files/commit/branch using fzf\n"
|
|
|
|
echo -e "Default: checkout files back to HEAD (Reset changes back to HEAD)\n"
|
2020-04-11 06:38:49 +00:00
|
|
|
echo -e "optional arguments:"
|
2020-05-19 07:51:13 +00:00
|
|
|
echo -e " -h\t\tshow this help message and exit"
|
|
|
|
echo -e " -a\t\tsearch all files and select a commit to checkout for selected files"
|
|
|
|
echo -e " -b\t\tlist all branch and checkout/switch the selected branch"
|
|
|
|
echo -e " -c\t\tlist all commits and checkout selected commit"
|
2020-04-11 06:38:49 +00:00
|
|
|
}
|
|
|
|
|
2020-05-15 08:04:43 +00:00
|
|
|
action_type="modified"
|
2020-04-11 06:38:49 +00:00
|
|
|
|
2020-05-15 08:04:43 +00:00
|
|
|
while getopts ":habcy" opt
|
2020-04-11 06:38:49 +00:00
|
|
|
do
|
|
|
|
case "$opt" in
|
2020-04-14 07:00:37 +00:00
|
|
|
a)
|
2020-05-15 08:04:43 +00:00
|
|
|
action_type="allfiles"
|
2020-04-11 06:38:49 +00:00
|
|
|
;;
|
2020-04-14 06:22:02 +00:00
|
|
|
b)
|
2020-05-15 08:04:43 +00:00
|
|
|
action_type="branch"
|
2020-04-14 07:55:55 +00:00
|
|
|
;;
|
|
|
|
c)
|
2020-05-15 08:04:43 +00:00
|
|
|
action_type="commit"
|
|
|
|
;;
|
|
|
|
y)
|
|
|
|
confirm="y"
|
2020-04-14 06:22:02 +00:00
|
|
|
;;
|
2020-04-11 06:38:49 +00:00
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-05-15 08:04:43 +00:00
|
|
|
if [[ "${action_type}" == "branch" ]]; then
|
2020-04-14 08:14:22 +00:00
|
|
|
# checkout branch
|
2020-04-16 05:04:34 +00:00
|
|
|
selected_branch=$(get_branch 'Select a branch to checkout')
|
2020-04-14 07:55:55 +00:00
|
|
|
[[ -z "${selected_branch}" ]] && exit 0
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_branch}"
|
|
|
|
exit 0
|
2020-05-15 08:04:43 +00:00
|
|
|
elif [[ "${action_type}" == "commit" ]]; then
|
2020-04-24 07:26:45 +00:00
|
|
|
# checkout commit
|
2020-04-17 07:55:22 +00:00
|
|
|
selected_commit=$(get_commit 'Select a commit to checkout')
|
|
|
|
[[ -z "${selected_commit}" ]] && exit 0
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}"
|
2020-04-14 07:55:55 +00:00
|
|
|
exit 0
|
2020-04-14 07:00:37 +00:00
|
|
|
else
|
2020-04-14 08:14:22 +00:00
|
|
|
# checkout files (reset file changes back to HEAD)
|
2020-05-15 08:04:43 +00:00
|
|
|
if [[ "${action_type}" == "modified" ]]; then
|
2020-04-17 06:45:40 +00:00
|
|
|
selected_files=$(get_modified_file 'select a file to checkout version in HEAD' | tr '\n' ' ')
|
2020-04-17 06:10:59 +00:00
|
|
|
[[ -z "${selected_files}" ]] && exit 0
|
2020-04-17 06:45:40 +00:00
|
|
|
echo "(dryrun) dotbare checkout -- ${selected_files}"
|
2020-05-15 08:04:43 +00:00
|
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
|
|
|
|
[[ "${confirm}" != 'y' ]] && exit 1
|
2020-04-17 07:55:22 +00:00
|
|
|
# shellcheck disable=SC2086
|
2020-04-17 06:45:40 +00:00
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- ${selected_files}
|
2020-05-15 08:04:43 +00:00
|
|
|
elif [[ "${action_type}" == "allfiles" ]]; then
|
2020-04-17 06:45:40 +00:00
|
|
|
selected_files=$(get_git_file 'select a file to checkout' | tr '\n' ' ')
|
2020-04-17 06:10:59 +00:00
|
|
|
[[ -z "${selected_files}" ]] && exit 0
|
2020-04-17 05:50:00 +00:00
|
|
|
# continue select a commit and then checkout the file back to the selected commit
|
2020-04-17 07:55:22 +00:00
|
|
|
selected_commit=$(get_commit 'select the target commit' "${selected_files}")
|
|
|
|
[[ -z "${selected_commit}" ]] && exit 0
|
|
|
|
echo "(dryrun) dotbare checkout ${selected_commit} -- ${selected_files}"
|
2020-05-15 08:04:43 +00:00
|
|
|
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
|
2020-04-17 05:50:00 +00:00
|
|
|
[[ "${confirm}" != 'y' ]] && exit 0
|
2020-04-17 07:55:22 +00:00
|
|
|
# shellcheck disable=SC2086
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" ${selected_files}
|
2020-04-17 05:50:00 +00:00
|
|
|
fi
|
2020-04-11 06:38:49 +00:00
|
|
|
fi
|