Add git-checkout-file & git-clean functions

Former-commit-id: 72fc7f4c51
This commit is contained in:
Wenxuan 2018-06-09 16:46:55 +08:00
parent df8e4eab0d
commit 0d34ff50f8
2 changed files with 49 additions and 31 deletions

View file

@ -35,41 +35,23 @@ Interactive `git add` selector
![screenshot](screenshot-ga.png)
| Keybind | Action |
| ---------------- | -------------------------- |
| `<Tab>` | Mark/Unmark(and move down) |
| `<C-r>` | Reverse selection |
| `<Enter>` | Confirm and quit |
| `<C-j/n><C-k/p>` | Selection down/up |
| `<?>` | Toggle preview window |
| `<A-w>` | Toggle preview wrap |
| `<A-j><A-k>` | Preview down/up |
### glo
Interactive `git log` browser
![screenshot](screenshot-glo.png)
| Keybind | Action |
| ---------------- | --------------------- |
| `<Enter>` | Fullscreen preview |
| `<C-j/n><C-k/p>` | Selection down/up |
| `<?>` | Toggle preview window |
| `<A-w>` | Toggle preview wrap |
| `<A-j><A-k>` | Preview down/up |
### gd
Interactive `git diff` browser
| Keybind | Action |
| ---------------- | --------------------- |
| `<Enter>` | Fullscreen preview |
| `<C-j/n><C-k/p>` | Selection down/up |
| `<?>` | Toggle preview window |
| `<A-w>` | Toggle preview wrap |
| `<A-j><A-k>` | Preview down/up |
### gcf
Interactive `git checkout <file>` selector
### gclean
Interactive `git clean` selector
### gi
@ -77,6 +59,21 @@ Interactive `.gitignore` generator
![screenshot](screenshot-gi.png)
## Default keybinds
| Keybind | Action |
| ---------- | --------------------- |
| `<Enter>` | Confirm |
| `<Tab>` | Toggle mark |
| `<C-k/p>` | Selection up |
| `<C-j/n>` | Selection down |
| `<A-k/p>` | Preview up |
| `<A-j/n>` | Preview down |
| `<A-w>` | Toggle preview wrap |
| `<?>` | Toggle preview window |
| `<C-r>` | Toggle selection |
| `<C-s>` | 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)

View file

@ -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'