git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/2] a few git-jump quality-of-life fixes
@ 2024-09-15 11:11 Jeff King
  2024-09-15 11:18 ` [PATCH 1/2] git-jump: always specify column 1 for diff entries Jeff King
  2024-09-15 11:20 ` [PATCH 2/2] git-jump: ignore deleted files in diff mode Jeff King
  0 siblings, 2 replies; 10+ messages in thread
From: Jeff King @ 2024-09-15 11:11 UTC (permalink / raw)
  To: git

Here are fixes for some small corner-case annoyances I found using
"git-jump diff".

  [1/2]: git-jump: always specify column 1 for diff entries
  [2/2]: git-jump: ignore deleted files in diff mode

 contrib/git-jump/git-jump | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

-Peff

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

* [PATCH 1/2] git-jump: always specify column 1 for diff entries
  2024-09-15 11:11 [PATCH 0/2] a few git-jump quality-of-life fixes Jeff King
@ 2024-09-15 11:18 ` Jeff King
  2024-09-16 20:13   ` Martin Ågren
  2024-09-15 11:20 ` [PATCH 2/2] git-jump: ignore deleted files in diff mode Jeff King
  1 sibling, 1 reply; 10+ messages in thread
From: Jeff King @ 2024-09-15 11:18 UTC (permalink / raw)
  To: git

When we generate a quickfix entry for a diff hunk, we provide just the
filename and line number along with the content, like:

  file:1: contents of the line

This can be a problem if the line itself looks like a quickfix header.
For example (and this is adapted from a real-world case that bit me):

  echo 'static_lease 10:11:12:13:14:15:16 10.0.0.1' >file
  git add file
  echo change >file

produces:

  file:1: static_lease 10:11:12:13:14:15:16 10.0.0.1

which is ambiguous. It could be line 1 of "file", or line 11 of the file
"file:1: static_lease 10", and so on. In the case of vim's default
config, it seems to prefer the latter (you can configure "errorformat"
with a variety of patterns, but out of the box it matches some common
ones).

One easy way to fix this is to provide a column number, like:

  file:1:1: static_lease 10:11:12:13:14:15:16 10.0.0.1

which causes vim to prefer line 1 of "file" again (due to the preference
order of the various patterns in the default errorformat).

There are other options. For example, at least in my version of vim,
wrapping the file in quotation marks like:

  "file":1: static_lease 10:11:12:13:14:15:16 10.0.0.1

also works. That perhaps would the right thing even if you had the silly
file name "file:1:1: foo 10". But it's not clear what would happen if
you had a filename with quotes in it.

This feature is inherently scraping text, and there's bound to be some
ambiguities. I don't think it's worth worrying too much about unlikely
filenames, as its the file content that is more likely to introduce
unexpected characters.

So let's just go with the extra ":1" column specifier. We know this is
supported everywhere, as git-jump's "grep" mode already uses it (and
thus doesn't exhibit the same problem).

The "merge" mode is mostly immune to this, as it only matches "<<<<<<<"
conflict marker lines. It's possible of course to have a marker that
says "foo 10:11" later in the line, but in practice these will only have
branches and perhaps file names, so it's probably not worth worrying
about (and fixing it would involve passing --column to the system grep,
which may not be portable).

I also gave some thought as to whether we could put something more
useful than "1" in the column field for diffs. In theory we could find
the first changed character of the line, but this is tricky in practice.
You'd have to correlate before/after lines of the hunk to decide what
changed. So:

  -this is a foo line
  +this is a bar line

is easy (column 11). But:

  -this is a foo line
  +another line
  +this is a bar line

is harder.

This commit certainly doesn't preclude trying to do something more
clever later, but it's a much deeper rabbit hole than just fixing the
syntactic ambiguity.

Signed-off-by: Jeff King <peff@peff.net>
---
 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 47e0c557e6..78e7394406 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -50,7 +50,7 @@ mode_diff() {
 	defined($line) or next;
 	if (/^ /) { $line++; next }
 	if (/^[-+]\s*(.*)/) {
-		print "$file:$line: $1\n";
+		print "$file:$line:1: $1\n";
 		$line = undef;
 	}
 	'
-- 
2.46.1.893.gc4b01a7614


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

* [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-15 11:11 [PATCH 0/2] a few git-jump quality-of-life fixes Jeff King
  2024-09-15 11:18 ` [PATCH 1/2] git-jump: always specify column 1 for diff entries Jeff King
