Code

user-manual: Add section "Why bisecting merge commits can be harder ..."
[git.git] / git-svn.perl
index 98218dabd993f982fbad02c9467129d9d8303b03..4c779b6c6d1c53030c0ed984155c885a25604e57 100755 (executable)
@@ -374,6 +374,9 @@ sub cmd_set_tree {
 
 sub cmd_dcommit {
        my $head = shift;
+       git_cmd_try { command_oneline(qw/diff-index --quiet HEAD/) }
+               'Cannot dcommit with a dirty index.  Commit your changes first'
+               . "or stash them with `git stash'.\n";
        $head ||= 'HEAD';
        my @refs;
        my ($url, $rev, $uuid, $gs) = working_head_info($head, \@refs);
@@ -384,7 +387,14 @@ sub cmd_dcommit {
        }
        my $last_rev;
        my ($linear_refs, $parents) = linearize_history($gs, \@refs);
-       foreach my $d (@$linear_refs) {
+       if ($_no_rebase && scalar(@$linear_refs) > 1) {
+               warn "Attempting to commit more than one change while ",
+                    "--no-rebase is enabled.\n",
+                    "If these changes depend on each other, re-running ",
+                    "without --no-rebase will be required."
+       }
+       while (1) {
+               my $d = shift @$linear_refs or last;
                unless (defined $last_rev) {
                        (undef, $last_rev, undef) = cmt_metadata("$d~1");
                        unless (defined $last_rev) {
@@ -395,6 +405,7 @@ sub cmd_dcommit {
                if ($_dry_run) {
                        print "diff-tree $d~1 $d\n";
                } else {
+                       my $cmt_rev;
                        my %ed_opts = ( r => $last_rev,
                                        log => get_commit_entry($d)->{log},
                                        ra => Git::SVN::Ra->new($gs->full_url),
@@ -402,42 +413,78 @@ sub cmd_dcommit {
                                        tree_b => $d,
                                        editor_cb => sub {
                                               print "Committed r$_[0]\n";
-                                              $last_rev = $_[0]; },
+                                              $cmt_rev = $_[0];
+                                       },
                                        svn_path => '');
                        if (!SVN::Git::Editor->new(\%ed_opts)->apply_diff) {
                                print "No changes\n$d~1 == $d\n";
                        } elsif ($parents->{$d} && @{$parents->{$d}}) {
-                               $gs->{inject_parents_dcommit}->{$last_rev} =
+                               $gs->{inject_parents_dcommit}->{$cmt_rev} =
                                                               $parents->{$d};
                        }
+                       $_fetch_all ? $gs->fetch_all : $gs->fetch;
+                       next if $_no_rebase;
+
+                       # we always want to rebase against the current HEAD,
+                       # not any head that was passed to us
+                       my @diff = command('diff-tree', $d,
+                                          $gs->refname, '--');
+                       my @finish;
+                       if (@diff) {
+                               @finish = rebase_cmd();
+                               print STDERR "W: $d and ", $gs->refname,
+                                            " differ, using @finish:\n",
+                                            join("\n", @diff), "\n";
+                       } else {
+                               print "No changes between current HEAD and ",
+                                     $gs->refname,
+                                     "\nResetting to the latest ",
+                                     $gs->refname, "\n";
+                               @finish = qw/reset --mixed/;
+                       }
+                       command_noisy(@finish, $gs->refname);
+                       if (@diff) {
+                               @refs = ();
+                               my ($url_, $rev_, $uuid_, $gs_) =
+                                             working_head_info($head, \@refs);
+                               my ($linear_refs_, $parents_) =
+                                             linearize_history($gs_, \@refs);
+                               if (scalar(@$linear_refs) !=
+                                   scalar(@$linear_refs_)) {
+                                       fatal "# of revisions changed ",
+                                         "\nbefore:\n",
+                                         join("\n", @$linear_refs),
+                                         "\n\nafter:\n",
+                                         join("\n", @$linear_refs_), "\n",
+                                         'If you are attempting to commit ',
+                                         "merges, try running:\n\t",
+                                         'git rebase --interactive',
+                                         '--preserve-merges ',
+                                         $gs->refname,
+                                         "\nBefore dcommitting";
+                               }
+                               if ($url_ ne $url) {
+                                       fatal "URL mismatch after rebase: ",
+                                             "$url_ != $url";
+                               }
+                               if ($uuid_ ne $uuid) {
+                                       fatal "uuid mismatch after rebase: ",
+                                             "$uuid_ != $uuid";
+                               }
+                               # remap parents
+                               my (%p, @l, $i);
+                               for ($i = 0; $i < scalar @$linear_refs; $i++) {
+                                       my $new = $linear_refs_->[$i] or next;
+                                       $p{$new} =
+                                               $parents->{$linear_refs->[$i]};
+                                       push @l, $new;
+                               }
+                               $parents = \%p;
+                               $linear_refs = \@l;
+                       }
+                       $last_rev = $cmt_rev;
                }
        }
-       return if $_dry_run;
-       unless ($gs) {
-               warn "Could not determine fetch information for $url\n",
-                    "Will not attempt to fetch and rebase commits.\n",
-                    "This probably means you have useSvmProps and should\n",
-                    "now resync your SVN::Mirror repository.\n";
-               return;
-       }
-       $_fetch_all ? $gs->fetch_all : $gs->fetch;
-       unless ($_no_rebase) {
-               # we always want to rebase against the current HEAD, not any
-               # head that was passed to us
-               my @diff = command('diff-tree', 'HEAD', $gs->refname, '--');
-               my @finish;
-               if (@diff) {
-                       @finish = rebase_cmd();
-                       print STDERR "W: HEAD and ", $gs->refname, " differ, ",
-                                    "using @finish:\n", "@diff";
-               } else {
-                       print "No changes between current HEAD and ",
-                             $gs->refname, "\nResetting to the latest ",
-                             $gs->refname, "\n";
-                       @finish = qw/reset --mixed/;
-               }
-               command_noisy(@finish, $gs->refname);
-       }
 }
 
 sub cmd_find_rev {
@@ -837,14 +884,9 @@ sub working_head_info {
 
 sub read_commit_parents {
        my ($parents, $c) = @_;
-       my ($fh, $ctx) = command_output_pipe(qw/cat-file commit/, $c);
-       while (<$fh>) {
-               chomp;
-               last if '';
-               /^parent ($sha1)/ or next;
-               push @{$parents->{$c}}, $1;
-       }
-       close $fh; # break the pipe
+       chomp(my $p = command_oneline(qw/rev-list --parents -1/, $c));
+       $p =~ s/^($c)\s*// or die "rev-list --parents -1 $c failed!\n";
+       @{$parents->{$c}} = split(/ /, $p);
 }
 
 sub linearize_history {
@@ -3009,7 +3051,7 @@ package Git::SVN::Ra;
 use vars qw/@ISA $config_dir $_log_window_size/;
 use strict;
 use warnings;
-my ($can_do_switch, %ignored_err, $RA);
+my ($ra_invalid, $can_do_switch, %ignored_err, $RA);
 
 BEGIN {
        # enforce temporary pool usage for some simple functions
@@ -3170,7 +3212,11 @@ sub gs_do_switch {
                        $self->{url} = $full_url;
                        $reparented = 1;
                } else {
+                       $_[0] = undef;
+                       $self = undef;
+                       $RA = undef;
                        $ra = Git::SVN::Ra->new($full_url);
+                       $ra_invalid = 1;
                }
        }
        $ra ||= $self;
@@ -3230,6 +3276,7 @@ sub gs_fetch_loop_common {
        my $inc = $_log_window_size;
        my ($min, $max) = ($base, $head < $base + $inc ? $head : $base + $inc);
        my $longest_path = longest_common_path($gsv, $globs);
+       my $ra_url = $self->{url};
        while (1) {
                my %revs;
                my $err;
@@ -3291,6 +3338,13 @@ sub gs_fetch_loop_common {
                                        "$g->{t}-maxRev";
                                Git::SVN::tmp_config($k, $r);
                        }
+                       if ($ra_invalid) {
+                               $_[0] = undef;
+                               $self = undef;
+                               $RA = undef;
+                               $self = Git::SVN::Ra->new($ra_url);
+                               $ra_invalid = undef;
+                       }
                }
                # pre-fill the .rev_db since it'll eventually get filled in
                # with '0' x40 if something new gets committed
@@ -3565,7 +3619,7 @@ sub config_pager {
 }
 
 sub run_pager {
-       return unless -t *STDOUT;
+       return unless -t *STDOUT && defined $pager;
        pipe my $rfd, my $wfd or return;
        defined(my $pid = fork) or ::fatal "Can't fork: $!\n";
        if (!$pid) {