+ $cl = classify $cur;
+ my $ty = $cl->{Type};
+ my $st = $cl->{SubType};
+ $prline->("$cl->{CommitId} $cl->{Type}");
+ $found{$ty. ( defined($st) ? "-$st" : '' )}++;
+ push @processed, $cl;
+ my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
+ if ($ty eq 'AddPatches') {
+ $cur = $p0;
+ $rewrite_from_here->();
+ next;
+ } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
+ push @brw_cl, $cl;
+ $cur = $p0;
+ next;
+ } elsif ($ty eq 'BreakwaterStart') {
+ $build_start->('FirstPackaging', $cur);
+ } elsif ($ty eq 'Upstream') {
+ push @upp_cl, $cl;
+ $cur = $p0;
+ next;
+ } elsif ($ty eq 'Mixed') {
+ my $queue = sub {
+ my ($q, $wh) = @_;
+ my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
+ push @$q, $cls;
+ };
+ $queue->(\@brw_cl, "debian");
+ $queue->(\@upp_cl, "upstream");
+ $rewrite_from_here->();
+ $cur = $p0;
+ next;
+ } elsif ($ty eq 'Pseudomerge') {
+ my $contrib = $cl->{Contributor}{CommitId};
+ print $report " Contributor=$contrib" if $report;
+ push @pseudomerges, $cl;
+ $rewrite_from_here->();
+ $cur = $contrib;
+ next;
+ } elsif ($ty eq 'BreakwaterUpstreamMerge') {
+ $build_start->("PreviousBreakwater", $cur);
+ } elsif ($ty eq 'DgitImportUnpatched') {
+ my $pm = $pseudomerges[-1];
+ if (defined $pm) {
+ # To an extent, this is heuristic. Imports don't have
+ # a useful history of the debian/ branch. We assume
+ # that the first pseudomerge after an import has a
+ # useful history of debian/, and ignore the histories
+ # from later pseudomerges. Often the first pseudomerge
+ # will be the dgit import of the upload to the actual
+ # suite intended by the non-dgit NMUer, and later
+ # pseudomerges may represent in-archive copies.
+ my $ovwrs = $pm->{Overwritten};
+ printf $report " PM=%s \@Overwr:%d", $pm, (scalar @$ovwrs)
+ if $report;
+ if (@$ovwrs != 1) {
+ return $bomb->();
+ }
+ my $ovwr = $ovwrs->[0]{CommitId};
+ printf $report " Overwr=%s", $ovwr if $report;
+ # This import has a tree which is just like a
+ # breakwater tree, but it has the wrong history. It
+ # ought to have the previous breakwater (which the
+ # pseudomerge overwrote) as an ancestor. That will
+ # make the history of the debian/ files correct. As
+ # for the upstream version: either it's the same as
+ # was ovewritten (ie, same as the previous
+ # breakwater), in which case that history is precisely
+ # right; or, otherwise, it was a non-gitish upload of a
+ # new upstream version. We can tell these apart by
+ # looking at the tree of the supposed upstream.
+ push @brw_cl, {
+ %$cl,
+ SpecialMethod => 'DgitImportDebianUpdate',
+ $xmsg->("convert dgit import: debian changes")
+ };
+ my $differs = (get_differs $ovwr, $cl->{Tree});
+ printf $report " Differs=%#x", $differs if $report;
+ if ($differs & D_UPS) {
+ printf $report " D_UPS" if $report;
+ # This will also trigger if a non-dgit git-based NMU
+ # deleted .gitignore (which is a thing that some of
+ # the existing git tools do if the user doesn't
+ # somehow tell them not to). Ah well.
+ push @brw_cl, {
+ %$cl,
+ SpecialMethod => 'DgitImportUpstreamUpdate',
+ $xmsg->("convert dgit import: upstream changes")
+ };
+ }
+ $prline->(" Import");
+ $rewrite_from_here->();
+ $upp_limit //= $#upp_cl; # further, deeper, patches discarded
+ $cur = $ovwr;
+ next;
+ } else {
+ # Everything is from this import. This kind of import
+ # is already in valid breakwater format, with the
+ # patches as commits.
+ printf $report " NoPM" if $report;
+ # last thing we processed will have been the first patch,
+ # if there is one; which is fine, so no need to rewrite
+ # on account of this import
+ $build_start->("ImportOrigin", $cur);
+ }
+ die "$ty ?";
+ } else {
+ return $bomb->();
+ }
+ }
+ $prprdelim->();
+ return if $nogenerate;
+
+ # Now we build it back up again
+
+ fresh_workarea();
+
+ my $rewriting = 0;
+
+ my $rm_tree_cached = sub {
+ my ($subdir) = @_;
+ runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
+ };
+ my $read_tree_debian = sub {
+ my ($treeish) = @_;
+ $rm_tree_cached->(qw(debian));
+ runcmd @git, qw(read-tree --prefix=debian/), "$treeish:debian";
+ };
+ my $read_tree_upstream = sub {
+ my ($treeish) = @_;
+ runcmd @git, qw(read-tree), $treeish;
+ $read_tree_debian->($build);
+ };
+
+ my $committer_authline = calculate_committer_authline();
+
+ printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
+
+ confess "internal error" unless $build eq (pop @processed)->{CommitId};
+
+ in_workarea sub {
+ mkdir $rd or $!==EEXIST or die $!;
+ my $current_method;
+ runcmd @git, qw(read-tree), $build;
+ foreach my $cl (qw(Debian), (reverse @brw_cl),
+ { SpecialMethod => 'RecordBreakwaterTip' },
+ qw(Upstream), (reverse @upp_cl)) {
+ if (!ref $cl) {
+ $current_method = $cl;
+ next;
+ }
+ my $method = $cl->{SpecialMethod} // $current_method;
+ my @parents = ($build);
+ my $cltree = $cl->{CommitId};
+ printdebug "WALK BUILD ".($cltree//'undef').
+ " $method (rewriting=$rewriting)\n";
+ if ($method eq 'Debian') {
+ $read_tree_debian->($cltree);
+ } elsif ($method eq 'Upstream') {
+ $read_tree_upstream->($cltree);
+ } elsif ($method eq 'StartRewrite') {
+ $rewriting = 1;
+ next;
+ } elsif ($method eq 'RecordBreakwaterTip') {
+ $breakwater = $build;
+ next;
+ } elsif ($method eq 'DgitImportDebianUpdate') {
+ $read_tree_debian->($cltree);
+ $rm_tree_cached->(qw(debian/patches));
+ } elsif ($method eq 'DgitImportUpstreamUpdate') {
+ $read_tree_upstream->($cltree);
+ push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
+ } else {
+ confess "$method ?";
+ }
+ if (!$rewriting) {
+ my $procd = (pop @processed) // 'UNDEF';
+ if ($cl ne $procd) {
+ $rewriting = 1;
+ printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
+ }
+ }
+ my $newtree = cmdoutput @git, qw(write-tree);
+ my $ch = $cl->{Hdr};
+ $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
+ $ch =~ s{^parent .*\n}{}m;
+ $ch =~ s{(?=^author)}{
+ join '', map { "parent $_\n" } @parents
+ }me or confess "$ch ?";
+ if ($rewriting) {
+ $ch =~ s{^committer .*$}{$committer_authline}m
+ or confess "$ch ?";
+ }
+ my $cf = "$rd/m$rewriting";
+ open CD, ">", $cf or die $!;
+ print CD $ch, "\n", $cl->{Msg} or die $!;
+ close CD or die $!;
+ my @cmd = (@git, qw(hash-object));
+ push @cmd, qw(-w) if $rewriting;
+ push @cmd, qw(-t commit), $cf;
+ my $newcommit = cmdoutput @cmd;
+ confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
+ $build = $newcommit;
+ }
+ };
+
+ my $final_check = get_differs $build, $input;
+ die sprintf "internal error %#x %s %s", $final_check, $build, $input
+ if $final_check & ~D_PAT_ADD;