diff --git a/conf.d/forgit.plugin.fish b/conf.d/forgit.plugin.fish index 4c848bc..dbf13a5 100644 --- a/conf.d/forgit.plugin.fish +++ b/conf.d/forgit.plugin.fish @@ -34,7 +34,7 @@ function forgit::log -d "git commit viewer" forgit::inside_work_tree || return 1 set files (echo $argv | sed -nE 's/.* -- (.*)/\1/p') - set cmd "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager" + set preview "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager" if test -n "$FORGIT_COPY_CMD" set copy_cmd $FORGIT_COPY_CMD @@ -45,41 +45,59 @@ function forgit::log -d "git commit viewer" set opts " $FORGIT_FZF_DEFAULT_OPTS +s +m --tiebreak=index - --bind=\"enter:execute($cmd |env LESS='-r' less)\" + --bind=\"enter:execute($preview |env LESS='-r' less)\" --bind=\"ctrl-y:execute-silent(echo {} |grep -Eo '[a-f0-9]+' | head -1 | tr -d '[:space:]' |$copy_cmd)\" + --preview=\"$preview\" $FORGIT_LOG_FZF_OPTS " + if test -n "$FORGIT_GLO_FORMAT" + set log_format "$FORGIT_GLO_FORMAT" + else + set log_format "$forgit_log_format" + end + if set -q FORGIT_LOG_GRAPH_ENABLE set graph "--graph" else set graph "" end - eval "git log $graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" | - env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" + eval "git log $graph --color=always --format='$log_format' $argv $forgit_emojify" | + env FZF_DEFAULT_OPTS="$opts" fzf end ## git diff viewer -function forgit::diff -d "git diff viewer" +function forgit::diff -d "git diff viewer" --argument-names arg1 arg2 forgit::inside_work_tree || return 1 - if count $argv > /dev/null - if git rev-parse "$argv[1]" > /dev/null 2>&1 - set commit "$argv[1]" && set files "$argv[2..]" + if test -n "$arg1" + # If this first arg is a commit hash + if git rev-parse "$arg1" > /dev/null 2>&1 + if git rev-parse "$arg2" > /dev/null 2>&1 + set commits "$arg1 $arg2" && set files "$argv[3..-1]" + else + set commits "$arg1" && set files "$argv[2..-1]" + end else set files "$argv" end end set repo (git rev-parse --show-toplevel) - set cmd "echo {} |sed 's/.*] //' | xargs -I% git diff --color=always $commit -- '$repo/%' | $forgit_diff_pager" + set preview "echo {} | sed 's/.*] *//' | sed 's/ -> / /' | xargs -I% git diff --color=always $commits -- '$repo/%' | $forgit_diff_pager" + + set opts " $FORGIT_FZF_DEFAULT_OPTS - +m -0 --bind=\"enter:execute($cmd |env LESS='-r' less)\" + +m -0 --bind=\"enter:execute($preview |env LESS='-r' less)\" + --preview=\"$preview\" $FORGIT_DIFF_FZF_OPTS " - eval "git diff --name-only $commit -- $files*| sed -E 's/^(.)[[:space:]]+(.*)\$/[\1] \2/'" | env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" + eval git diff --name-only $commit -- $files* | + sed -E 's/^(.)[[:space:]]+(.*)\$/[\1] \2/' | + sed 's/\t/ -> /2' | expand -t 8 | + env FZF_DEFAULT_OPTS="$opts" fzf end # git add selector @@ -110,12 +128,13 @@ function forgit::add -d "git add selector" set opts " $FORGIT_FZF_DEFAULT_OPTS -0 -m --nth 2..,.. + --preview=\"$preview\" $FORGIT_ADD_FZF_OPTS " set files (git -c color.status=always -c status.relativePaths=true status -su | grep -F -e "$changed" -e "$unmerged" -e "$untracked" | sed -E 's/^(..[^[:space:]]*)[[:space:]]+(.*)\$/[\1] \2/' | # deal with white spaces internal to fname - env FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | + env FZF_DEFAULT_OPTS="$opts" fzf | sh -c "$extract_file") # for rename case if test -n "$files" @@ -131,13 +150,14 @@ end ## git reset HEAD (unstage) selector function forgit::reset::head -d "git reset HEAD (unstage) selector" forgit::inside_work_tree || return 1 - set cmd "git diff --cached --color=always -- {} | $forgit_diff_pager" + set preview "git diff --cached --color=always -- {} | $forgit_diff_pager" set opts " $FORGIT_FZF_DEFAULT_OPTS -m -0 + --preview=\"$preview\" $FORGIT_RESET_HEAD_FZF_OPTS " - set files (git diff --cached --name-only --relative | env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd") + set files (git diff --cached --name-only --relative | env FZF_DEFAULT_OPTS="$opts" fzf) if test -n "$files" for file in $files echo $file | tr '\n' '\0' |xargs -I{} -0 git reset -q HEAD {} @@ -160,14 +180,15 @@ function forgit::checkout::file -d "git checkout-file selector" --argument-names end - set cmd "git diff --color=always -- {} | $forgit_diff_pager" + set preview "git diff --color=always -- {} | $forgit_diff_pager" set opts " $FORGIT_FZF_DEFAULT_OPTS -m -0 + --preview=\"$preview\" $FORGIT_CHECKOUT_FILE_FZF_OPTS " set git_rev_parse (git rev-parse --show-toplevel) - set files (git ls-files --modified "$git_rev_parse" | env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd") + set files (git ls-files --modified "$git_rev_parse" | env FZF_DEFAULT_OPTS="$opts" fzf) if test -n "$files" for file in $files @@ -196,11 +217,12 @@ function forgit::checkout::commit -d "git checkout commit selector" --argument-n end - set cmd "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % | $forgit_show_pager" + set preview "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % | $forgit_show_pager" set 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:]' | $copy_cmd)\" + --preview=\"$preview\" $FORGIT_COMMIT_FZF_OPTS " @@ -211,7 +233,7 @@ function forgit::checkout::commit -d "git checkout commit selector" --argument-n end 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 % -- + FZF_DEFAULT_OPTS="$opts" fzf | grep -Eo '[a-f0-9]+' | head -1 | xargs -I% git checkout % -- end @@ -225,16 +247,17 @@ function forgit::checkout::branch -d "git checkout branch selector" --argument-n return $checkout_status end - set cmd "git branch --color=always --verbose --all | sort -k1.1,1.1 -r" set preview "git log {1} --graph --pretty=format:'$forgit_log_format' --color=always --abbrev-commit --date=relative" set opts " $FORGIT_FZF_DEFAULT_OPTS +s +m --tiebreak=index --header-lines=1 + --preview=\"$preview\" $FORGIT_CHECKOUT_BRANCH_FZF_OPTS " - set branch (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | awk '{print $1}') + set cmd "git branch --color=always --verbose --all | sort -k1.1,1.1 -r" + set branch (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | awk '{print $1}') test -z "$branch" && return 1 @@ -247,13 +270,14 @@ end # git stash viewer function forgit::stash::show -d "git stash viewer" forgit::inside_work_tree || return 1 - set cmd "echo {} |cut -d: -f1 |xargs -I% git stash show --color=always --ext-diff % |$forgit_diff_pager" + set preview "echo {} |cut -d: -f1 |xargs -I% git stash show --color=always --ext-diff % |$forgit_diff_pager" set opts " $FORGIT_FZF_DEFAULT_OPTS - +s +m -0 --tiebreak=index --bind=\"enter:execute($cmd |env LESS='-r' less)\" + +s +m -0 --tiebreak=index --bind=\"enter:execute($preview |env LESS='-r' less)\" + --preview=\"$preview\" $FORGIT_STASH_FZF_OPTS " - git stash list | env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" + git stash list | env FZF_DEFAULT_OPTS="$opts" fzf end # git clean selector @@ -287,13 +311,14 @@ function forgit::cherry::pick -d "git cherry-picking" --argument-names 'target' end set preview "echo {1} | xargs -I% git show --color=always % | $forgit_show_pager" set opts " + --preview=\"$preview\" $FORGIT_FZF_DEFAULT_OPTS -m -0 " echo $base echo $target git cherry "$base" "$target" --abbrev -v | cut -d ' ' -f2- | - env FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | cut -d' ' -f1 | + env FZF_DEFAULT_OPTS="$opts" fzf | cut -d' ' -f1 | xargs -I% git cherry-pick % end @@ -307,7 +332,6 @@ function forgit::fixup -d "git fixup" set graph "" end - set cmd "git log $graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" set files (echo $argv | sed -nE 's/.* -- (.*)/\1/p') set preview "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager" @@ -321,10 +345,12 @@ function forgit::fixup -d "git fixup" $FORGIT_FZF_DEFAULT_OPTS +s +m --tiebreak=index --bind=\"ctrl-y:execute-silent(echo {} |grep -Eo '[a-f0-9]+' | head -1 | tr -d '[:space:]' |$copy_cmd)\" + --preview=\"$preview\" $FORGIT_FIXUP_FZF_OPTS " - set target_commit (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | grep -Eo '[a-f0-9]+' | head -1) + set cmd "git log $graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" + set target_commit (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf | grep -Eo '[a-f0-9]+' | head -1) if test -n "$target_commit" && git commit --fixup "$target_commit" # "$target_commit~" is invalid when the commit is the first commit, but we can use "--root" instead @@ -347,7 +373,7 @@ function forgit::rebase -d "git rebase" else set graph "" end - set cmd "git log $graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" + set preview "git log $graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" set files (echo $argv | sed -nE 's/.* -- (.*)/\1/p') set preview "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager" @@ -362,9 +388,10 @@ function forgit::rebase -d "git rebase" $FORGIT_FZF_DEFAULT_OPTS +s +m --tiebreak=index --bind=\"ctrl-y:execute-silent(echo {} |grep -Eo '[a-f0-9]+' | head -1 | tr -d '[:space:]' |$copy_cmd)\" + --preview=\"$preview\" $FORGIT_REBASE_FZF_OPTS " - set commit (eval "$cmd" | FZF_DEFAULT_OPTS="$opts" fzf --preview="$preview" | + set commit (eval "$preview" | FZF_DEFAULT_OPTS="$opts" fzf | grep -Eo '[a-f0-9]+' | head -1) if test $commit @@ -394,10 +421,11 @@ function forgit::ignore -d "git ignore generator" forgit::ignore::update end - set cmd "$forgit_ignore_pager $FORGIT_GI_TEMPLATES/{2}{,.gitignore} 2>/dev/null" + set preview "$forgit_ignore_pager $FORGIT_GI_TEMPLATES/{2}{,.gitignore} 2>/dev/null" set opts " $FORGIT_FZF_DEFAULT_OPTS -m --preview-window='right:70%' + --preview=\"$preview\" $FORGIT_IGNORE_FZF_OPTS " set IFS '\n' @@ -405,7 +433,7 @@ function forgit::ignore -d "git ignore generator" set args $argv if not count $argv > /dev/null set args (forgit::ignore::list | nl -nrn -w4 -s' ' | - env FZF_DEFAULT_OPTS="$opts" fzf --preview="$cmd" |awk '{print $2}') + env FZF_DEFAULT_OPTS="$opts" fzf |awk '{print $2}') end if not count $args > /dev/null @@ -415,6 +443,37 @@ function forgit::ignore -d "git ignore generator" forgit::ignore::get $args end +function forgit::revert::commit --argument-names 'commit_hash' + if test -n "$commit_hash" + git revert -- "$commit_hash" + set revert_status $status + git status --short + return $revert_status + end + + set preview "echo {} |grep -Eo '[a-f0-9]+' |head -1 |xargs -I% git show --color=always % -- $files | $forgit_show_pager" + set opts " + $FORGIT_FZF_DEFAULT_OPTS + +s --tiebreak=index + --preview=\"$preview\" + $FORGIT_REVERT_COMMIT_OPTS + " + + set files (echo $argv | sed -nE 's/.* -- (.*)/\1/p') # extract files parameters for `git show` command + + set cmd "git log --graph --color=always --format='$forgit_log_format' $argv $forgit_emojify" + + set commits (eval $cmd | + FZF_DEFAULT_OPTS="$opts" fzf -m | + string match -r "[a-f0-9]+") + + if test -z "$commits" + return 1 + end + + git revert $commits +end + function forgit::ignore::update if test -d "$FORGIT_GI_REPO_LOCAL" forgit::info 'Updating gitignore repo...' @@ -439,12 +498,13 @@ function forgit::ignore::get end function forgit::ignore::list - find "$FORGIT_GI_TEMPLATES" -print |sed -e 's#.gitignore$##' -e 's#.*/##' | sort -fu + find "$FORGIT_GI_TEMPLATES" -print | sed -e 's#.gitignore$##' -e 's#.*/##' | sort -fu end function forgit::ignore::clean - setopt localoptions rmstarsilent - [[ -d "$FORGIT_GI_REPO_LOCAL" ]] && rm -rf "$FORGIT_GI_REPO_LOCAL" + if test -d "$FORGIT_GI_REPO_LOCAL" + rm -rf "$FORGIT_GI_REPO_LOCAL" + end end set -g FORGIT_FZF_DEFAULT_OPTS " @@ -542,4 +602,10 @@ if test -z "$FORGIT_NO_ALIASES" alias gco 'forgit::checkout::commit' end + if test -n "$forgit_revert_commit" + alias $forgit_revert_commit 'forgit::revert::commit' + else + alias grc 'forgit::revert::commit' + end + end