From 0534c5cef862734d650a25aa2b2816800a0ecac9 Mon Sep 17 00:00:00 2001 From: kevin zhuang Date: Tue, 14 Apr 2020 17:55:55 +1000 Subject: [PATCH] support checkout commits --- scripts/fcheckout | 67 +++++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/scripts/fcheckout b/scripts/fcheckout index cf219bc..746d5fd 100755 --- a/scripts/fcheckout +++ b/scripts/fcheckout @@ -31,15 +31,22 @@ function usage() { all_files="" branch="" +commits="" -while getopts ":hab" opt +while getopts ":habc" opt do case "$opt" in a) all_files='true' + break ;; b) branch='true' + break + ;; + c) + commits='true' + break ;; h) usage @@ -64,30 +71,38 @@ if [[ -n "${branch}" ]]; then }' | \ fzf --no-multi --header='select a branch to checkout' \ --preview="/usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} log --oneline --graph --color=always --decorate=short {}") - [[ -z "${selected_branch}" ]] && exit 0 - /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_branch}" - exit 0 -fi - -if [[ -n "${all_files}" ]]; then - selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" ls-files --full-name --directory "${DOTBARE_TREE}" | \ - fzf --multi --header='select a file to checkout version in HEAD' --preview "head -50 ${DOTBARE_TREE}/{}" | \ - awk -v home="${DOTBARE_TREE}" '{print home "/" $0}') + [[ -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 + selected_commits=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" log --oneline --color=always --decorate=short | \ + fzf --no-multi --header='select which commit to reset' --preview "echo {} | awk '{print \$1}' | \ + xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} show --color=always __" | \ + awk '{print $1}') + [[ -z "${selected_commits}" ]] && exit 0 + /usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" checkout "${selected_commits}" + exit 0 else - selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --name-status | \ - awk '{print "\033[31m" $1 " " $2}' | \ - fzf --multi --header='select a file to checkout version in HEAD' \ - --preview "echo {} | awk '{print \$2}' | \ - xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} diff --color=always ${DOTBARE_TREE}/__" | \ - awk -v home="${DOTBARE_TREE}" '{print home "/" $2}') + if [[ -n "${all_files}" ]]; then + selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" ls-files --full-name --directory "${DOTBARE_TREE}" | \ + fzf --multi --header='select a file to checkout version in HEAD' --preview "head -50 ${DOTBARE_TREE}/{}" | \ + awk -v home="${DOTBARE_TREE}" '{print home "/" $0}') + else + selected_files=$(/usr/bin/git --git-dir="${DOTBARE_DIR}" --work-tree="${DOTBARE_TREE}" diff --name-status | \ + awk '{print "\033[31m" $1 " " $2}' | \ + fzf --multi --header='select a file to checkout version in HEAD' \ + --preview "echo {} | awk '{print \$2}' | \ + xargs -I __ /usr/bin/git --git-dir=${DOTBARE_DIR} --work-tree=${DOTBARE_TREE} diff --color=always ${DOTBARE_TREE}/__" | \ + awk -v home="${DOTBARE_TREE}" '{print home "/" $2}') + fi + [[ -z "${selected_files}" ]] && exit 0 + 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}" fi -[[ -z "${selected_files}" ]] && exit 0 -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}"