From 7c1d3b859ed48f1e87acbf2a738e6ea28bfd6b2e Mon Sep 17 00:00:00 2001 From: Wenxuan Zhang Date: Wed, 7 Apr 2021 17:14:57 +0800 Subject: [PATCH] feat: add gco to checkout a commit --- README.md | 14 ++++++++----- forgit.plugin.zsh | 52 ++++++++++++++++++++++++++++++++--------------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 948ebbc..446ddac 100644 --- a/README.md +++ b/README.md @@ -85,6 +85,10 @@ source (curl -sSL git.io/forgit-fish | psub) - **Interactive `git checkout ` selector** (`gcf`) +- **Interactive `git checkout ` selector** (`gcb`) + +- **Interactive `git checkout ` selector** (`gco`) + - **Interactive `git stash` viewer** (`gss`) - **Interactive `git clean` selector** (`gclean`) @@ -95,8 +99,6 @@ source (curl -sSL git.io/forgit-fish | psub) - **Interactive `git commit --fixup && git rebase -i --autosquash` selector** (`gfu`) -- **Interactive `git checkout` selector** (`gcb`) - ### ⌨ Keybinds | Key | Action | @@ -131,12 +133,13 @@ forgit_add=ga forgit_reset_head=grh forgit_ignore=gi forgit_checkout_file=gcf +forgit_checkout_branch=gcb +forgit_checkout_commit=gco forgit_clean=gclean forgit_stash_show=gss forgit_cherry_pick=gcp forgit_rebase=grb forgit_fixup=gfu -forgit_checkout_branch=gcb ``` #### pagers @@ -178,11 +181,12 @@ Customizing fzf options for each command individually is also supported: | `gd` | `FORGIT_DIFF_FZF_OPTS` | | `grh` | `FORGIT_RESET_HEAD_FZF_OPTS` | | `gcf` | `FORGIT_CHECKOUT_FILE_FZF_OPTS` | +| `gcb` | `FORGIT_CHECKOUT_BRANCH_FZF_OPTS` | +| `gco` | `FORGIT_CHECKOUT_COMMIT_FZF_OPTS` | | `gss` | `FORGIT_STASH_FZF_OPTS` | | `gclean` | `FORGIT_CLEAN_FZF_OPTS` | | `grb` | `FORGIT_REBASE_FZF_OPTS` | | `gfu` | `FORGIT_FIXUP_FZF_OPTS` | -| `gcb` | `FORGIT_CHECKOUT_BRANCH_FZF_OPTS` | Complete loading order of fzf options is: @@ -221,7 +225,7 @@ FORGIT_LOG_FZF_OPTS=' ### 💡 Tips -- Commands like `glo`, `gd`, `gcf` and `gclean` accept arguments to restrain the items listed in fzf(eg, `glo develop`, `glo f738479..188a849b -- main.go`, `gclean output/` etc.). +- Most of the commands accept optional arguments (eg, `glo develop`, `glo f738479..188a849b -- main.go`, `gco master`). - `gd` supports specifying revision(eg, `gd HEAD~`, `gd v1.0 README.md`). - Call `gi` with arguments to get the wanted `.gitignore` contents directly(eg, `gi cmake c++`). - You can use the commands as sub-commands of `git`, see [#147](https://github.com/wfxr/forgit/issues/147) for details. diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index ae8945a..fdb6073 100755 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -107,21 +107,6 @@ forgit::reset::head() { echo 'Nothing to unstage.' } -# git checkout-restore selector -forgit::checkout::file() { - forgit::inside_work_tree || return 1 - local cmd files opts - cmd="git diff --color=always -- {} | $forgit_diff_pager" - opts=" - $FORGIT_FZF_DEFAULT_OPTS - -m -0 - $FORGIT_CHECKOUT_FILE_FZF_OPTS - " - files="$(git ls-files --modified "$(git rev-parse --show-toplevel)"| FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd")" - [[ -n "$files" ]] && echo "$files" | tr '\n' '\0' | xargs -0 -I% git checkout % && git status --short && return - echo 'Nothing to restore.' -} - # git stash viewer forgit::stash::show() { forgit::inside_work_tree || return 1 @@ -215,6 +200,22 @@ forgit::fixup() { } +# git checkout-file selector +forgit::checkout::file() { + forgit::inside_work_tree || return 1 + [[ $# -ne 0 ]] && { git checkout -- "$*"; return $?; } + local cmd files opts + cmd="git diff --color=always -- {} | $forgit_diff_pager" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + -m -0 + $FORGIT_CHECKOUT_FILE_FZF_OPTS + " + files="$(git ls-files --modified "$(git rev-parse --show-toplevel)"| FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd")" + [[ -n "$files" ]] && echo "$files" | tr '\n' '\0' | xargs -0 -I% git checkout % +} + +# git checkout-branch selector forgit::checkout::branch() { forgit::inside_work_tree || return 1 [[ $# -ne 0 ]] && { git checkout -b "$*"; return $?; } @@ -223,12 +224,30 @@ forgit::checkout::branch() { preview="git log {} --graph --pretty=format:'$forgit_log_format' --color=always --abbrev-commit --date=relative" opts=" $FORGIT_FZF_DEFAULT_OPTS - +s +m --tiebreak=index --ansi + +s +m --tiebreak=index $FORGIT_CHECKOUT_BRANCH_FZF_OPTS " eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | xargs -I% git checkout % } +# git checkout-commit selector +forgit::checkout::commit() { + forgit::inside_work_tree || return 1 + [[ $# -ne 0 ]] && { git checkout "$*"; return $?; } + local cmd opts graph + cmd="echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % | $forgit_show_pager" + opts=" + $FORGIT_FZF_DEFAULT_OPTS + +s +m --tiebreak=index + --bind=\"ctrl-y:execute-silent(echo {} |grep -Eo '[a-f0-9]+' | head -1 | tr -d '[:space:]' |${FORGIT_COPY_CMD:-pbcopy})\" + $FORGIT_COMMIT_FZF_OPTS + " + graph=--graph + [[ $FORGIT_LOG_GRAPH_ENABLE == false ]] && graph= + eval "git log $graph --color=always --format='$forgit_log_format' $forgit_emojify" | + FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git checkout % -- +} + # git ignore generator export FORGIT_GI_REPO_REMOTE=${FORGIT_GI_REPO_REMOTE:-https://github.com/dvcs/gitignore} export FORGIT_GI_REPO_LOCAL="${FORGIT_GI_REPO_LOCAL:-${XDG_CACHE_HOME:-$HOME/.cache}/forgit/gi/repos/dvcs/gitignore}" @@ -302,6 +321,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then alias "${forgit_ignore:-gi}"='forgit::ignore' alias "${forgit_checkout_file:-gcf}"='forgit::checkout::file' alias "${forgit_checkout_branch:-gcb}"='forgit::checkout::branch' + alias "${forgit_checkout_commit:-gco}"='forgit::checkout::commit' alias "${forgit_clean:-gclean}"='forgit::clean' alias "${forgit_stash_show:-gss}"='forgit::stash::show' alias "${forgit_cherry_pick:-gcp}"='forgit::cherry::pick'