Initial commit

main
Junegunn Choi 2 years ago
parent 4bc0323b48
commit 9867a4ac3c
No known key found for this signature in database
GPG Key ID: 254BC280FEF9C627

@ -1,16 +1,35 @@
fzf-git.sh fzf-git.sh
========== ==========
bash and zsh key bindings for Git objects, powered by fzf. bash and zsh key bindings for Git objects, powered by [fzf][fzf].
[fzf]: https://github.com/junegunn/fzf
Installation Installation
------------ ------------
Source [fzf-git.sh](fzf-git.sh) file in from your .bashrc or .zshrc. 1. Install the latest version of [fzf][fzf]
2. Source [fzf-git.sh](fzf-git.sh) file from your .bashrc or .zshrc.
Usage
-----
* <kbd>CTRL-G</kbd><kbd>CTRL-F</kbd> for **F**iles
* <kbd>CTRL-G</kbd><kbd>CTRL-B</kbd> for **B**ranches
* <kbd>CTRL-G</kbd><kbd>CTRL-T</kbd> for **T**ags
* <kbd>CTRL-G</kbd><kbd>CTRL-R</kbd> for **R**emotes
* <kbd>CTRL-G</kbd><kbd>CTRL-H</kbd> for commit **H**ashes
* <kbd>CTRL-G</kbd><kbd>CTRL-S</kbd> for **S**tashes
Customization
-------------
* <kbd>CTRL-G</kbd><kbd>CTRL-F</kbd> for files ```sh
* <kbd>CTRL-G</kbd><kbd>CTRL-B</kbd> for branches # Redefine this function to change the options
* <kbd>CTRL-G</kbd><kbd>CTRL-T</kbd> for tags _fzf_git_fzf() {
* <kbd>CTRL-G</kbd><kbd>CTRL-R</kbd> for remotes fzf-tmux -p80%,60% -- \
* <kbd>CTRL-G</kbd><kbd>CTRL-H</kbd> for commit hashes --layout=reverse --multi --height=50% --min-height=20 --border \
* <kbd>CTRL-G</kbd><kbd>CTRL-S</kbd> for stashes --preview-window='right,50%,border-left' \
--bind='ctrl-/:change-preview-window(down,50%,border-top|hidden|)' "$@"
}
```

