Skip to content

Commit 01deb9a

Browse files
committed
Feature: add forgit show command
Use the new command in git log enter. This fixes the display of diffs for merge commits. Fixes #416.
1 parent 755b3dd commit 01deb9a

File tree

5 files changed

+80
-1
lines changed

5 files changed

+80
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,8 @@ Then add the following to your shell's config file:
101101

102102
- **Interactive `git diff` viewer** (`gd`)
103103

104+
- **Interactive `git show` viewer** (`gs`)
105+
104106
- **Interactive `git reset HEAD <file>` selector** (`grh`)
105107

106108
- **Interactive `git checkout <file>` selector** (`gcf`)
@@ -172,6 +174,7 @@ You can change the default aliases by defining these variables below.
172174
forgit_log=glo
173175
forgit_reflog=grl
174176
forgit_diff=gd
177+
forgit_show=gs
175178
forgit_add=ga
176179
forgit_reset_head=grh
177180
forgit_ignore=gi
@@ -232,6 +235,7 @@ These are passed to the according `git` calls.
232235
| `glo` | `FORGIT_LOG_GIT_OPTS` |
233236
| `grl` | `FORGIT_REFLOG_GIT_OPTS` |
234237
| `gd` | `FORGIT_DIFF_GIT_OPTS` |
238+
| `gs` | `FORGIT_SHOW_GIT_OPTS` |
235239
| `grh` | `FORGIT_RESET_HEAD_GIT_OPTS` |
236240
| `gcf` | `FORGIT_CHECKOUT_FILE_GIT_OPTS` |
237241
| `gcb` | `FORGIT_CHECKOUT_BRANCH_GIT_OPTS`, `FORGIT_CHECKOUT_BRANCH_BRANCH_GIT_OPTS` |
@@ -286,6 +290,7 @@ Customizing fzf options for each command individually is also supported:
286290
| `grl` | `FORGIT_REFLOG_FZF_OPTS` |
287291
| `gi` | `FORGIT_IGNORE_FZF_OPTS` |
288292
| `gd` | `FORGIT_DIFF_FZF_OPTS` |
293+
| `gs` | `FORGIT_SHOW_FZF_OPTS` |
289294
| `grh` | `FORGIT_RESET_HEAD_FZF_OPTS` |
290295
| `gcf` | `FORGIT_CHECKOUT_FILE_FZF_OPTS` |
291296
| `gcb` | `FORGIT_CHECKOUT_BRANCH_FZF_OPTS` |

bin/git-forgit

Lines changed: 64 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ _forgit_log_enter() {
185185
local sha
186186
sha=$(echo "$1" | _forgit_extract_sha)
187187
shift
188-
echo "$sha" | xargs -I% "${FORGIT}" diff %^! "$@"
188+
echo "$sha" | xargs -I% "${FORGIT}" show % "$@"
189189
}
190190

191191
# git commit viewer
@@ -339,6 +339,65 @@ _forgit_diff() {
339339
return $fzf_exit_code
340340
}
341341

