Skip to content

Commit

Permalink
Add forgit show command
Browse files Browse the repository at this point in the history
  • Loading branch information
carlfriedrich committed Dec 29, 2024
1 parent 755b3dd commit c06d9f9
Showing 1 changed file with 63 additions and 0 deletions.
63 changes: 63 additions & 0 deletions bin/git-forgit
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,65 @@ _forgit_diff() {
return $fzf_exit_code
}

_forgit_exec_show() {
_forgit_show_git_opts=()
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
git show --pretty="" --diff-merges=first-parent --color=always "${_forgit_show_git_opts[@]}" "$@"
}

_forgit_show_view() {
local input_line=$1
local diff_context=$2
local commit=$3
local repo
repo=$(git rev-parse --show-toplevel)
cd "$repo" || return 1
echo "$input_line" | _forgit_get_files_from_diff_line | xargs -0 \
"$FORGIT" exec_show "${commit}" -U"$diff_context" -- | _forgit_pager diff
}

_forgit_show_enter() {
file=$1
commit=$2
_forgit_show_view "$file" "$_forgit_fullscreen_context" "${commit}"
}

# git show viewer
_forgit_show() {
_forgit_inside_work_tree || return 1
local files opts commit escaped_commit
files=()
if [[ $# -ne 0 ]]; then
if git rev-parse "$1" -- &>/dev/null ; then
commit="$1" && files=("${@:2}")
else
commit="HEAD" && files=("$@")
fi
else
commit="HEAD"
fi
# Escape opening brackets to support stashes (see comment in _forgit_diff)
escaped_commit=${commit//\{/\\\\\{}
opts="
$FORGIT_FZF_DEFAULT_OPTS
+m -0 --bind=\"enter:execute($FORGIT show_enter {} $escaped_commit | $FORGIT pager enter)\"
--preview=\"$FORGIT show_view {} '$_forgit_preview_context' $escaped_commit\"
--bind=\"alt-e:execute-silent($FORGIT edit_diffed_file {})+refresh-preview\"
$FORGIT_DIFF_FZF_OPTS
--prompt=\"${commit} > \"
"
_forgit_show_git_opts=()
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
git show --pretty="" --name-status --diff-merges=first-parent "${_forgit_show_git_opts[@]}" "${commit}" -- "${files[@]}" |
sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1] \2/' |
sed 's/ / -> /2' | expand -t 8 |
FZF_DEFAULT_OPTS="$opts" fzf
fzf_exit_code=$?
# exit successfully on 130 (ctrl-c/esc)
[[ $fzf_exit_code == 130 ]] && return 0
return $fzf_exit_code
}

_forgit_add_preview() {
file=$(echo "$1" | _forgit_get_single_file_from_add_line)
if (git status -s -- "$file" | grep '^??') &>/dev/null; then # diff with /dev/null for untracked files
Expand Down Expand Up @@ -1021,6 +1080,7 @@ public_commands=(
"rebase"
"reset_head"
"revert_commit"
"show"
"stash_show"
"stash_push"
)
Expand All @@ -1035,10 +1095,13 @@ private_commands=(
"cherry_pick_preview"
"clean_preview"
"diff_enter"
"exec_show"
"file_preview"
"ignore_preview"
"revert_preview"
"reset_head_preview"
"show_enter"
"show_view"
"stash_push_preview"
"stash_show_preview"
"yank_sha"
Expand Down

0 comments on commit c06d9f9

Please sign in to comment.