]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-add--interactive.perl
git-add -i: allow multiple selection in patch subcommand
[thirdparty/git.git] / git-add--interactive.perl
index be6881496c88fcffc52031e482bfdd8d03faa66c..e3472165503a98572769ac8b28caef39d2f3d6b4 100755 (executable)
@@ -37,10 +37,7 @@ sub list_untracked {
                chomp $_;
                $_;
        }
-       run_cmd_pipe(qw(git ls-files --others
-                       --exclude-per-directory=.gitignore),
-                    "--exclude-from=$GIT_DIR/info/exclude",
-                    '--', @_);
+       run_cmd_pipe(qw(git ls-files --others --exclude-standard --), @ARGV);
 }
 
 my $status_fmt = '%12s %12s %s';
@@ -59,9 +56,17 @@ sub list_modified {
        my ($only) = @_;
        my (%data, @return);
        my ($add, $del, $adddel, $file);
+       my @tracked = ();
+
+       if (@ARGV) {
+               @tracked = map {
+                       chomp $_; $_;
+               } run_cmd_pipe(qw(git ls-files --exclude-standard --), @ARGV);
+               return if (!@tracked);
+       }
 
        for (run_cmd_pipe(qw(git diff-index --cached
-                            --numstat --summary HEAD))) {
+                            --numstat --summary HEAD --), @tracked)) {
                if (($add, $del, $file) =
                    /^([-\d]+)  ([-\d]+)        (.*)/) {
                        my ($change, $bin);
@@ -84,7 +89,7 @@ sub list_modified {
                }
        }
 
-       for (run_cmd_pipe(qw(git diff-files --numstat --summary))) {
+       for (run_cmd_pipe(qw(git diff-files --numstat --summary --), @tracked)) {
                if (($add, $del, $file) =
                    /^([-\d]+)  ([-\d]+)        (.*)/) {
                        if (!exists $data{$file}) {
@@ -255,7 +260,7 @@ sub list_and_choose {
                                $chosen[$i] = $choose;
                        }
                }
-               last if ($opts->{IMMEDIATE});
+               last if ($opts->{IMMEDIATE} || $line eq '*');
        }
        for ($i = 0; $i < @stuff; $i++) {
                if ($chosen[$i]) {
@@ -360,7 +365,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);
 }
 
@@ -372,9 +379,8 @@ sub split_hunk {
        # it can be split, but we would need to take care of
        # overlaps later.
 
-       my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
+       my ($o_ofs, undef, $n_ofs) = parse_hunk_header($text->[0]);
        my $hunk_start = 1;
-       my $next_hunk_start;
 
       OUTER:
        while (1) {
@@ -441,8 +447,8 @@ sub split_hunk {
        for my $hunk (@split) {
                $o_ofs = $hunk->{OLD};
                $n_ofs = $hunk->{NEW};
-               $o_cnt = $hunk->{OCNT};
-               $n_cnt = $hunk->{NCNT};
+               my $o_cnt = $hunk->{OCNT};
+               my $n_cnt = $hunk->{NCNT};
 
                my $head = ("@@ -$o_ofs" .
                            (($o_cnt != 1) ? ",$o_cnt" : '') .
@@ -457,7 +463,7 @@ sub split_hunk {
 sub find_last_o_ctx {
        my ($it) = @_;
        my $text = $it->{TEXT};
-       my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) = parse_hunk_header($text->[0]);
+       my ($o_ofs, $o_cnt) = parse_hunk_header($text->[0]);
        my $i = @{$text};
        my $last_o_ctx = $o_ofs + $o_cnt;
        while (0 < --$i) {
@@ -529,8 +535,7 @@ sub coalesce_overlapping_hunks {
 
        for (grep { $_->{USE} } @in) {
                my $text = $_->{TEXT};
-               my ($o_ofs, $o_cnt, $n_ofs, $n_cnt) =
-                   parse_hunk_header($text->[0]);
+               my ($o_ofs) = parse_hunk_header($text->[0]);
                if (defined $last_o_ctx &&
                    $o_ofs <= $last_o_ctx) {
                        merge_hunk($out[-1], $_);
@@ -562,15 +567,17 @@ sub patch_update_cmd {
        @mods = grep { !($_->{BINARY}) } @mods;
        return if (!@mods);
 
-       my ($it) = list_and_choose({ PROMPT => 'Patch update',
-                                    SINGLETON => 1,
-                                    IMMEDIATE => 1,
-                                    HEADER => $status_head, },
-                                  @mods);
-       return if (!$it);
+       my (@them) = list_and_choose({ PROMPT => 'Patch update',
+                                      HEADER => $status_head, },
+                                    @mods);
+       for (@them) {
+               patch_update_file($_->{VALUE});
+       }
+}
 
+sub patch_update_file {
        my ($ix, $num);
-       my $path = $it->{VALUE};
+       my $path = shift;
        my ($head, @hunk) = parse_diff($path);
        for (@{$head->{TEXT}}) {
                print;
@@ -697,7 +704,7 @@ sub patch_update_cmd {
 
        @hunk = coalesce_overlapping_hunks(@hunk);
 
-       my ($o_lofs, $n_lofs) = (0, 0);
+       my $n_lofs = 0;
        my @result = ();
        for (@hunk) {
                my $text = $_->{TEXT};
@@ -705,9 +712,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 +723,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");
                        }
@@ -807,8 +811,6 @@ sub main_loop {
        }
 }
 
-my @z;
-
 refresh();
 status_cmd();
 main_loop();