@ 2024-09-15 11:20 ` Jeff King
  2024-09-16 20:14   ` Martin Ågren
  2024-09-17  8:58   ` Taylor Blau
  1 sibling, 2 replies; 10+ messages in thread
From: Jeff King @ 2024-09-15 11:20 UTC (permalink / raw)
  To: git

If you do something like this:

  rm file_a
  echo change >file_b
  git jump diff

then we'll generate two quickfix entries for the diff, one for each
file. But the one for the deleted file is rather pointless. There's no
content to show since the file is gone, and in fact we open the editor
with the path /dev/null!

In vim, at least, the result is a confusing annoyance: the editor opens
with an empty buffer, and you have to skip past it to the useful
quickfix entry (after scratching your head and figuring out that no,
nothing is broken).

Let's skip such entries entirely. There's nothing useful to show, since
the point is that the file has been deleted.

It is possible that you could be doing a diff whose post-image is not
the working tree, and then you'd perhaps be jumping to the deleted
content (or at least something that was in the same spot). But I don't
think it's worth worrying about that case. For one thing, using git-jump
for such diffs is a bad idea in general, as it's going to sometimes move
you to the wrong spot. And two, a deletion is always going to have one
hunk starting at line 1, which is not that interesting to jump to in the
first place.

Signed-off-by: Jeff King <peff@peff.net>
---
 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 78e7394406..3f69675961 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; next }
+	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
 	defined($file) or next;
 	if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
 	defined($line) or next;
-- 
2.46.1.893.gc4b01a7614

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

* Re: [PATCH 1/2] git-jump: always specify column 1 for diff entries
  2024-09-15 11:18 ` [PATCH 1/2] git-jump: always specify column 1 for diff entries Jeff King
@ 2024-09-16 20:13   ` Martin Ågren
  0 siblings, 0 replies; 10+ messages in thread
From: Martin Ågren @ 2024-09-16 20:13 UTC (permalink / raw)
  To: Jeff King; +Cc: git

On Sun, 15 Sept 2024 at 13:18, Jeff King <peff@peff.net> wrote:
>
> When we generate a quickfix entry for a diff hunk, we provide just the
> filename and line number along with the content, like:
>
>   file:1: contents of the line
>
> This can be a problem if the line itself looks like a quickfix header.
> For example (and this is adapted from a real-world case that bit me):
>
>   echo 'static_lease 10:11:12:13:14:15:16 10.0.0.1' >file
>   git add file
>   echo change >file
>
> produces:
>
>   file:1: static_lease 10:11:12:13:14:15:16 10.0.0.1
>
> which is ambiguous. It could be line 1 of "file", or line 11 of the file
> "file:1: static_lease 10", and so on. In the case of vim's default
> config, it seems to prefer the latter (you can configure "errorformat"
> with a variety of patterns, but out of the box it matches some common
> ones).

I've never hit this, but it doesn't look too crazy. A couple of digits
and a colon and things begin to match. Ok.

> One easy way to fix this is to provide a column number, like:
>
>   file:1:1: static_lease 10:11:12:13:14:15:16 10.0.0.1
>
> which causes vim to prefer line 1 of "file" again (due to the preference
> order of the various patterns in the default errorformat).

Makes sense.

> There are other options. For example, at least in my version of vim,
> wrapping the file in quotation marks like:
>
>   "file":1: static_lease 10:11:12:13:14:15:16 10.0.0.1
>
> also works. That perhaps would the right thing even if you had the silly
> file name "file:1:1: foo 10". But it's not clear what would happen if
> you had a filename with quotes in it.

Right. Looking around, I can find someone asking the Internet how to
escape the filename and not getting any response.

> This feature is inherently scraping text, and there's bound to be some
> ambiguities. I don't think it's worth worrying too much about unlikely
> filenames, as its the file content that is more likely to introduce
> unexpected characters.

Agreed. (s/its/it's/)

> So let's just go with the extra ":1" column specifier. We know this is
> supported everywhere, as git-jump's "grep" mode already uses it (and
> thus doesn't exhibit the same problem).
>
> The "merge" mode is mostly immune to this, as it only matches "<<<<<<<"
> conflict marker lines. It's possible of course to have a marker that
> says "foo 10:11" later in the line, but in practice these will only have
> branches and perhaps file names, so it's probably not worth worrying
> about (and fixing it would involve passing --column to the system grep,
> which may not be portable).

I suppose we could use `git grep --no-index` instead of `grep` for `git
jump merge`. Anyway, that's out of scope here.

> I also gave some thought as to whether we could put something more
> useful than "1" in the column field for diffs. In theory we could find

Heh. Yes, in theory everything is possible. Your approach makes sense.

> -               print "$file:$line: $1\n";
> +               print "$file:$line:1: $1\n";

Looks good to me and from my testing, this fixes the problem as
described.

Martin

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-15 11:20 ` [PATCH 2/2] git-jump: ignore deleted files in diff mode Jeff King
@ 2024-09-16 20:14   ` Martin Ågren
  2024-09-17  8:58   ` Taylor Blau
  1 sibling, 0 replies; 10+ messages in thread
