From 8f0f134ca39ab52b9a46b41e0118ad611babdc06 Mon Sep 17 00:00:00 2001 From: Patrick Fong Date: Tue, 19 Jul 2022 15:03:07 -0700 Subject: [PATCH] [Search git status] rewrite parsing logic without regex and fix bug with 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. --- functions/_fzf_preview_changed_file.fish | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-) diff --git a/functions/_fzf_preview_changed_file.fish b/functions/_fzf_preview_changed_file.fish index b9cdb122..0b3d23c6 100644 --- a/functions/_fzf_preview_changed_file.fish +++ b/functions/_fzf_preview_changed_file.fish @@ -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