From 0d34ff50f82e391fe6201a906ce11a31e8ec3b9c Mon Sep 17 00:00:00 2001 From: Wenxuan Date: Sat, 9 Jun 2018 16:46:55 +0800 Subject: [PATCH] Add git-checkout-file & git-clean functions Former-commit-id: 72fc7f4c519bc798d1e510eb5a7df582f3f4d487 --- README.md | 51 +++++++++++++++++++++++------------------------ forgit.plugin.zsh | 29 ++++++++++++++++++++++----- 2 files changed, 49 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index 46f3477..dab95cb 100644 --- a/README.md +++ b/README.md @@ -35,41 +35,23 @@ Interactive `git add` selector ![screenshot](screenshot-ga.png) -| Keybind | Action | -| ---------------- | -------------------------- | -| `` | Mark/Unmark(and move down) | -| `` | Reverse selection | -| `` | Confirm and quit | -| `` | Selection down/up | -| `` | Toggle preview window | -| `` | Toggle preview wrap | -| `` | Preview down/up | - ### glo Interactive `git log` browser ![screenshot](screenshot-glo.png) -| Keybind | Action | -| ---------------- | --------------------- | -| `` | Fullscreen preview | -| `` | Selection down/up | -| `` | Toggle preview window | -| `` | Toggle preview wrap | -| `` | Preview down/up | - ### gd Interactive `git diff` browser -| Keybind | Action | -| ---------------- | --------------------- | -| `` | Fullscreen preview | -| `` | Selection down/up | -| `` | Toggle preview window | -| `` | Toggle preview wrap | -| `` | Preview down/up | +### gcf + +Interactive `git checkout ` selector + +### gclean + +Interactive `git clean` selector ### gi @@ -77,6 +59,21 @@ Interactive `.gitignore` generator ![screenshot](screenshot-gi.png) +## Default keybinds + +| Keybind | Action | +| ---------- | --------------------- | +| `` | Confirm | +| `` | Toggle mark | +| `` | Selection up | +| `` | Selection down | +| `` | Preview up | +| `` | Preview down | +| `` | Toggle preview wrap | +| `` | Toggle preview window | +| `` | Toggle selection | +| `` | Toggle sort | + ## Custom options You can change the default aliases by defining these variables below. @@ -87,6 +84,8 @@ forgit_log=glo forgit_diff=gd forgit_add=ga forgit_ignore=gi +forgit_restore=gcf +forgit_clean=gclean ``` You can add custom fzf options for `forgit`, including keybinds, layout, etc. @@ -106,7 +105,7 @@ FORGIT_FZF_DEFAULT_OPTS=" - Hit `q` to Quit from full screen preview any time. - Install [`diff-so-fancy`](https://github.com/so-fancy/diff-so-fancy) to have better `diff` output. -- Call `glo` with arguments to get logs only related to these files or directories(eg, `glo main.go test.go`). +- Commands like `glo`, `gd`, `gcf` and `gclean` accept path arguments to restrain the items listed in fzf(eg, `glo main.go test.go`, `gclean output/`). - Call `gi` with arguments to get wanted `.gitignore` contents directly(eg, `gi c++`). ## [License](LICENSE.txt) diff --git a/forgit.plugin.zsh b/forgit.plugin.zsh index 2b7f47c..cba4e65 100644 --- a/forgit.plugin.zsh +++ b/forgit.plugin.zsh @@ -3,11 +3,8 @@ forgit::fzf() { $FZF_DEFAULT_OPTS --ansi --height '80%' - --bind='alt-v:page-up' - --bind='ctrl-v:page-down' --bind='alt-k:preview-up,alt-p:preview-up' --bind='alt-j:preview-down,alt-n:preview-down' - --bind='alt-a:select-all' --bind='ctrl-r:toggle-all' --bind='ctrl-s:toggle-sort' --bind='?:toggle-preview' @@ -69,7 +66,8 @@ forgit::log() { forgit::diff() { forgit::inside_work_tree || return 1 local cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy" - git ls-files --modified $(git rev-parse --show-toplevel)| + [[ $# -eq 0 ]] && local files=$(git rev-parse --show-toplevel) || local files="$@" + git ls-files --modified "$files"| forgit::fzf +m -0 \ --bind="enter:execute($cmd |LESS='-R' less)" \ --preview="$cmd" @@ -91,7 +89,26 @@ forgit::add() { --preview="git diff --color=always -- {-1} $forgit_emojify $forgit_fancy" | cut -d] -f2 | sed 's/.* -> //') # for rename case - [[ -n $files ]] && echo $files |xargs -I{} git add {} && git status + [[ -n "$files" ]] && echo "$files" |xargs -I{} git add {} && git status --short && return + echo 'Nothing to add.' +} + +# git checkout-restore selector +forgit::restore() { + forgit::inside_work_tree || return 1 + local cmd="git diff --color=always -- {} $forgit_emojify $forgit_fancy" + local files=$(git ls-files --modified $(git rev-parse --show-toplevel)| + forgit::fzf -m -0 --preview="$cmd") + [[ -n "$files" ]] && echo "$files" |xargs -I{} git checkout {} && git status --short && return + echo 'Nothing to restore.' +} + +forgit::clean() { + forgit::inside_work_tree || return 1 + # Note: Postfix '/' in directory path should be removed. Otherwise the directory itself will not be removed. + local files=$(git clean -xdfn "$@"| awk '{print $3}'| forgit::fzf -m -0 |sed 's#/$##') + [[ -n "$files" ]] && echo "$files" |xargs -I{} git clean -xdf {} + echo 'Nothing to clean.' } # git ignore generator @@ -138,3 +155,5 @@ alias ${forgit_log:-ga}='forgit::add' alias ${forgit_log:-glo}='forgit::log' alias ${forgit_diff:-gd}='forgit::diff' alias ${forgit_ignore:-gi}='forgit:ignore' +alias ${forgit_restore:-gcf}='forgit::restore' +alias ${forgit_clean:-gclean}='forgit::clean'