From c06d9f9c944a3dfed5352404cc8e8fea6b5359a8 Mon Sep 17 00:00:00 2001 From: carlfriedrich Date: Sun, 29 Dec 2024 13:13:01 +0100 Subject: [PATCH] Add forgit show command --- bin/git-forgit | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 63 insertions(+) diff --git a/bin/git-forgit b/bin/git-forgit index 4aebe59a..0dabad6f 100755 --- a/bin/git-forgit +++ b/bin/git-forgit @@ -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 @@ -1021,6 +1080,7 @@ public_commands=( "rebase" "reset_head" "revert_commit" + "show" "stash_show" "stash_push" ) @@ -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"