From: Martin Ågren @ 2024-09-16 20:14 UTC (permalink / raw)
  To: Jeff King; +Cc: git

On Sun, 15 Sept 2024 at 13:20, Jeff King <peff@peff.net> wrote:
>
> If you do something like this:
>
>   rm file_a
>   echo change >file_b
>   git jump diff
>
> then we'll generate two quickfix entries for the diff, one for each
> file. But the one for the deleted file is rather pointless. There's no
> content to show since the file is gone, and in fact we open the editor
> with the path /dev/null!

Ah, yes, I've seen this. I just went "I can see why this would happen"
and moved on to the next hunk. Thanks for actually patching.

> Let's skip such entries entirely. There's nothing useful to show, since
> the point is that the file has been deleted.

Agreed. Anybody who relies on `git jump diff` to produce an empty
quickfix list (no entries to go through) if *and only if* the diff is
empty is already in for a surprise due to binary files. Skipping
/dev/null makes perfect sense.

>         perl -ne '
> -       if (m{^\+\+\+ (.*)}) { $file = $1; next }
> +       if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
>         defined($file) or next;

This looks very reasonable. From my testing, this patch fixes the issue.

Martin

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-15 11:20 ` [PATCH 2/2] git-jump: ignore deleted files in diff mode Jeff King
  2024-09-16 20:14   ` Martin Ågren
@ 2024-09-17  8:58   ` Taylor Blau
  2024-09-17 15:03     ` Junio C Hamano
  1 sibling, 1 reply; 10+ messages in thread
From: Taylor Blau @ 2024-09-17  8:58 UTC (permalink / raw)
  To: Jeff King; +Cc: git

On Sun, Sep 15, 2024 at 07:20:24AM -0400, Jeff King wrote:
> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> index 78e7394406..3f69675961 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; next }
> +	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }

I was surprised to not see you use `--diff-filter` here, but I think
that that makes sense. You only would want to exclude deletions, since
that would be the only time the post-image is /dev/null AFAICT.

But I guess that would make it impossible to do "git jump diff
--diff-filter", which may be useful in some cases. TBH, I have almost
never used --diff-filter myself, so I'm not sure how common that is.

In any event, this seems to be quite a reasonable implementation to me.

Thanks,
Taylor

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-17  8:58   ` Taylor Blau
@ 2024-09-17 15:03     ` Junio C Hamano
  2024-09-24 20:55       ` Jeff King
  0 siblings, 1 reply; 10+ messages in thread
From: Junio C Hamano @ 2024-09-17 15:03 UTC (permalink / raw)
  To: Taylor Blau; +Cc: Jeff King, git

Taylor Blau <me@ttaylorr.com> writes:

> On Sun, Sep 15, 2024 at 07:20:24AM -0400, Jeff King wrote:
>> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
>> index 78e7394406..3f69675961 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; next }
>> +	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
>
> I was surprised to not see you use `--diff-filter` here, but I think
> that that makes sense. You only would want to exclude deletions, since
> that would be the only time the post-image is /dev/null AFAICT.

So "--diff-filter=d" (lowercase)?

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-17 15:03     ` Junio C Hamano
@ 2024-09-24 20:55       ` Jeff King
  2024-09-24 22:03         ` Taylor Blau
  0 siblings, 1 reply; 10+ messages in thread
From: Jeff King @ 2024-09-24 20:55 UTC (permalink / raw)
  To: Junio C Hamano; +Cc: Taylor Blau, git

