git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[PATCH 1/3] contrib/git-jump: fix greedy regex when matching hunks

From
Jeff King <peff@peff.net>
Date
Jul 22, 2016, 16:28 UTC
Message-ID
<20160722162844.GA14080@sigill.intra.peff.net>
In-Reply-To
<20160722162707.GA13905@sigill.intra.peff.net>

The hunk-header regex looks for "\+\d+" to find the post-image line numbers, but it skips the pre-image line numbers with a simple ".*". That means we may greedily eat the post-image numbers and match a "\+\d" further on, in the funcname text.

For example, commit 6b9c38e has this hunk header:
  diff --git a/t/t0006-date.sh b/t/t0006-date.sh
  [...]
  @@ -50,8 +50,8 @@ check_show iso-local "$TIME" '2016-06-15 14:13:20 +0000'
If you run:
  git checkout 6b9c38e
  git jump diff HEAD^ t/

it will erroneously match "+0000" as the starting line number and jump there, rather than line 50.

We can fix it by just making the "skip" regex non-greedy, taking the first "+" we see, which should be the post-image line information.

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 dc90cd6..1f1b996 100755
--- a/contrib/git-jump/git-jump
+++ b/contrib/git-jump/git-jump
@@ -25,7 +25,7 @@ mode_diff() {
 	perl -ne '
 	if (m{^\+\+\+ (.*)}) { $file = $1; next }
 	defined($file) or next;
-	if (m/^@@ .*\+(\d+)/) { $line = $1; next }
+	if (m/^@@ .*?\+(\d+)/) { $line = $1; next }
 	defined($line) or next;
 	if (/^ /) { $line++; next }
 	if (/^[-+]\s*(.*)/) {
-- 
2.9.2.506.g8452fe7
Previous: Jeff KingNext: Jeff King
Message 2 of 7 in “minor git-jump improvements”
  1. 0/3 minor git-jump improvementsJeff King, Jul 22, 2016
  2. 1/3 contrib/git-jump: fix greedy regex when matching hunksJeff King, Jul 22, 2016
  3. 2/3 contrib/git-jump: add whitespace-checking modeJeff King, Jul 22, 2016
  4. 2/3 contrib/git-jump: add whitespace-checking modeJeff King, Jul 22, 2016
  5. 3/3 contrib/git-jump: fix typo in READMEJeff King, Jul 22, 2016
  6. Johannes SchindelinJul 23, 2016
  7. Jeff KingJul 24, 2016

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.