Skip to content

Commit 7288ed8

Browse files
jlherrenspearce
authored andcommitted
git add -i: Fix parsing of abbreviated hunk headers
The unified diff format allows one-line ranges to be abbreviated by omiting the size. The hunk header "@@ -10,1 +10,1 @@" can be expressed as "@@ -10 +10 @@", but this wasn't properly parsed in all cases. Such abbreviated hunk headers are generated when a one-line change (add, remove or modify) appears without context; for example because the file is a one-liner itself or because GIT_DIFF_OPTS was set to '-u0'. If the user then runs 'git add -i' and enters the 'patch' command for that file, perl complains about undefined variables. Signed-off-by: Jean-Luc Herren <jlh@gmx.ch> Signed-off-by: Lars Hjemli <hjemli@gmail.com> Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
1 parent a72c874 commit 7288ed8

File tree

1 file changed

+5
-6
lines changed

1 file changed

+5
-6
lines changed

git-add--interactive.perl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,9 @@ sub hunk_splittable {
360360
sub parse_hunk_header {
361361
my ($line) = @_;
362362
my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
363-
$line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
363+
$line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
364+
$o_cnt = 1 unless defined $o_cnt;
365+
$n_cnt = 1 unless defined $n_cnt;
364366
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
365367
}
366368

@@ -705,9 +707,6 @@ sub patch_update_cmd {
705707
parse_hunk_header($text->[0]);
706708

707709
if (!$_->{USE}) {
708-
if (!defined $o_cnt) { $o_cnt = 1; }
709-
if (!defined $n_cnt) { $n_cnt = 1; }
710-
711710
# We would have added ($n_cnt - $o_cnt) lines
712711
# to the postimage if we were to use this hunk,
713712
# but we didn't. So the line number that the next
@@ -719,10 +718,10 @@ sub patch_update_cmd {
719718
if ($n_lofs) {
720719
$n_ofs += $n_lofs;
721720
$text->[0] = ("@@ -$o_ofs" .
722-
((defined $o_cnt)
721+
(($o_cnt != 1)
723722
? ",$o_cnt" : '') .
724723
" +$n_ofs" .
725-
((defined $n_cnt)
724+
(($n_cnt != 1)
726725
? ",$n_cnt" : '') .
727726
" @@\n");
728727
}

0 commit comments

Comments
 (0)