+sub cmd_analyse () {
+ die if ($ARGV[0]//'') =~ m/^-/;
+ badusage "too many arguments to analyse" if @ARGV>1;
+ my ($old) = @ARGV;
+ if (defined $old) {
+ $old = git_rev_parse $old;
+ } else {
+ $old = get_head();
+ }
+ my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
+ STDOUT->error and die $!;
+}
+
+sub cmd_new_upstream_v0 () {
+ # tree should be clean and this is not checked
+ # automatically and unconditionally launders before rebasing
+ # if rebase --abort is used, laundering has still been done
+
+ my %pieces;
+
+ badusage "need NEW-VERSION UPS-COMMITTISH" unless @ARGV >= 2;
+
+ # parse args - low commitment
+ my $new_version = (new Dpkg::Version scalar(shift @ARGV), check => 1);
+ my $new_upstream_version = $new_version->version();
+
+ my $new_upstream = git_rev_parse shift @ARGV;
+
+ my $piece = sub {
+ my ($n, @x) = @_; # may be ''
+ my $pc = $pieces{$n} //= {
+ Name => $n,
+ Desc => ($n ? "upstream piece \`$n'" : "upstream (main piece"),
+ };
+ while (my $k = shift @x) { $pc->{$k} = shift @x; }
+ $pc;
+ };
+
+ my @newpieces;
+ my $newpiece = sub {
+ my ($n, @x) = @_; # may be ''
+ my $pc = $piece->($n, @x, NewIx => (scalar @newpieces));
+ push @newpieces, $pc;
+ };
+
+ $newpiece->('',
+ OldIx => 0,
+ New => $new_upstream,
+ );
+ while (@ARGV && $ARGV[0] !~ m{^-}) {
+ my $n = shift @ARGV;
+
+ badusage "for each EXTRA-UPS-NAME need EXTRA-UPS-COMMITISH"
+ unless @ARGV && $ARGV[0] !~ m{^-};
+
+ my $c = git_rev_parse shift @ARGV;
+ die unless $n =~ m/^$extra_orig_namepart_re$/;
+ $newpiece->($n, New => $c);
+ }
+
+ # now we need to investigate the branch this generates the
+ # laundered version but we don't switch to it yet
+ my $old_head = get_head();
+ my ($old_laundered_tip,$old_bw,$old_upstream_update) = walk $old_head;
+
+ my $old_bw_cl = classify $old_bw;
+ my $old_upstream_update_cl = classify $old_upstream_update;
+ confess unless $old_upstream_update_cl->{OrigParents};
+ my $old_upstream = parsecommit
+ $old_upstream_update_cl->{OrigParents}[0]{CommitId};
+
+ $piece->('', Old => $old_upstream->{CommitId});
+
+ if ($old_upstream->{Msg} =~ m{^\[git-debrebase }m) {
+ if ($old_upstream->{Msg} =~
+ m{^\[git-debrebase upstream-combine \.((?: $extra_orig_namepart_re)+)\:.*\]$}m
+ ) {
+ my @oldpieces = ('', split / /, $1);
+ my $parentix = -1 + scalar @{ $old_upstream->{Parents} };
+ foreach my $i (0..$#oldpieces) {
+ my $n = $oldpieces[$i];
+ $piece->($n, Old => $old_upstream->{CommitId}.'^'.$parentix);
+ }
+ } else {
+ fproblem "previous upstream $old_upstream->{CommitId} is from".
+ " git-debrebase but not an \`upstream-combine' commit";
+ }
+ }
+
+ foreach my $pc (values %pieces) {
+ if (!$pc->{Old}) {
+ fproblem "introducing upstream piece \`$pc->{Name}'";
+ } elsif (!$pc->{New}) {
+ fproblem "dropping upstream piece \`$pc->{Name}'";
+ } elsif (!is_fast_fwd $pc->{Old}, $pc->{New}) {
+ fproblem "not fast forward: $pc->{Name} $pc->{Old}..$pc->{New}";
+ }
+ }
+
+ printdebug "%pieces = ", (dd \%pieces), "\n";
+ printdebug "\@newpieces = ", (dd \@newpieces), "\n";
+
+ fproblems_maybe_bail();
+
+ my $new_bw;
+
+ fresh_workarea();
+ in_workarea sub {
+ my @upstream_merge_parents;
+
+ if (!$fproblems) {
+ push @upstream_merge_parents, $old_upstream->{CommitId};
+ }
+
+ foreach my $pc (@newpieces) { # always has '' first
+ if ($pc->{Name}) {
+ read_tree_subdir $pc->{Name}, $pc->{New};
+ } else {
+ runcmd @git, qw(read-tree), $pc->{New};
+ }
+ push @upstream_merge_parents, $pc->{New};
+ }
+
+ # index now contains the new upstream
+
+ if (@newpieces > 1) {
+ # need to make the upstream subtree merge commit
+ $new_upstream = make_commit \@upstream_merge_parents,
+ [ "Combine upstreams for $new_upstream_version",
+ ("[git-debrebase upstream-combine . ".
+ (join " ", map { $_->{Name} } @newpieces[1..$#newpieces]).
+ ": new upstream]"),
+ ];
+ }
+
+ # $new_upstream is either the single upstream commit, or the
+ # combined commit we just made. Either way it will be the
+ # "upstream" parent of the breakwater special merge.
+
+ read_tree_subdir 'debian', "$old_bw:debian";
+
+ # index now contains the breakwater merge contents
+ $new_bw = make_commit [ $old_bw, $new_upstream ],
+ [ "Update to upstream $new_upstream_version",
+ "[git-debrebase breakwater: new upstream $new_upstream_version, merge]",
+ ];
+
+ # Now we have to add a changelog stanza so the Debian version
+ # is right.
+ die if unlink "debian";
+ die $! unless $!==ENOENT or $!==ENOTEMPTY;
+ unlink "debian/changelog" or $!==ENOENT or die $!;
+ mkdir "debian" or die $!;
+ open CN, ">", "debian/changelog" or die $!;
+ my $oldclog = git_cat_file ":debian/changelog";
+ $oldclog =~ m/^($package_re) \(\S+\) / or
+ fail "cannot parse old changelog to get package name";
+ my $p = $1;
+ print CN <<END, $oldclog or die $!;
+$p ($new_version) UNRELEASED; urgency=medium
+
+ * Update to new upstream version $new_upstream_version.
+
+ --
+
+END
+ close CN or die $!;
+ runcmd @git, qw(update-index --add --replace), 'debian/changelog';
+
+ # Now we have the final new breakwater branch in the index
+ $new_bw = make_commit [ $new_bw ],
+ [ "Update changelog for new upstream $new_upstream_version",
+ "[git-debrebase: new upstream $new_upstream_version, changelog]",
+ ];
+ };
+
+ # we have constructed the new breakwater. we now need to commit to
+ # the laundering output, because git-rebase can't easily be made
+ # to make a replay list which is based on some other branch
+
+ update_head_postlaunder $old_head, $old_laundered_tip,
+ 'launder for new upstream';
+
+ my @cmd = (@git, qw(rebase --onto), $new_bw, $old_bw, @ARGV);
+ runcmd @cmd;
+ # now it's for the user to sort out
+}
+
+sub cmd_gbp2debrebase () {
+ badusage "needs 1 optional argument, the upstream" unless @ARGV<=1;
+ my ($upstream_spec) = @ARGV;
+ $upstream_spec //= 'refs/heads/upstream';
+ my $upstream = git_rev_parse $upstream_spec;
+ my $old_head = get_head();
+
+ my $upsdiff = get_differs $upstream, $old_head;
+ if ($upsdiff & D_UPS) {
+ runcmd @git, qw(--no-pager diff),
+ $upstream, $old_head,
+ qw( -- :!/debian :/);
+ fail "upstream ($upstream_spec) and HEAD are not identical in upstream files";
+ }
+
+ if (!is_fast_fwd $upstream, $old_head) {
+ fproblem "upstream ($upstream) is not an ancestor of HEAD";
+ } else {
+ my $wrong = cmdoutput
+ (@git, qw(rev-list --ancestry-path), "$upstream..HEAD",
+ qw(-- :/ :!/debian));
+ if (length $wrong) {
+ fproblem "history between upstream ($upstream) and HEAD contains direct changes to upstream files - are you sure this is a gbp (patches-unapplied) branch?";
+ print STDERR "list expected changes with: git log --stat --ancestry-path $upstream_spec..HEAD -- :/ ':!/debian'\n";
+ }
+ }
+
+ if ((git_cat_file "$upstream:debian")[0] ne 'missing') {
+ fproblem "upstream ($upstream) contains debian/ directory";
+ }
+
+ fproblems_maybe_bail();
+
+ my $work;
+
+ fresh_workarea();
+ in_workarea sub {
+ runcmd @git, qw(checkout -q -b gdr-internal), $old_head;
+ # make a branch out of the patch queue - we'll want this in a mo
+ runcmd qw(gbp pq import);
+ # strip the patches out
+ runcmd @git, qw(checkout -q gdr-internal~0);
+ rm_subdir_cached 'debian/patches';
+ $work = make_commit ['HEAD'], [
+ 'git-debrebase import: drop patch queue',
+ 'Delete debian/patches, as part of converting to git-debrebase format.',
+ '[git-debrebase: gbp2debrebase, drop patches]'
+ ];
+ # make the breakwater pseudomerge
+ # the tree is already exactly right
+ $work = make_commit [$work, $upstream], [
+ 'git-debrebase import: declare upstream',
+ 'First breakwater merge.',
+ '[git-debrebase breakwater: declare upstream]'
+ ];
+
+ # rebase the patch queue onto the new breakwater
+ runcmd @git, qw(reset --quiet --hard patch-queue/gdr-internal);
+ runcmd @git, qw(rebase --quiet --onto), $work, qw(gdr-internal);
+ $work = get_head();
+ };
+
+ update_head_checkout $old_head, $work, 'gbp2debrebase';
+}
+
+sub cmd_downstream_rebase_launder_v0 () {
+ badusage "needs 1 argument, the baseline" unless @ARGV==1;
+ my ($base) = @ARGV;
+ $base = git_rev_parse $base;
+ my $old_head = get_head();
+ my $current = $old_head;
+ my $topmost_keep;
+ for (;;) {
+ if ($current eq $base) {
+ $topmost_keep //= $current;
+ print " $current BASE stop\n";
+ last;
+ }
+ my $cl = classify $current;
+ print " $current $cl->{Type}";
+ my $keep = 0;
+ my $p0 = $cl->{Parents}[0]{CommitId};
+ my $next;
+ if ($cl->{Type} eq 'Pseudomerge') {
+ print " ^".($cl->{Contributor}{Ix}+1);
+ $next = $cl->{Contributor}{CommitId};
+ } elsif ($cl->{Type} eq 'AddPatches' or
+ $cl->{Type} eq 'Changelog') {
+ print " strip";
+ $next = $p0;
+ } else {
+ print " keep";
+ $next = $p0;
+ $keep = 1;
+ }
+ print "\n";
+ if ($keep) {
+ $topmost_keep //= $current;
+ } else {
+ die "to-be stripped changes not on top of the branch\n"
+ if $topmost_keep;
+ }
+ $current = $next;
+ }
+ if ($topmost_keep eq $old_head) {
+ print "unchanged\n";
+ } else {
+ print "updating to $topmost_keep\n";
+ update_head_checkout
+ $old_head, $topmost_keep,
+ 'downstream-rebase-launder-v0';
+ }
+}
+
+GetOptions("D+" => \$debuglevel,
+ 'force!') or die badusage "bad options\n";
+initdebug('git-debrebase ');
+enabledebug if $debuglevel;
+
+my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);