refactor: extract _forgit_git_show from _forgit_show

This commit is contained in:
sandroid 2026-03-12 21:37:35 +01:00 committed by sandr01d
parent 41912c2907
commit b82812007c
2 changed files with 47 additions and 9 deletions

View file

@ -430,6 +430,19 @@ _forgit_show_enter() {
_forgit_show_view "$file" "$_forgit_fullscreen_context" "${commit}"
}
_forgit_git_show() {
local commit=$1
shift
_forgit_show_git_opts=()
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
# Add "^{commit}" suffix after the actual commit. This suppresses the tag information in case it is a tag.
# See: https://git-scm.com/docs/git-show#Documentation/git-show.txt-codegitshow-s--formatsv100commitcode
git show --pretty="" --name-status --diff-merges=first-parent "${_forgit_show_git_opts[@]}" "${commit}^{commit}" -- "$@" |
sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1] \2/' |
sed 's/ / -> /2' | expand -t 8
}
# git show viewer
_forgit_show() {
_forgit_inside_work_tree || return 1
@ -458,15 +471,7 @@ _forgit_show() {
$FORGIT_SHOW_FZF_OPTS
--prompt=\"${commit} > \"
"
_forgit_show_git_opts=()
_forgit_parse_array _forgit_show_git_opts "$FORGIT_SHOW_GIT_OPTS"
# Add "^{commit}" suffix after the actual commit. This suppresses the tag information in case it is a tag.
# See: https://git-scm.com/docs/git-show#Documentation/git-show.txt-codegitshow-s--formatsv100commitcode
git show --pretty="" --name-status --diff-merges=first-parent "${_forgit_show_git_opts[@]}" "${commit}^{commit}" \
-- "${files[@]}" |
sed -E 's/^([[:alnum:]]+)[[:space:]]+(.*)$/[\1] \2/' |
sed 's/ / -> /2' | expand -t 8 |
FZF_DEFAULT_OPTS="$opts" fzf
_forgit_git_show "$commit" "${files[@]}" | FZF_DEFAULT_OPTS="$opts" fzf
fzf_exit_code=$?
# exit successfully on 130 (ctrl-c/esc)
[[ $fzf_exit_code == 130 ]] && return 0

33
tests/show.test.sh Normal file
View file

@ -0,0 +1,33 @@
#!/usr/bin/env bash
function set_up_before_script() {
source bin/git-forgit
# ignore global git config files
export GIT_CONFIG_SYSTEM=/dev/null
export GIT_CONFIG_GLOBAL=/dev/null
# create a new git repository in a temp directory
cd "$(bashunit::temp_dir)" || return 1
git init --quiet
git config user.email "test@example.com"
git config user.name "Test User"
# create an initial commit so we have a valid repo
touch file.txt
touch file1.txt
git add .
git commit -qm "Initial commit"
}
function test_forgit_git_show() {
local output
output=$(_forgit_git_show HEAD 2>&1)
assert_same "[A] file.txt
[A] file1.txt" "$output"
}
function test_forgit_git_show_filter() {
local output
output=$(_forgit_git_show HEAD -- file.txt 2>&1)
assert_same "[A] file.txt" "$output"
}