+ foreach my $ph (@ph) {
+ push @p, {
+ Ix => $#p,
+ CommitId => $ph,
+ Differs => (get_differs $t, $ph),
+ };
+ }
+
+ printdebug "classify $objid \$t=$t \@p",
+ (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
+ "\n";
+
+ my $classify = sub {
+ my ($type, @rest) = @_;
+ $r = { %$r, Type => $type, @rest };
+ if ($debuglevel) {
+ my $dd = new Data::Dumper [ $r ];
+ Terse $dd 1; Indent $dd 0; Useqq $dd 1;
+ printdebug " = $type ".(Dump $dd)."\n";
+ }
+ return $r;
+ };
+ my $unknown = sub {
+ my ($why) = @_;
+ $r = { %$r, Type => qw(Unknown) };
+ printdebug " ** Unknown\n";
+ return $r;
+ };
+
+ if (@p == 1) {
+ my $d = $r->{Parents}[0]{Differs};
+ if ($d == D_PAT_ADD) {
+ return $classify->(qw(AddPatches));
+ } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
+ return $unknown->("edits debian/patches");
+ } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
+ my ($ty,$dummy) = git_cat_file "$ph[0]:debian";
+ if ($ty eq 'tree') {
+ if ($d == D_DEB_CLOG) {
+ return $classify->(qw(Changelog));
+ } else {
+ return $classify->(qw(Packaging));
+ }
+ } elsif ($ty eq 'missing') {
+ return $classify->(qw(BreakwaterStart));
+ } else {
+ return $unknown->("parent's debian is not a directory");
+ }
+ } elsif ($d == D_UPS) {
+ return $classify->(qw(Upstream));
+ } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
+ return $classify->(qw(Mixed));
+ } elsif ($d == 0) {
+ return $unknown->("no changes");
+ } else {
+ confess "internal error $objid ?";
+ }
+ }
+ if (!@p) {
+ return $unknown->("origin commit");
+ }
+
+ my @identical = grep { !$_->{Differs} } @p;
+ if (@p == 2 && @identical == 1) {
+ my @overwritten = grep { $_->{Differs} } @p;
+ confess "internal error $objid ?" unless @overwritten==1;
+ return $classify->(qw(Pseudomerge),
+ Overwritten => $overwritten[0],
+ Contributor => $identical[0]);
+ }
+ if (@p == 2 && @identical == 2) {
+ my @bytime = nsort_by {
+ my ($ph,$pm) = get_commit $_->{CommitId};
+ $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
+ $1;
+ } @p;
+ return $classify->(qw(Pseudomerge),
+ SubType => qw(Ambiguous),
+ Overwritten => $bytime[0],
+ Contributor => $bytime[1]);
+ }
+ foreach my $p (@p) {
+ my ($p_h, $p_m) = get_commit $p->{CommitId};
+ $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
+ ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
+ }
+ my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
+ my $m2 = $m;
+ if (!(grep { !$_->{IsOrigin} } @p) and
+ (@orig_ps >= @p - 1) and
+ $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
+ $r->{NewMsg} = $m2;
+ return $classify->(qw(DgitImportUnpatched),
+ OrigParents => \@orig_ps);
+ }
+
+ my ($stype, $series) = git_cat_file "$t:debian/patches/series";
+ my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
+
+ # How to decide about l/r ordering of breakwater merges ? git
+ # --topo-order prefers to expand 2nd parent first. There's
+ # already an easy rune to look for debian/ history anyway (git log
+ # debian/) so debian breakwater branch should be 1st parent; that
+ # way also there's also an easy rune to look for the upstream
+ # patches (--topo-order).
+
+ # The above tells us which way *we* will generate them. But we
+ # might encounter ad-hoc breakwater merges generated manually,
+ # which might be the other way around. In principle, in some odd
+ # situations, a breakwater merge might have two identical parents.
+ # In that case we guess which way round it is (ie, which parent
+ # has the upstream history). The order of the 2-iteration loop
+ # controls which guess we make.
+
+ foreach my $prevbrw (qw(0 1)) {
+ if (@p == 2 &&
+ !$haspatches &&
+ !$p[$prevbrw]{IsOrigin} && # breakwater never starts with an origin
+ !($p[$prevbrw]{Differs} & ~DS_DEB) &&
+ !($p[!$prevbrw]{Differs} & ~D_UPS)) {
+ return $classify->(qw(BreakwaterUpstreamMerge),
+ OrigParents => [ $p[!$prevbrw] ]);
+ }
+ # xxx multi-.orig upstreams
+ }
+
+ return $unknown->("complex merge");
+}
+
+sub walk ($;$$);
+sub walk ($;$$) {
+ my ($input,
+ $nogenerate,$report) = @_;
+ # => ($tip, $breakwater_tip)
+ # (or nothing, if $nogenerate)