Skip to content

Commit

Permalink
Fix %(lineno) computation for hunk postimages that span only one line
Browse files Browse the repository at this point in the history
Usually diff hunk headers look like "@@ -a,b +c,d @@" where "a" is the
starting line number and "b" is the number of lines in the preimage of the
hunk. Same for "c" and "d" except they refer to lines in the postimage.

Git omits "b" and "d" if they are 1.  Tig already treated "b" as optional but
not "d". We can simply ignore a missing number because "header->new.lines"
is already initialized to 1 in parse_chunk_header().

	$ echo old line 1 >? old
	$ echo new line 1 >? new
	$ git diff --no-index old new | tail -n3
	@@ -1 +1 @@
	-old line 1
	+new line 1
  • Loading branch information
krobelus committed Dec 31, 2020
1 parent a24d48e commit 1eea254
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/parse.c
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ parse_chunk_header(struct chunk_header *header, const char *line)
return parse_ulong(&line, &header->old.position, '-', false) &&
parse_ulong(&line, &header->old.lines, ',', true) &&
parse_ulong(&line, &header->new.position, '+', false) &&
parse_ulong(&line, &header->new.lines, ',', false);
parse_ulong(&line, &header->new.lines, ',', true);
}

bool
Expand Down
31 changes: 31 additions & 0 deletions test/diff/line-number-test
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#!/bin/sh

. libtest.sh
. libgit.sh

export COLUMNS=40
export LINES=3

steps '
:view-diff
:move-last-line
:move-up
:!echo %(lineno)
:save-display added-line-number.screen
'


git_init "$work_dir"

test_setup_work_dir()
{
git_commit --allow-empty-message
git_add README.md 'new line 1\n'
}

test_tig

assert_equals 'added-line-number.screen' <<EOF
1
[pager] echo 1 - line 1 of 1 100%
EOF

0 comments on commit 1eea254

Please sign in to comment.