342+
_forgit_exec_show() {
343+
_forgit_show_git_opts=()
344+
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
345+
git show --pretty="" --diff-merges=first-parent --color=always "${_forgit_show_git_opts[@]}" "$@"
346+
}
347+
348+
_forgit_show_view() {
349+
local input_line=$1
350+
local diff_context=$2
351+
local commit=$3
352+
local repo
353+
repo=$(git rev-parse --show-toplevel)
354+
cd "$repo" || return 1
355+
echo "$input_line" | _forgit_get_files_from_diff_line | xargs -0 \
356+
"$FORGIT" exec_show "${commit}" -U"$diff_context" -- | _forgit_pager diff
357+
}
358+
359+
_forgit_show_enter() {
360+
file=$1
361+
commit=$2
362+
_forgit_show_view "$file" "$_forgit_fullscreen_context" "${commit}"
363+
}
364+
365+
# git show viewer
366+
_forgit_show() {
367+
_forgit_inside_work_tree || return 1
368+
local files opts commit escaped_commit
369+
files=()
370+
if [[ $# -ne 0 ]]; then
371+
if git rev-parse "$1" -- &>/dev/null ; then
372+
commit="$1" && files=("${@:2}")
373+
else
374+
commit="HEAD" && files=("$@")
375+
fi
376+
else
377+
commit="HEAD"
378+
fi
379+
# Escape opening brackets to support stashes (see comment in _forgit_diff)
380+
escaped_commit=${commit//\{/\\\\\{}
381+
opts="
382+
$FORGIT_FZF_DEFAULT_OPTS
383+
+m -0 --bind=\"enter:execute($FORGIT show_enter {} $escaped_commit | $FORGIT pager enter)\"
384+
--preview=\"$FORGIT show_view {} '$_forgit_preview_context' $escaped_commit\"
385+
--bind=\"alt-e:execute-silent($FORGIT edit_diffed_file {})+refresh-preview\"
386+
$FORGIT_DIFF_FZF_OPTS
387+
--prompt=\"${commit} > \"
388+
"
389+
_forgit_show_git_opts=()
390+
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
391+
git show --pretty="" --name-status --diff-merges=first-parent "${_forgit_show_git_opts[@]}" "${commit}" -- "${files[@]}" |
392+
sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1] \2/' |
393+
sed 's/ / -> /2' | expand -t 8 |
394+
FZF_DEFAULT_OPTS="$opts" fzf
395+
fzf_exit_code=$?
396+
# exit successfully on 130 (ctrl-c/esc)
397+
[[ $fzf_exit_code == 130 ]] && return 0
398+
return $fzf_exit_code
399+
}
400+
342401
_forgit_add_preview() {
343402
file=$(echo "$1" | _forgit_get_single_file_from_add_line)
344403
if (git status -s -- "$file" | grep '^??') &>/dev/null; then # diff with /dev/null for untracked files
@@ -1021,6 +1080,7 @@ public_commands=(
10211080
"rebase"
10221081
"reset_head"
10231082
"revert_commit"
1083+
"show"
10241084
"stash_show"
10251085
"stash_push"
10261086
)
@@ -1035,10 +1095,13 @@ private_commands=(
10351095
"cherry_pick_preview"
10361096
"clean_preview"
10371097
"diff_enter"
1098+
"exec_show"
10381099
"file_preview"
10391100
"ignore_preview"
10401101
"revert_preview"
10411102
"reset_head_preview"
1103+
"show_enter"
1104+
"show_view"
10421105
"stash_push_preview"
10431106
"stash_show_preview"
10441107
"yank_sha"

completions/git-forgit.bash

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,7 @@ _git_forgit()
7575
rebase
7676
reset_head
7777
revert_commit
78+
show
7879
stash_show
7980
stash_push
8081
"
@@ -101,6 +102,7 @@ _git_forgit()
101102
rebase) _git_rebase ;;
102103
reset_head) _git_reset ;;
103104
revert_commit) _git_revert ;;
105+
show) _git_show ;;
104106
stash_show) _git_stash_show ;;
105107
esac
106108
;;
@@ -135,6 +137,7 @@ then
135137
__git_complete forgit::rebase _git_rebase
136138
__git_complete forgit::reset::head _git_reset
137139
__git_complete forgit::revert::commit _git_revert
140+
__git_complete forgit::show _git_show
138141
__git_complete forgit::stash::show _git_stash_show
139142

140143
# Completion for forgit plugin shell aliases
@@ -154,6 +157,7 @@ then
154157
__git_complete "${forgit_rebase}" _git_rebase
155158
__git_complete "${forgit_reset_head}" _git_reset
156159
__git_complete "${forgit_revert_commit}" _git_revert
160+
__git_complete "${forgit_show}" _git_show
157161
__git_complete "${forgit_stash_show}" _git_stash_show
158162
fi
159163
fi

completions/git-forgit.fish

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ complete -c git-forgit -n __fish_forgit_needs_subcommand -a reflog -d 'git reflo
4040
complete -c git-forgit -n __fish_forgit_needs_subcommand -a rebase -d 'git rebase'
4141
complete -c git-forgit -n __fish_forgit_needs_subcommand -a reset_head -d 'git reset HEAD (unstage) selector'
4242
complete -c git-forgit -n __fish_forgit_needs_subcommand -a revert_commit -d 'git revert commit selector'
43+
complete -c git-forgit -n __fish_forgit_needs_subcommand -a show -d 'git show viewer'
4344
complete -c git-forgit -n __fish_forgit_needs_subcommand -a stash_show -d 'git stash viewer'
4445
complete -c git-forgit -n __fish_forgit_needs_subcommand -a stash_push -d 'git stash push selector'
4546

forgit.plugin.zsh

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,10 @@ forgit::diff() {
5353
"$FORGIT" diff "$@"
5454
}
5555

56+
forgit::show() {
57+
"$FORGIT" show "$@"
58+
}
59+
5660
forgit::add() {
5761
"$FORGIT" add "$@"
5862
}
@@ -146,6 +150,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
146150
export forgit_log="${forgit_log:-glo}"
147151
export forgit_reflog="${forgit_reflog:-grl}"
148152
export forgit_diff="${forgit_diff:-gd}"
153+
export forgit_show="${forgit_show:-gs}"
149154
export forgit_ignore="${forgit_ignore:-gi}"
150155
export forgit_checkout_file="${forgit_checkout_file:-gcf}"
151156
export forgit_checkout_branch="${forgit_checkout_branch:-gcb}"
@@ -166,6 +171,7 @@ if [[ -z "$FORGIT_NO_ALIASES" ]]; then
166171
alias "${forgit_log}"='forgit::log'
167172
alias "${forgit_reflog}"='forgit::reflog'
168173
alias "${forgit_diff}"='forgit::diff'
174+
alias "${forgit_show}"='forgit::show'
169175
alias "${forgit_ignore}"='forgit::ignore'
170176
alias "${forgit_checkout_file}"='forgit::checkout::file'
171177
alias "${forgit_checkout_branch}"='forgit::checkout::branch'

0 commit comments

Comments
 (0)