On Tue, Sep 17, 2024 at 08:03:33AM -0700, Junio C Hamano wrote:

> Taylor Blau <me@ttaylorr.com> writes:
> 
> > On Sun, Sep 15, 2024 at 07:20:24AM -0400, Jeff King wrote:
> >> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> >> index 78e7394406..3f69675961 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; next }
> >> +	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
> >
> > I was surprised to not see you use `--diff-filter` here, but I think
> > that that makes sense. You only would want to exclude deletions, since
> > that would be the only time the post-image is /dev/null AFAICT.
> 
> So "--diff-filter=d" (lowercase)?

Hmm, yeah, I think that probably would work. In my mind, we are
accepting arbitrary diffs from the user because of the "$@". But anybody
who overrides us with

  git jump diff --diff-filter=D

maybe gets what they want/deserve. I think it's mostly academic, and as
the original has already graduated, I'm inclined to leave it unless
somebody comes back with some use case.

(Sorry for the slow reply, I am quite behind due to travel last week).

-Peff

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-24 20:55       ` Jeff King
@ 2024-09-24 22:03         ` Taylor Blau
  2024-09-25 17:11           ` Junio C Hamano
  0 siblings, 1 reply; 10+ messages in thread
From: Taylor Blau @ 2024-09-24 22:03 UTC (permalink / raw)
  To: Jeff King; +Cc: Junio C Hamano, git

On Tue, Sep 24, 2024 at 04:55:58PM -0400, Jeff King wrote:
> On Tue, Sep 17, 2024 at 08:03:33AM -0700, Junio C Hamano wrote:
>
> > Taylor Blau <me@ttaylorr.com> writes:
> >
> > > On Sun, Sep 15, 2024 at 07:20:24AM -0400, Jeff King wrote:
> > >> diff --git a/contrib/git-jump/git-jump b/contrib/git-jump/git-jump
> > >> index 78e7394406..3f69675961 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; next }
> > >> +	if (m{^\+\+\+ (.*)}) { $file = $1 eq "/dev/null" ? undef : $1; next }
> > >
> > > I was surprised to not see you use `--diff-filter` here, but I think
> > > that that makes sense. You only would want to exclude deletions, since
> > > that would be the only time the post-image is /dev/null AFAICT.
> >
> > So "--diff-filter=d" (lowercase)?
>
> Hmm, yeah, I think that probably would work. In my mind, we are
> accepting arbitrary diffs from the user because of the "$@". But anybody
> who overrides us with
>
>   git jump diff --diff-filter=D
>
> maybe gets what they want/deserve. I think it's mostly academic, and as
> the original has already graduated, I'm inclined to leave it unless
> somebody comes back with some use case.

Yeah, this is definitely all academic. I do not at all mind the
implementation that you went with here.

> (Sorry for the slow reply, I am quite behind due to travel last week).

Hopefully your travel back went smoothly. Welcome back :-).

Thanks,
Taylor

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

* Re: [PATCH 2/2] git-jump: ignore deleted files in diff mode
  2024-09-24 22:03         ` Taylor Blau
@ 2024-09-25 17:11           ` Junio C Hamano
  0 siblings, 0 replies; 10+ messages in thread
From: Junio C Hamano @ 2024-09-25 17:11 UTC (permalink / raw)
  To: Taylor Blau; +Cc: Jeff King, git

Taylor Blau <me@ttaylorr.com> writes:

> Yeah, this is definitely all academic. I do not at all mind the
> implementation that you went with here.

Neither do I.  Thanks, both.


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

end of thread, other threads:[~2024-09-25 17:12 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-09-15 11:11 [PATCH 0/2] a few git-jump quality-of-life fixes Jeff King
2024-09-15 11:18 ` [PATCH 1/2] git-jump: always specify column 1 for diff entries Jeff King
2024-09-16 20:13   ` Martin Ågren
2024-09-15 11:20 ` [PATCH 2/2] git-jump: ignore deleted files in diff mode Jeff King
2024-09-16 20:14   ` Martin Ågren
2024-09-17  8:58   ` Taylor Blau
2024-09-17 15:03     ` Junio C Hamano
2024-09-24 20:55       ` Jeff King
2024-09-24 22:03         ` Taylor Blau
2024-09-25 17:11           ` Junio C Hamano

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).