refactor(fcheckout): refactor argument handling and handle space

pull/13/head
kevin zhuang 4 years ago
parent 1af316c608
commit b6ad51301c

@ -11,10 +11,13 @@
# the selected commit 6 char code
# e.g. b60b330
#######################################
# TODO: line33 "${files[*]}" cannot handle space in file names, but "${files[@]}" won't get properly
# processed by git for whatever reason just in this preview situation but works every where else, HELP needed.
# although this won't affect any real functionality or break the code, it will just print a error message in preview.
function get_commit() {
local header="${1:-select a commit}"
local files="$2"
if [[ -z "${files}" ]]; then
local files=("${@:2}")
if [[ "${#files[@]}" -eq 0 ]]; then
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" \
log --oneline --color=always --decorate=short \
| fzf --no-multi --header="${header}" \
@ -29,7 +32,7 @@ function get_commit() {
| 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 __ $files" \
diff --color=always __ ${files[*]}" \
| awk '{print $1}'
fi
}

@ -36,28 +36,32 @@ function usage() {
}
action_type="modified"
selected_files=()
while getopts ":habcy" opt
do
case "$opt" in
a)
action_type="allfiles"
while [[ "$#" -gt 0 ]]; do
case "$1" in
-s|--select)
action_type="select"
shift
;;
b)
-b|--branch)
action_type="branch"
shift
;;
c)
-c|--commit)
action_type="commit"
shift
;;
y)
-y|--yes)
confirm="y"
shift
;;
h)
-h|--help)
usage
exit 0
;;
*)
echo "Invalid option: ${OPTARG}" >&2
echo "Invalid option: $1" >&2
usage
exit 1
;;
@ -69,33 +73,32 @@ if [[ "${action_type}" == "branch" ]]; then
selected_branch=$(get_branch 'Select a branch to checkout')
[[ -z "${selected_branch}" ]] && exit 0
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_branch}"
exit 0
elif [[ "${action_type}" == "commit" ]]; then
# checkout commit
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}"
exit 0
else
# checkout files (reset file changes back to HEAD)
if [[ "${action_type}" == "modified" ]]; then
selected_files=$(get_modified_file 'select a file to checkout version in HEAD' | tr '\n' ' ')
[[ -z "${selected_files}" ]] && exit 0
echo "(dryrun) dotbare checkout -- ${selected_files}"
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
[[ "${confirm}" != 'y' ]] && exit 1
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- ${selected_files}
elif [[ "${action_type}" == "allfiles" ]]; then
selected_files=$(get_git_file 'select a file to checkout' | tr '\n' ' ')
[[ -z "${selected_files}" ]] && exit 0
# continue select a commit and then checkout the file back to the selected commit
selected_commit=$(get_commit 'select the target commit' "${selected_files}")
[[ -z "${selected_commit}" ]] && exit 0
echo "(dryrun) dotbare checkout ${selected_commit} -- ${selected_files}"
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
[[ "${confirm}" != 'y' ]] && exit 0
# shellcheck disable=SC2086
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" ${selected_files}
fi
elif [[ "${action_type}" == "modified" ]]; then
# checkout modified file back to version in HEAD
while IFS= read -r line; do
selected_files+=("${line}")
done < <(get_modified_file 'select a file to checkout version in HEAD')
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
[[ -z "${confirm}" ]] && echo "(dryrun) dotbare checkout --" "${selected_files[@]}"
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
[[ "${confirm}" != 'y' ]] && exit 1
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout -- "${selected_files[@]}"
elif [[ "${action_type}" == "select" ]]; then
# checkout selected files to a selected commit
while IFS= read -r line; do
selected_files+=("${line}")
done < <(get_git_file 'select a file to checkout')
[[ "${#selected_files[@]}" -eq 0 ]] && exit 1
# continue select a commit and then checkout the file back to the selected commit
selected_commit=$(get_commit 'select the target commit' "${selected_files[@]}")
[[ -z "${selected_commit}" ]] && exit 1
[[ -z "${confirm}" ]] && echo "(dryrun) dotbare checkout ${selected_commit} --" "${selected_files[@]}"
[[ -z "${confirm}" ]] && confirm=$(get_confirmation "Confirm?")
[[ "${confirm}" != 'y' ]] && exit 0
/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commit}" "${selected_files[@]}"
fi

Loading…
Cancel
Save