chiark / gitweb /
git-debrebase: merge: Fix a $nomerge message
[dgit.git] / git-debrebase
index b015e885a2bb195cec1418c0123ae366cd5e41b4..1e8de52941e10f68a80dc244642925ac0169cbd4 100755 (executable)
@@ -36,6 +36,8 @@ use Dpkg::Version;
 use File::FnMatch qw(:fnmatch);
 use File::Copy;
 
+$debugcmd_when_debuglevel = 2;
+
 our ($usage_message) = <<'END';
 usages:
   git-debrebase [<options>] [--|-i <git rebase options...>]
@@ -226,7 +228,7 @@ sub get_differs ($$) {
            unless $differs & (D_PAT_ADD|D_PAT_OTH);
     }
 
-    printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
+    printdebug sprintf "get_differs %s %s = %#x\n", $x, $y, $differs;
 
     return $differs;
 }
@@ -261,11 +263,15 @@ sub read_tree_debian ($) {
     rm_subdir_cached 'debian/patches';
 }
 
-sub read_tree_upstream ($;$) {
-    my ($treeish, $keep_patches) = @_;
-    my $save = cmdoutput @git, qw(write-tree --prefix=debian/);
+sub read_tree_upstream ($;$$) {
+    my ($treeish, $keep_patches, $tree_with_debian) = @_;
+    # if $tree_with_debian is supplied, will use that for debian/
+    # otherwise will save and restore it.
+    my $debian =
+       $tree_with_debian ? "$tree_with_debian:debian"
+       : cmdoutput @git, qw(write-tree --prefix=debian/);
     runcmd @git, qw(read-tree), $treeish;
-    read_tree_subdir 'debian', $save;
+    read_tree_subdir 'debian', $debian;
     rm_subdir_cached 'debian/patches' unless $keep_patches;
 };
 
@@ -329,6 +335,7 @@ sub gbp_pq_export ($$$) {
     # must be run in a workarea.  $bname and patch-queue/$bname
     # ought not to exist.  Leaves you on patch-queue/$bname with
     # the patches staged but not committed.
+    printdebug "gbp_pq_export $bname $base $tip\n";
     runcmd @git, qw(checkout -q -b), $bname, $base;
     runcmd @git, qw(checkout -q -b), "patch-queue/$bname", $tip;
     my @gbp_cmd = (qw(gbp pq export));
@@ -341,11 +348,11 @@ sub gbp_pq_export ($$$) {
 }
 
 
-# xxx allow merge separately from laundering
+# xxx allow merge resolution separately from laundering, before git merge
 #
-# xxx docs highlight forbidden things
-# xxx docs list allowable things ?
-# xxx docs explicitly forbid some rebase
+# xxx general gdr docs highlight forbidden things
+# xxx general gdr docs list allowable things ?
+# xxx general gdr docs explicitly forbid some rebase
 #
 # xxx provide a way for the user to help
 # xxx (eg, provide wreckage provide way to continue)
@@ -369,6 +376,8 @@ sub merge_series ($$;@) {
 
     # Creates, in *_q, a key MR for its private use
 
+    printdebug "merge_series newbase=$newbase\n";
+
     $input_qs[$_]{MR}{S} = $_ foreach (0..$#input_qs);
     $base_q->{MR}{S} = 'base';
 
@@ -386,10 +395,11 @@ sub merge_series ($$;@) {
        foreach my $q ($base_q, reverse @input_qs) {
            my $s = $q->{MR}{S};
            gbp_pq_export "p-$s", $q->{SeriesBase}, $q->{SeriesTip};
+           my @earlier;
            if (open S, $seriesfile) {
-               my @earlier;
                while (my $patch = <S>) {
                    chomp $patch or die $!;
+                   $prereq{$patch} //= {};
                    foreach my $earlier (@earlier) {
                        $prereq{$patch}{$earlier}{$s}++ and die;
                    }
@@ -407,17 +417,34 @@ sub merge_series ($$;@) {
                 "[git-debrebase merge-innards patch-queue import:".
                 " $q->{SeriesTip}]"
             ];
+           printdebug "merge_series  pec $pec ";
            runcmd @git, qw(rm -q --cached), $seriesfile;
            $pec = make_commit [ $pec ], [
                 "Drop series file from $s to avoid merge trouble",
                 "[git-debrebase merge-innards patch-queue prep:".
                 " $q->{SeriesTip}]"
             ];
+
+           read_tree_debian $newbase;
+           if (@earlier) {
+               read_tree_subdir 'debian/patches', "$pec:debian/patches";
+           } else {
+               rm_subdir_cached 'debian/patches';
+           }
+           $pec = make_commit [ $pec ], [
+ "Update debian/ (excluding patches) to final to avoid re-merging",
+ "debian/ was already merged and we need to just take that.",
+                "[git-debrebase merge-innards patch-queue packaging:".
+                " $q->{SeriesTip}]"
+            ];
+
+           printdebug "pec' $pec\n";
             runcmd @git, qw(reset -q --hard), $pec;
            $q->{MR}{PEC} = $pec;
        }
        # now, because of reverse, we are on $input_q->{MR}{OQC}
        runcmd @git, qw(checkout -q -b merge);
+       printdebug "merge_series merging...\n";
        my @mergecmd = (@git, qw(merge --quiet --no-edit), "p-1");
        debugcmd '+', @mergecmd;
        $!=0; $?=-1;
@@ -425,23 +452,29 @@ sub merge_series ($$;@) {
            failedcmd @mergecmd;
        }
 
+       printdebug "merge_series merge ok, series...\n";
        # We need to construct a new series file
        # Firstly, resolve prereq
-       foreach my $f (keys %prereq) {
+       foreach my $f (sort keys %prereq) {
+           printdebug "merge_series  patch\t$f\t";
            if (!stat_exists "debian/patches/$f") {
+               print DEBUG " drop\n" if $debuglevel;
                # git merge deleted it; that's how we tell it's not wanted
                delete $prereq{$f};
                next;
            }
-           foreach my $g (keys %{ $prereq{$f} }) {
+           print DEBUG " keep\n" if $debuglevel;
+           foreach my $g (sort keys %{ $prereq{$f} }) {
                my $gfp = $prereq{$f}{$g};
-               next unless
-                   # want to keep it
-                   !!$gfp->{0} == !!$gfp->{1}
+               printdebug "merge_series  prereq\t$f\t-> $g\t";
+               if (!!$gfp->{0} == !!$gfp->{1}
                    ? $gfp->{0}
-                   : !$gfp->{base}
-                   ;
-               delete $prereq{$f}{$g};
+                   : !$gfp->{base}) {
+                   print DEBUG "\tkeep\n" if $debuglevel;
+               } else {
+                   print DEBUG "\tdrop\n" if $debuglevel;
+                   delete $prereq{$f}{$g};
+               }
            }
        }
 
@@ -480,6 +513,7 @@ sub merge_series ($$;@) {
                }
                $best = $try;
            }
+           printdebug "merge_series series next $best\n";
            print NS "$best\n" or die $!;
            delete $prereq{$best};
            foreach my $gp (values %prereq) {
@@ -490,16 +524,19 @@ sub merge_series ($$;@) {
        runcmd @git, qw(add), $seriesfile;
        runcmd @git, qw(commit --quiet -m), 'Merged series';
 
+       printdebug "merge_series series gbp pq import\n";
        runcmd qw(gbp pq import);
 
        # OK now we are on patch-queue/merge, and we need to rebase
        # onto the intended parent and drop the patches from each one
 
+       printdebug "merge_series series ok, building...\n";
        my $build = $newbase;
        my @lcmd = (@git, qw(rev-list --reverse merge..patch-queue/merge));
        foreach my $c (grep /./, split /\n/, cmdoutput @lcmd) {
            my $commit = git_cat_file $c, 'commit';
-           read_tree_upstream $c;
+           printdebug "merge_series series ok, building $c\n";
+           read_tree_upstream $c, 0, $newbase;
            my $tree = cmdoutput @git, qw(write-tree);
            $commit =~ s{^parent (\S+)$}{parent $build}m or confess;
            $commit =~ s{^tree (\S+)$}{tree $tree}m      or confess;
@@ -510,6 +547,13 @@ sub merge_series ($$;@) {
        }
        $result = $build;
        runcmd @git, qw(update-ref refs/heads/result), $result;
+
+       runcmd @git, qw(checkout -q -b debug);
+       runcmd @git, qw(commit --allow-empty -q -m M-INDEX);
+       runcmd @git, qw(add .);
+       runcmd @git, qw(commit --allow-empty -q -m M-WORKTREE);
+       printdebug sprintf "merge_series done debug=%s\n",
+           git_rev_parse 'HEAD';
     };
     printdebug "merge_series returns $result\n";
     return $result;
@@ -791,8 +835,25 @@ sub classify ($) {
     return $classify->("VanillaMerge");
 }
 
-sub keycommits ($;$$$$) {
-    my ($head, $furniture, $unclean, $trouble, $fatal) = @_;
+sub keycommits ($;$$$$$);
+
+sub mergedbreakwaters_anchor ($) {
+    my ($cl) = @_;
+    my $best_anchor;
+    foreach my $p (@{ $cl->{Parents} }) {
+       my ($panchor, $pbw) = keycommits $p->{CommitId},
+           undef,undef,undef,undef, 1;
+       $best_anchor = $panchor
+           if !defined $best_anchor
+           or is_fast_fwd $best_anchor, $panchor;
+       fail "inconsistent anchors in merged-breakwaters $p->{CommitId}"
+           unless is_fast_fwd $panchor, $best_anchor;
+    }
+    return $best_anchor;
+}
+
+sub keycommits ($;$$$$$) {
+    my ($head, $furniture, $unclean, $trouble, $fatal, $claimed_bw) = @_;
     # => ($anchor, $breakwater)
 
     # $unclean->("unclean-$tagsfx", $msg, $cl)
@@ -808,6 +869,8 @@ sub keycommits ($;$$$$) {
     # $fatal is for unprocessable commits, and should normally cause
     #    a failure.  If ignored, agaion, (undef, undef) is returned.
     #
+    # If $claimed_bw, this is supposed to be a breakwater commit.
+    #
     # If a callback is undef, fail is called instead.
     # If a callback is defined but false, the situation is ignored.
     # Callbacks may say:
@@ -815,6 +878,7 @@ sub keycommits ($;$$$$) {
     # if the answer is no longer wanted.
 
     my ($anchor, $breakwater);
+    $breakwater = $head if $claimed_bw;
     my $clogonly;
     my $cl;
     my $found_pm;
@@ -827,6 +891,13 @@ sub keycommits ($;$$$$) {
        return unless $cb;
        $cb->("unclean-$tagsfx", $why, $cl, $mainwhy);
     };
+    my $found_anchor = sub {
+       ($anchor) = @_;
+       $breakwater //= $clogonly;
+       $breakwater //= $head;
+       no warnings qw(exiting);
+       last;
+    };
     for (;;) {
        $cl = classify $head;
        my $ty = $cl->{Type};
@@ -840,10 +911,7 @@ sub keycommits ($;$$$$) {
        } elsif ($ty eq 'Anchor' or
                 $ty eq 'TreatAsAnchor' or
                 $ty eq 'BreakwaterStart') {
-           $anchor = $head;
-           $breakwater //= $clogonly;
-           $breakwater //= $head;
-           last;
+           $found_anchor->($head);
        } elsif ($ty eq 'Upstream') {
            $x->($unclean, 'ordering',
  "packaging change ($breakwater) follows upstream change"," (eg $head)")
@@ -875,6 +943,8 @@ sub keycommits ($;$$$$) {
            $x->($trouble, 'vanillamerge',
                 "found vanilla merge"," ($head)");
            return (undef,undef);
+       } elsif ($ty eq 'MergedBreakwaters') {
+           $found_anchor->(mergedbreakwaters_anchor $cl);
        } else {
            $x->($fatal, 'unprocessable',
                 "found unprocessable commit, cannot cope: $cl->{Why}",
@@ -1062,9 +1132,21 @@ sub walk ($;$$$) {
                return $bomb->();
            }
            die "$ty ?";
-       } elsif ($ty eq 'VanillaMerge' or $ty eq 'MergedBreakwaters') {
-           # xxx need to handle ffq if one side was unstitched
-           # wait both of them may be!
+       } elsif ($ty eq 'MergedBreakwaters') {
+           $last_anchor = mergedbreakwaters_anchor $cl;
+           $build_start->(' MergedBreakwaters', $cur);
+           last;
+       } elsif ($ty eq 'VanillaMerge') {
+           # User may have merged unstitched branch(es).  We will
+           # have now lost what ffq-prev was then (since the later
+           # pseudomerge may introduce further changes).  The effect
+           # of resolving such a merge is that we may have to go back
+           # further in history to find a merge base, since the one
+           # which was reachable via ffq-prev is no longer findable.
+           # This is suboptimal, but if it all works we'll have done
+           # the right thing.
+           # xxx we should warn the user in the docs about this
+
            my $ok=1;
            my $best_anchor;
            # We expect to find a dominating anchor amongst the
@@ -1090,9 +1172,6 @@ sub walk ($;$$$) {
 
            printdebug "*** MERGE\n";
 
-           # xxx avoid calling walk without nogenerate when
-           # we have a MergedBreakwater; instead call keycommits ?
-
            my @bwbcmd = (@git, qw(merge-base));
            my @ibcmd = (@git, qw(merge-base --all));
            my $might_be_in_bw = 1;
@@ -1120,7 +1199,7 @@ sub walk ($;$$$) {
            foreach my $p (@$ps) {
                $prline->(" VanillaMerge ".$p->{Ix});
                if (!is_fast_fwd $p->{Anchor}, $best_anchor) {
-                   $nomerge->('DivergentAnchor');
+                   $nomerge->('divergent anchors');
                } elsif ($p->{Anchor} eq $best_anchor) {
                    print $report " SameAnchor" if $report;
                } else {
@@ -1138,14 +1217,14 @@ sub walk ($;$$$) {
            }
 
            if ($ok && $might_be_in_bw) {
+               # We could rewrite this to contaion the metadata
+               # declaring it to be MergedBreakwaters, but
+               # unnecessarily rewriting a merge seems unhelpful.
                $prline->(" VanillaMerge MergedBreakwaters");
                $last_anchor = $best_anchor;
                $build_start->('MergedBreakwaters', $cur);
            }
 
-           $nomerge->("alleged merged-breakwater is not a breakwater")
-               unless $ty eq 'VanillaMerge';
-
            my $bwb = cmdoutput @bwbcmd;
 
            # OK, now we have a breakwater base, but we need the merge
@@ -1188,7 +1267,8 @@ sub walk ($;$$$) {
            push @brw_cl, {
                 %$cl,
                 SpecialMethod => 'MergeCreateMergedBreakwaters',
-                $xmsg->('construct merged breakwater from vanilla merge'),
+                $xmsg->('constructed from vanilla merge',
+                       ' merged-breakwater'),
             };
            push @upp_cl, {
                 %$cl,
@@ -1213,6 +1293,11 @@ sub walk ($;$$$) {
 
     my $rewriting = 0;
 
+    my $read_tree_upstream = sub {
+       my ($treeish) = @_;
+       read_tree_upstream $treeish, 0, $build;
+    };
+
     $#upp_cl = $upp_limit if defined $upp_limit;
  
     my $committer_authline = calculate_committer_authline();
@@ -1240,7 +1325,7 @@ sub walk ($;$$$) {
            if ($method eq 'Debian') {
                read_tree_debian($cltree);
            } elsif ($method eq 'Upstream') {
-               read_tree_upstream($cltree);
+               $read_tree_upstream->($cltree);
            } elsif ($method eq 'StartRewrite') {
                $rewriting = 1;
                next;
@@ -1253,13 +1338,13 @@ sub walk ($;$$$) {
                confess unless $rewriting;
                my $differs = (get_differs $build, $cltree);
                next unless $differs & D_UPS;
-               read_tree_upstream($cltree);
+               $read_tree_upstream->($cltree);
                push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
            } elsif ($method eq 'MergeCreateMergedBreakwaters') {
                print "Found a general merge, will try to tidy it up.\n";
                $rewriting = 1;
-               read_tree_upstream($cl->{MergeBestAnchor});
-               read_tree_upstream($cl->{MergeBestAnchor});
+               $read_tree_upstream->($cl->{MergeBestAnchor});
+               $read_tree_upstream->($cl->{MergeBestAnchor});
                read_tree_debian($cltree);
                @parents = map { $_->{Breakwater} } @{ $cl->{Parents} };
            } elsif ($method eq 'MergeMergeSeries') {
@@ -1269,7 +1354,29 @@ sub walk ($;$$$) {
                    $cl->{MergeInterchangeBaseInfo},
                    @{ $cl->{Parents} };
                $last_anchor = $cl->{MergeBestAnchor};
-               # xxx need to check the tree somehow
+
+               # Check for mismerges:
+               my $check = sub {
+                   my ($against, $allow, $what) = @_;
+                   my $differs = get_differs $build, $against;
+                   $nomerge->(sprintf
+       "merge misresolved: %s are not the same (%s %s d.%#x)",
+                              $what, $against, $build, $differs)
+                       if $differs & ~($allow | D_PAT_ADD);
+               };
+
+               # Breakwater changes which were in each side of the
+               # merge will have been incorporated into the
+               # MergeCreateMergedBreakwaters output.  Because the
+               # upstream series was rebased onto the new breakwater,
+               # so should all of the packaging changes which were in
+               # the input.
+               $check->($input, D_UPS, 'debian files');
+
+               # Upstream files are merge_series, which ought to
+               # have been identical to the original merge.
+               $check->($cl->{CommitId}, DS_DEB, 'upstream files');
+
                print "Merge resolution successful.\n";
                next;
            } else {
@@ -1310,7 +1417,7 @@ sub walk ($;$$$) {
     };
 
     my $final_check = get_differs $build, $input;
-    die sprintf "internal error %#x %s %s", $final_check, $build, $input
+    die sprintf "internal error %#x %s %s", $final_check, $input, $build
        if $final_check & ~D_PAT_ADD;
 
     my @r = ($build, $breakwater, $last_anchor);