git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] git-jump: make `diff` work with filenames containing spaces
@ 2025-08-08 17:42 Greg Hurrell via GitGitGadget
  2025-08-09 14:44 ` D. Ben Knoble
                   ` (2 more replies)
  0 siblings, 3 replies; 11+ messages in thread
From: Greg Hurrell via GitGitGadget @ 2025-08-08 17:42 UTC (permalink / raw)
  To: git; +Cc: Greg Hurrell, Greg Hurrell

From: Greg Hurrell <greg.hurrell@datadoghq.com>

In diff.c, we output a trailing "\t" at the end of any filename that
contains a space:

    case DIFF_SYMBOL_FILEPAIR_PLUS:
            meta = diff_get_color_opt(o, DIFF_METAINFO);
            reset = diff_get_color_opt(o, DIFF_RESET);
            fprintf(o->file, "%s%s+++ %s%s%s\n", diff_line_prefix(o), meta,
                    line, reset,
                    strchr(line, ' ') ? "\t" : "");
            break;

That is, for a file "foo.txt" we'll emit:

    +++ a/foo.txt

but for "foo bar.txt" we'll emit:

    +++ a/foo bar.txt\t

This in turn leads us to produce a quickfix format like this:

    foo bar.txt\t:1:1:contents

Because no "foo bar.txt\t" file actually exists on disk, opening it in
Vim will just land the user in an empty buffer.

This commit takes the simple approach of unconditionally stripping any
trailing tab. Consider the following three examples:

1. For file "foo bar", Git will emit "foo bar\t".
2. For file "foo\t", Git will emit "foo\t".
3. For file "foo bar\t", Git will emit "foo bar\t\t".

Before this commit, `git-jump` correctly handled only case "2".

After this commit, `git-jump` correctly handles cases "1" and "3". In
reality, "1" is the only case people are going to run into with any
regularity, and the other two are extreme edge cases.

The argument here is that stripping the "\t" unconditionally gives us a
minimal change, and it addresses the common case without bringing in
complexity for the uncommon ones. If anybody ever complains about case
"2" no longer working for them, we can do the more complicated thing and
only strip the "\t" if the filename contains a space.

Signed-off-by: Greg Hurrell <greg.hurrell@datadoghq.com>
---
    git-jump: make diff work with filenames containing spaces

Published-As: https://github.com/gitgitgadget/git/releases/tag/pr-1950%2Fwincent%2Fstrip-trailing-tab-v1
Fetch-It-Via: git fetch https://github.com/gitgitgadget/git pr-1950/wincent/strip-trailing-tab-v1
Pull-Request: https://github.com/gitgitgadget/git/pull/1950

 contrib/git-jump/git-jump | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
index 3f696759617..8d1d5d79a69 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -44,7 +44,7 @@ open_editor() {
 mode_diff() {
 	git diff --no-prefix --relative "$@" |
 	perl -ne '
-	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
+	if (m{^\+\+\+ (.*?)\t?$}) { $file = $1 eq "/dev/null" ? undef : $1; next }
 	defined($file) or next;
 	if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
 	defined($line) or next;

base-commit: 2c2ba49d55ff26c1082b8137b1ec5eeccb4337d1
-- 
gitgitgadget

^ permalink raw reply related	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2025-08-15 15:51 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-08 17:42 [PATCH] git-jump: make `diff` work with filenames containing spaces Greg Hurrell via GitGitGadget
2025-08-09 14:44 ` D. Ben Knoble
2025-08-10 10:09   ` Phillip Wood
2025-08-10 13:20     ` Phillip Wood
2025-08-14 23:14       ` Jeff King
2025-08-15 15:51         ` Phillip Wood
2025-08-10  0:03 ` Junio C Hamano
2025-08-11 11:55 ` [PATCH v2] " Greg Hurrell via GitGitGadget
2025-08-11 13:16   ` Phillip Wood
2025-08-11 21:05     ` D. Ben Knoble
2025-08-14 23:18   ` Jeff King

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).