Merge pull request #211 from sandr01d/gb

Feature: Added interactive git blame
This commit is contained in:
Wenxuan 2022-06-17 22:14:12 +08:00 committed by GitHub
commit 98a5cc27a1
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 36 additions and 7 deletions

View file

@ -111,6 +111,8 @@ Install `forgit` in just one click.
- **Interactive `git rebase -i` selector** (`grb`)
- **Interactive `git blame` selector** (`gbl`)
- **Interactive `git commit --fixup && git rebase -i --autosquash` selector** (`gfu`)
### ⌨ Keybinds
@ -158,6 +160,7 @@ forgit_clean=gclean
forgit_stash_show=gss
forgit_cherry_pick=gcp
forgit_rebase=grb
forgit_blame=gbl
forgit_fixup=gfu
```
@ -198,13 +201,14 @@ Forgit will use the default configured pager from git (`core.pager`,
`pager.show`, `pager.diff`) but can be altered with the following environment
variables:
| Use case | Option | Fallbacks to |
| ------------ | ------------------- | -------------------------------------------- |
| common pager | `FORGIT_PAGER` | `git config core.pager` _or_ `cat` |
| pager on `git show` | `FORGIT_SHOW_PAGER` | `git config pager.show` _or_ `$FORGIT_PAGER` |
| pager on `git diff` | `FORGIT_DIFF_PAGER` | `git config pager.diff` _or_ `$FORGIT_PAGER` |
| pager on `gitignore` | `FORGIT_IGNORE_PAGER` | `bat -l gitignore --color always` _or_ `cat` |
| git log format | `FORGIT_GLO_FORMAT` | `%C(auto)%h%d %s %C(black)%C(bold)%cr%reset` |
| Use case | Option | Fallbacks to |
| ------------ | ------------------- | --------------------------------------------- |
| common pager | `FORGIT_PAGER` | `git config core.pager` _or_ `cat` |
| pager on `git show` | `FORGIT_SHOW_PAGER` | `git config pager.show` _or_ `$FORGIT_PAGER` |
| pager on `git diff` | `FORGIT_DIFF_PAGER` | `git config pager.diff` _or_ `$FORGIT_PAGER` |
| pager on `git blame` | `FORGIT_BLAME_PAGER` | `git config pager.blame` _or_ `$FORGIT_PAGER` |
| pager on `gitignore` | `FORGIT_IGNORE_PAGER` | `bat -l gitignore --color always` _or_ `cat` |
| git log format | `FORGIT_GLO_FORMAT` | `%C(auto)%h%d %s %C(black)%C(bold)%cr%reset` |
#### fzf options
@ -239,6 +243,7 @@ Customizing fzf options for each command individually is also supported:
| `gss` | `FORGIT_STASH_FZF_OPTS` |
| `gclean` | `FORGIT_CLEAN_FZF_OPTS` |
| `grb` | `FORGIT_REBASE_FZF_OPTS` |
| `gbl` | `FORGIT_BLAME_FZF_OPTS` |
| `gfu` | `FORGIT_FIXUP_FZF_OPTS` |
Complete loading order of fzf options is:

View file

@ -11,6 +11,7 @@ forgit_pager=${FORGIT_PAGER:-$(git config core.pager || echo 'cat')}
forgit_show_pager=${FORGIT_SHOW_PAGER:-$(git config pager.show || echo "$forgit_pager")}
forgit_diff_pager=${FORGIT_DIFF_PAGER:-$(git config pager.diff || echo "$forgit_pager")}
forgit_ignore_pager=${FORGIT_IGNORE_PAGER:-$(hash bat &>/dev/null && echo 'bat -l gitignore --color=always' || echo 'cat')}
forgit_blame_pager=${FORGIT_BLAME_PAGER:-$(git config pager.blame || echo "$forgit_pager")}
forgit_log_format=${FORGIT_LOG_FORMAT:-%C(auto)%h%d %s %C(black)%C(bold)%cr%Creset}
@ -336,6 +337,28 @@ forgit::revert::commit() {
done
}
# git blame viewer
forgit::blame() {
forgit::inside_work_tree || return 1
[[ $# -ne 0 ]] && git blame "$@" && return 0
local opts flags preview file
opts="
$FORGIT_FZF_DEFAULT_OPTS
$FORGIT_BLAME_FZF_OPTS
"
flags=$(git rev-parse --flags "$@")
preview="
if (git ls-files {} --error-unmatch) &>/dev/null; then
git blame {} --date=short $flags | $forgit_blame_pager
else
echo File not tracked
fi
"
file=$(FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview")
[[ -z "$file" ]] && return 1
eval git blame "$file" "$flags"
}
# 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}"
@ -419,6 +442,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
alias "${forgit_cherry_pick:-gcp}"='forgit::cherry::pick'
alias "${forgit_rebase:-grb}"='forgit::rebase'
alias "${forgit_fixup:-gfu}"='forgit::fixup'
alias "${forgit_blame:-gbl}"='forgit::blame'
fi
# set installation path (for use by `bin/git-forgit`)