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>
Jean-Luc Herren committed
Oct 9, 2007 at 21:29 UTC
7288ed8ebdab54e5aa05400bd840e1348ffdc270
1 file changed
+5
-6
git-add--interactive.perl
+5
-6
@@ -360,7 +360,9 @@ sub hunk_splittable {
360
sub parse_hunk_header {
361
my ($line) = @_;
362
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;
366
return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
367
}
368
@@ -705,9 +707,6 @@ sub patch_update_cmd {
707
parse_hunk_header($text->[0]);
708
709
if (!$_->{USE}) {
708
- if (!defined $o_cnt) { $o_cnt = 1; }
709
- if (!defined $n_cnt) { $n_cnt = 1; }
710
-
710
# We would have added ($n_cnt - $o_cnt) lines
711
# to the postimage if we were to use this hunk,
712
# but we didn't. So the line number that the next
@@ -719,10 +718,10 @@ sub patch_update_cmd {
718
if ($n_lofs) {
719
$n_ofs += $n_lofs;
720
$text->[0] = ("@@ -$o_ofs" .
722
- ((defined $o_cnt)
721
+ (($o_cnt != 1)
722
? ",$o_cnt" : '') .
723
" +$n_ofs" .
725
- ((defined $n_cnt)
724
+ (($n_cnt != 1)
725
? ",$n_cnt" : '') .
726
" @@\n");
727
}