@ -25,19 +25,28 @@ if [[ $- =~ i ]]; then
# Redefine this function to change the options # Redefine this function to change the options
_fzf_git_fzf() { _fzf_git_fzf() {
fzf-tmux -p80%,60% --layout=reverse --multi --height 50% --min-height 20 --border --bind ctrl-/:toggle-preview "$@" fzf-tmux -p80%,60% -- \
--layout=reverse --multi --height=50% --min-height=20 --border \
--preview-window='right,50%,border-left' \
--bind='ctrl-/:change-preview-window(down,50%,border-top|hidden|)' "$@"
} }
_fzf_git_check() { _fzf_git_check() {
git rev-parse HEAD > /dev/null 2>&1 git rev-parse HEAD > /dev/null 2>&1 && return
[[ -n $TMUX ]] && tmux display-message "Not in a git repository"
return 1
} }
# Sometimes bat is installed as batcat if [[ -z $_fzf_git_cat ]]; then
export _fzf_git_cat=cat # Sometimes bat is installed as batcat
if command -v batcat > /dev/null; then export _fzf_git_cat="cat"
_fzf_git_cat="batcat --style='${BAT_STYLE:-numbers}' --color=always --pager=never" _fzf_git_bat_options="--style='${BAT_STYLE:-full}' --color=always --pager=never"
elif command -v bat > /dev/null; then if command -v batcat > /dev/null; then
_fzf_git_cat="bat --style='${BAT_STYLE:-numbers}' --color=always --pager=never" _fzf_git_cat="batcat $_fzf_git_bat_options"
elif command -v bat > /dev/null; then
_fzf_git_cat="bat $_fzf_git_bat_options"
fi
fi fi
_fzf_git_files() { _fzf_git_files() {
@ -45,7 +54,7 @@ _fzf_git_files() {
(git -c color.status=always status --short (git -c color.status=always status --short
git ls-files | grep -vf <(git status -s | grep '^[^?]' | cut -c4-) | sed 's/^/ /') | git ls-files | grep -vf <(git status -s | grep '^[^?]' | cut -c4-) | sed 's/^/ /') |
_fzf_git_fzf -m --ansi --nth 2..,.. \ _fzf_git_fzf -m --ansi --nth 2..,.. \
--prompt 'Git Files> ' \ --prompt '📁 Files> ' \
--preview "git diff --color=always -- {-1} | sed 1,4d; $_fzf_git_cat {-1}" | --preview "git diff --color=always -- {-1} | sed 1,4d; $_fzf_git_cat {-1}" |
cut -c4- | sed 's/.* -> //' cut -c4- | sed 's/.* -> //'
} }
@ -54,7 +63,7 @@ _fzf_git_branches() {
_fzf_git_check || return _fzf_git_check || return
git branch -a --color=always | grep -v '/HEAD\s' | sort | git branch -a --color=always | grep -v '/HEAD\s' | sort |
_fzf_git_fzf --ansi --tac --preview-window right,70% \ _fzf_git_fzf --ansi --tac --preview-window right,70% \
--prompt 'Git Branches> ' \ --prompt '🌵 Branches> ' \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1)' | --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" $(sed s/^..// <<< {} | cut -d" " -f1)' |
sed 's/^..//' | cut -d' ' -f1 | sed 's/^..//' | cut -d' ' -f1 |
sed 's#^remotes/##' sed 's#^remotes/##'
@ -64,7 +73,7 @@ _fzf_git_tags() {
_fzf_git_check || return _fzf_git_check || return
git tag --sort -version:refname | git tag --sort -version:refname |
_fzf_git_fzf --preview-window right,70% \ _fzf_git_fzf --preview-window right,70% \
--prompt 'Git Tags> ' \ --prompt '📛 Tags> ' \
--preview 'git show --color=always {}' --preview 'git show --color=always {}'
} }
@ -72,7 +81,7 @@ _fzf_git_hashes() {
_fzf_git_check || return _fzf_git_check || return
git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always | git log --date=short --format="%C(green)%C(bold)%cd %C(auto)%h%d %s (%an)" --graph --color=always |
_fzf_git_fzf --ansi --no-sort --bind 'ctrl-s:toggle-sort' \ _fzf_git_fzf --ansi --no-sort --bind 'ctrl-s:toggle-sort' \
--prompt 'Git Hashes> ' \ --prompt '🍡 Hashes> ' \
--header 'Press CTRL-S to toggle sort' \ --header 'Press CTRL-S to toggle sort' \
--preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always' | --preview 'grep -o "[a-f0-9]\{7,\}" <<< {} | xargs git show --color=always' |
grep -o "[a-f0-9]\{7,\}" grep -o "[a-f0-9]\{7,\}"
@ -82,7 +91,8 @@ _fzf_git_remotes() {
_fzf_git_check || return _fzf_git_check || return
git remote -v | awk '{print $1 "\t" $2}' | uniq | git remote -v | awk '{print $1 "\t" $2}' | uniq |
_fzf_git_fzf --tac \ _fzf_git_fzf --tac \
--prompt 'Git Remotes> ' \ --prompt '📡 Remotes> ' \
--preview-window right,70% \
--preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {1}/"$(git rev-parse --abbrev-ref HEAD)"' | --preview 'git log --oneline --graph --date=short --color=always --pretty="format:%C(auto)%cd %h%d %s" {1}/"$(git rev-parse --abbrev-ref HEAD)"' |
cut -d$'\t' -f1 cut -d$'\t' -f1
} }
@ -90,7 +100,7 @@ _fzf_git_remotes() {
_fzf_git_stashes() { _fzf_git_stashes() {
_fzf_git_check || return _fzf_git_check || return
git stash list | _fzf_git_fzf \ git stash list | _fzf_git_fzf \
--prompt 'Git Stashes> ' \ --prompt '🥡 Stashes> ' \
-d: --preview 'git show --color=always {1}' | -d: --preview 'git show --color=always {1}' |
cut -d: -f1 cut -d: -f1
} }

Loading…
Cancel
Save