]> rtime.felk.cvut.cz Git - git.git/commitdiff
git add -i: Fix parsing of abbreviated hunk headers
authorJean-Luc Herren <jlh@gmx.ch>
Tue, 9 Oct 2007 19:29:26 +0000 (21:29 +0200)
committerShawn O. Pearce <spearce@spearce.org>
Tue, 16 Oct 2007 01:00:40 +0000 (21:00 -0400)
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>
git-add--interactive.perl

index be6881496c88fcffc52031e482bfdd8d03faa66c..15b3f5b36c78978ce7165c46ec7b6539e260f0a4 100755 (executable)
@@ -360,7 +360,9 @@ sub hunk_splittable {
 sub parse_hunk_header {
        my ($line) = @_;
        my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
-           $line =~ /^@@ -(\d+)(?:,(\d+)) \+(\d+)(?:,(\d+)) @@/;
+           $line =~ /^@@ -(\d+)(?:,(\d+))? \+(\d+)(?:,(\d+))? @@/;
+       $o_cnt = 1 unless defined $o_cnt;
+       $n_cnt = 1 unless defined $n_cnt;
        return ($o_ofs, $o_cnt, $n_ofs, $n_cnt);
 }
 
@@ -705,9 +707,6 @@ sub patch_update_cmd {
                    parse_hunk_header($text->[0]);
 
                if (!$_->{USE}) {
-                       if (!defined $o_cnt) { $o_cnt = 1; }
-                       if (!defined $n_cnt) { $n_cnt = 1; }
-
                        # We would have added ($n_cnt - $o_cnt) lines
                        # to the postimage if we were to use this hunk,
                        # but we didn't.  So the line number that the next
@@ -719,10 +718,10 @@ sub patch_update_cmd {
                        if ($n_lofs) {
                                $n_ofs += $n_lofs;
                                $text->[0] = ("@@ -$o_ofs" .
-                                             ((defined $o_cnt)
+                                             (($o_cnt != 1)
                                               ? ",$o_cnt" : '') .
                                              " +$n_ofs" .
-                                             ((defined $n_cnt)
+                                             (($n_cnt != 1)
                                               ? ",$n_cnt" : '') .
                                              " @@\n");
                        }