2020-04-11 06:38:49 +00:00
|
|
|
#!/bin/bash
|
|
|
|
#
|
2020-04-14 07:17:42 +00:00
|
|
|
# checkout files/commits/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
|
|
|
|
# ${all_files}: search all tracked files instead of just the modified files
|
|
|
|
# ${branch}: checkout branch
|
|
|
|
# ${selected_branch}: selected_branch to switch
|
|
|
|
# ${selected_files}: selected_files to checkout to the version in HEAD
|
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-14 08:16:14 +00:00
|
|
|
# -c: search commits and checkout commits
|
2020-04-11 06:38:49 +00:00
|
|
|
|
|
|
|
set -e
|
|
|
|
|
|
|
|
mydir="${0%/*}"
|
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-04-11 06:38:49 +00:00
|
|
|
echo -e "Reset/Unstage the selected staged file"
|
|
|
|
echo -e "Or reset the commit to certain point\n"
|
|
|
|
echo -e "optional arguments:"
|
|
|
|
echo -e " -h\t\tshow this help message and exit"
|
2020-04-14 08:16:14 +00:00
|
|
|
echo -e " -a\t\tsearch all files instead of just modified files"
|
|
|
|
echo -e " -b\t\tcheckout branch"
|
|
|
|
echo -e " -c\t\tcheckout single commits"
|
2020-04-11 06:38:49 +00:00
|
|
|
}
|
|
|
|
|
2020-04-14 07:00:37 +00:00
|
|
|
all_files=""
|
2020-04-14 06:22:02 +00:00
|
|
|
branch=""
|
2020-04-14 07:55:55 +00:00
|
|
|
commits=""
|
2020-04-17 05:50:00 +00:00
|
|
|
commit_file=""
|
2020-04-11 06:38:49 +00:00
|
|
|
|
2020-04-17 05:50:00 +00:00
|
|
|
while getopts ":habcC" opt
|
2020-04-11 06:38:49 +00:00
|
|
|
do
|
|
|
|
case "$opt" in
|
2020-04-14 07:00:37 +00:00
|
|
|
a)
|
|
|
|
all_files='true'
|
2020-04-14 07:55:55 +00:00
|
|
|
break
|
2020-04-11 06:38:49 +00:00
|
|
|
;;
|
2020-04-14 06:22:02 +00:00
|
|
|
b)
|
|
|
|
branch='true'
|
2020-04-14 07:55:55 +00:00
|
|
|
break
|
|
|
|
;;
|
|
|
|
c)
|
|
|
|
commits='true'
|
|
|
|
break
|
2020-04-14 06:22:02 +00:00
|
|
|
;;
|
2020-04-17 05:50:00 +00:00
|
|
|
C)
|
|
|
|
commit_file='true'
|
|
|
|
all_files='true'
|
|
|
|
break
|
|
|
|
;;
|
2020-04-11 06:38:49 +00:00
|
|
|
h)
|
|
|
|
usage
|
|
|
|
exit 0
|
|
|
|
;;
|
|
|
|
*)
|
|
|
|
echo "Invalid option: ${OPTARG}" >&2
|
|
|
|
usage
|
|
|
|
exit 1
|
|
|
|
;;
|
|
|
|
esac
|
|
|
|
done
|
|
|
|
|
2020-04-14 07:00:37 +00:00
|
|
|
if [[ -n "${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
|
|
|
|
elif [[ -n "${commits}" ]]; then
|
2020-04-14 08:14:22 +00:00
|
|
|
# checkout commits
|
2020-04-16 05:04:34 +00:00
|
|
|
selected_commits=$(get_commit 'Select a commit to checkout')
|
2020-04-14 07:55:55 +00:00
|
|
|
[[ -z "${selected_commits}" ]] && exit 0
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commits}"
|
|
|
|
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-04-14 07:55:55 +00:00
|
|
|
if [[ -n "${all_files}" ]]; then
|
2020-04-16 05:30:48 +00:00
|
|
|
selected_files=$(get_git_file 'select a file to checkout version in HEAD')
|
2020-04-14 07:55:55 +00:00
|
|
|
else
|
2020-04-16 05:30:48 +00:00
|
|
|
selected_files=$(get_modified_file 'select a file to checkout version in HEAD')
|
2020-04-14 07:55:55 +00:00
|
|
|
fi
|
|
|
|
[[ -z "${selected_files}" ]] && exit 0
|
2020-04-17 05:50:00 +00:00
|
|
|
if [[ -z "${commit_file}" ]]; then
|
|
|
|
while IFS= read -r line; do
|
|
|
|
echo "(dryrun) dotbare checkout -- ${line}"
|
|
|
|
done <<< "${selected_files}"
|
|
|
|
confirm=$(get_confirmation "Confirm?")
|
|
|
|
[[ "${confirm}" != 'y' ]] && exit 0
|
|
|
|
while IFS= read -r line; do
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- "${line}"
|
|
|
|
echo "${line} checkout success"
|
|
|
|
done <<< "${selected_files}"
|
|
|
|
else
|
|
|
|
# continue select a commit and then checkout the file back to the selected commit
|
|
|
|
selected_commits=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
|
|
|
|
log --oneline --color=always --decorate=short \
|
|
|
|
| fzf --no-multi --header="${header}" --preview "echo {} \
|
|
|
|
| awk '{print \$1}' \
|
|
|
|
| xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} \
|
|
|
|
diff --color=always __ $selected_files" \
|
|
|
|
| awk '{print $1}')
|
|
|
|
[[ -z "${selected_commits}" ]] && exit 0
|
|
|
|
while IFS= read -r line; do
|
|
|
|
echo "(dryrun) dotbare checkout ${selected_commits} -- ${line}"
|
|
|
|
done <<< "${selected_files}"
|
|
|
|
confirm=$(get_confirmation "Confirm?")
|
|
|
|
[[ "${confirm}" != 'y' ]] && exit 0
|
|
|
|
while IFS= read -r line; do
|
|
|
|
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commits}" -- "${line}"
|
|
|
|
echo "${line} checkout success"
|
|
|
|
done <<< "${selected_files}"
|
|
|
|
fi
|
2020-04-11 06:38:49 +00:00
|
|
|
fi
|