Skip to content

Commit

Permalink
[Search git status] rewrite parsing logic without regex and fix bug w…
Browse files Browse the repository at this point in the history
…ith renames

There was a bug in the status parsing logic in which the unstaged output header would be printed if a file was renamed in the index but had no unstaged changes. I decided the regex code was too hard to read and unwieldy--hence how the bug survived code review--so rewrote it using simple, self-documenting substring checks while fixing the bug. This should also marginally improve performance.
  • Loading branch information
PatrickF1 committed Jul 19, 2022
1 parent a79dc75 commit 8f0f134
Showing 1 changed file with 12 additions and 6 deletions.
18 changes: 12 additions & 6 deletions functions/_fzf_preview_changed_file.fish
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,23 @@
# MM functions/_fzf_preview_changed_file.fish
function _fzf_preview_changed_file
set -l path (string split ' ' $argv)[-1]
if string match -r '^\?\?' $argv --quiet
# first letter of short format shows index, second letter shows working tree
# https://git-scm.com/docs/git-status/2.35.0#_output
set -l index_status (string sub --length 1 $argv)
set -l working_tree_status (string sub --start 2 --length 1 $argv)

if test $index_status = '?'
echo -e (set_color --underline)=== Untracked ===\n
_fzf_preview_file $path
else
if string match -r '\S\s\S' $argv --quiet
echo -e (set_color --underline red)=== Unstaged ===\n
git diff --color=always -- $path
end
if string match -r '^\S' $argv --quiet
if test $index_status != ' '
echo -e (set_color --underline green)=== Staged ===\n
git diff --color=always --staged -- $path
end

if test $working_tree_status != ' '
echo -e (set_color --underline red)=== Unstaged ===\n
git diff --color=always -- $path
end
end
end

0 comments on commit 8f0f134

Please sign in to comment.