]> git.ipfire.org Git - thirdparty/git.git/blobdiff - git-add--interactive.perl
Add "--patch" option to git-add--interactive
[thirdparty/git.git] / git-add--interactive.perl
index dc3038091dd7db7dd4bb96cf8832df789b4d127f..df5df3ec905e23da61e83906aa6a5651cf06149d 100755 (executable)
@@ -2,10 +2,20 @@
 
 use strict;
 
+# command line options
+my $patch_mode;
+
 sub run_cmd_pipe {
-       my $fh = undef;
-       open($fh, '-|', @_) or die;
-       return <$fh>;
+       if ($^O eq 'MSWin32') {
+               my @invalid = grep {m/[":*]/} @_;
+               die "$^O does not support: @invalid\n" if @invalid;
+               my @args = map { m/ /o ? "\"$_\"": $_ } @_;
+               return qx{@args};
+       } else {
+               my $fh = undef;
+               open($fh, '-|', @_) or die;
+               return <$fh>;
+       }
 }
 
 my ($GIT_DIR) = run_cmd_pipe(qw(git rev-parse --git-dir));
@@ -17,7 +27,7 @@ chomp($GIT_DIR);
 
 sub refresh {
        my $fh;
-       open $fh, '-|', qw(git update-index --refresh)
+       open $fh, 'git update-index --refresh |'
            or die;
        while (<$fh>) {
                ;# ignore 'needs update'
@@ -30,10 +40,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';
@@ -52,9 +59,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);
@@ -77,7 +92,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}) {
@@ -206,9 +221,13 @@ sub list_and_choose {
                        print ">> ";
                }
                my $line = <STDIN>;
-               last if (!$line);
+               if (!$line) {
+                       print "\n";
+                       $opts->{ON_EOF}->() if $opts->{ON_EOF};
+                       last;
+               }
                chomp $line;
-               my $donesomething = 0;
+               last if $line eq '';
                for my $choice (split(/[\s,]+/, $line)) {
                        my $choose = 1;
                        my ($bottom, $top);
@@ -240,12 +259,11 @@ sub list_and_choose {
                                next TOPLOOP;
                        }
                        for ($i = $bottom-1; $i <= $top-1; $i++) {
-                               next if (@stuff <= $i);
+                               next if (@stuff <= $i || $i < 0);
                                $chosen[$i] = $choose;
-                               $donesomething++;
                        }
                }
-               last if (!$donesomething || $opts->{IMMEDIATE});
+               last if ($opts->{IMMEDIATE} || $line eq '*');
        }
        for ($i = 0; $i < @stuff; $i++) {
                if ($chosen[$i]) {
@@ -296,7 +314,7 @@ sub revert_cmd {
                my @lines = run_cmd_pipe(qw(git ls-tree HEAD --),
                                         map { $_->{VALUE} } @update);
                my $fh;
-               open $fh, '|-', qw(git update-index --index-info)
+               open $fh, '| git update-index --index-info'
                    or die;
                for (@lines) {
                        print $fh $_;
@@ -350,7 +368,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);
 }
 
@@ -362,9 +382,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) {
@@ -431,8 +450,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" : '') .
@@ -447,7 +466,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) {
@@ -519,8 +538,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], $_);
@@ -537,8 +555,8 @@ sub help_patch_cmd {
        print <<\EOF ;
 y - stage this hunk
 n - do not stage this hunk
-a - stage this and all the remaining hunks
-d - do not stage this hunk nor any of the remaining hunks
+a - stage this and all the remaining hunks in the file
+d - do not stage this hunk nor any of the remaining hunks in the file
 j - leave this hunk undecided, see next undecided hunk
 J - leave this hunk undecided, see next hunk
 k - leave this hunk undecided, see previous undecided hunk
@@ -548,19 +566,29 @@ EOF
 }
 
 sub patch_update_cmd {
-       my @mods = list_modified('file-only');
-       @mods = grep { !($_->{BINARY}) } @mods;
-       return if (!@mods);
+       my @mods = grep { !($_->{BINARY}) } list_modified('file-only');
+       my @them;
 
-       my ($it) = list_and_choose({ PROMPT => 'Patch update',
-                                    SINGLETON => 1,
-                                    IMMEDIATE => 1,
-                                    HEADER => $status_head, },
-                                  @mods);
-       return if (!$it);
+       if (!@mods) {
+               print STDERR "No changes.\n";
+               return 0;
+       }
+       if ($patch_mode) {
+               @them = @mods;
+       }
+       else {
+               @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;
@@ -687,7 +715,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};
@@ -695,9 +723,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
@@ -709,10 +734,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");
                        }
@@ -725,7 +750,7 @@ sub patch_update_cmd {
        if (@result) {
                my $fh;
 
-               open $fh, '|-', qw(git apply --cached);
+               open $fh, '| git apply --cached';
                for (@{$head->{TEXT}}, @result) {
                        print $fh $_;
                }
@@ -769,6 +794,20 @@ add untracked - add contents of untracked files to the staged set of changes
 EOF
 }
 
+sub process_args {
+       return unless @ARGV;
+       my $arg = shift @ARGV;
+       if ($arg eq "--patch") {
+               $patch_mode = 1;
+               $arg = shift @ARGV or die "missing --";
+               die "invalid argument $arg, expecting --"
+                   unless $arg eq "--";
+       }
+       elsif ($arg ne "--") {
+               die "invalid argument $arg, expecting --";
+       }
+}
+
 sub main_loop {
        my @cmd = ([ 'status', \&status_cmd, ],
                   [ 'update', \&update_cmd, ],
@@ -784,6 +823,7 @@ sub main_loop {
                                             SINGLETON => 1,
                                             LIST_FLAT => 4,
                                             HEADER => '*** Commands ***',
+                                            ON_EOF => \&quit_cmd,
                                             IMMEDIATE => 1 }, @cmd);
                if ($it) {
                        eval {
@@ -796,8 +836,12 @@ sub main_loop {
        }
 }
 
-my @z;
-
+process_args();
 refresh();
-status_cmd();
-main_loop();
+if ($patch_mode) {
+       patch_update_cmd();
+}
+else {
+       status_cmd();
+       main_loop();
+}