3 # Script helping make fast-forwarding histories while still rebasing
4 # upstream deltas when working on Debian packaging
6 # Copyright (C)2017 Ian Jackson
8 # This program is free software: you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation, either version 3 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # git-debrebase new-upstreams-v0 \
22 # NEW-VERSION ORIG-COMMITISH
23 # [EXTRA-ORIG-NAME EXTRA-ORIG-COMMITISH ...]
26 # git-debrebase status
27 # git-debrebase start # like ffqrebase start + debrebase launder
28 # git-debrebase new-upstream [stuff] # see below
29 # git-debrebase <git-rebase options> # does debrebase start if necessary
31 # git-debrebase analyse
32 # git-debrebase launder # prints breakwater tip
33 # git-debrebase create-new-upstream-breakwater [-f] <upstreaminfo>...
36 # [,][<subdir>:][+]<commitid>[,...]
38 # if initial comma is supplied, entries are not positional. Unspecified
39 # <subdir> means root (and there may be only one).
40 # xxx want auto branch names
43 # [+]<commit> [<subdir/> [+]<commit>...]
45 # --new-upstream-different-subtrees
48 # git-debrebase new-upstream
49 # - previous breakwater merge must be gdr-generated
50 # - orig set is the same as before
51 # - implicitly uses upstream branches according to orig set
52 # - not all upstream branches need be updated
53 # - insists on fast-forward of each branch, unless
54 # --force (or --force=<subdir>[/])
55 # branch set adjustments
56 # git-debrebase new-upstream --add <subdir>/
57 # git-debrebase new-upstream --rm <subdir>/
58 # git-debrebase new-upstream / [<subdir>/ ...]
59 # - orig set is adjusted
60 # - otherwise like auto (--add is not checked for ffness, obv)
61 # - multiple --add and --rm may be specified
62 # - --add makes new upstream the last contributor
64 # git-debrebase / [<rootcommitid>] [<subdir>/ [<commitid>] ...]
65 # - orig set is precisely as specified now
66 # - previous breakwater merge is irrelevant
67 # - no fast forward checks
68 # for now only explicit with commitids
70 # implicitly uses `upstream'
71 # # (or multiple other branches)
72 # git-debrebase new-upstream \
73 # [<subdir>/]=<commitid>
75 # UPSTREAM[,[[SUBDIR:]SUBUPSTREAM]
76 # default for SUBDIR: is from previous upstream merge[xxx terminology]
80 # when starting must record original start (for ff)
81 # and new rebase basis
83 # git-ffqrebase start [BASE]
84 # # records previous HEAD so it can be overwritten
85 # # records base for future git-ffqrebase
86 # git-ffqrebase set-base BASE
87 # git-ffqrebase <git-rebase options>
88 # git-ffqrebase finish
89 # git-ffqrebase status [BRANCH]
91 # refs/ffqrebase-prev/BRANCH BRANCH may be refs/...; if not it means
92 # refs/ffqrebase-base/BRANCH refs/heads/BRANCH
93 # zero, one, or both of these may exist
95 # git-debrebase without start, if already started, is willing
96 # to strip pseudomerges provided that they overwrite exactly
98 # xxxx is this right ? what matters is have we pushed
99 # I think in fact the right answer is:
100 # git-debrebase always strips out pseudomerges from its branch
101 # a pseudomerge is put in at the time we want to push
102 # at that time, we make a pseudomerge of the remote tracking
103 # branch (if raw git) or the dgit view (if dgit)
104 # for raw git git-ffqrebase, do want preciseley to record
105 # value of remote tracking branch or our branch, on start, so we
106 # overwrite only things we intend to
107 # the previous pseudomerge check for tags and remote branches ?
111 use Debian::Dgit qw(:DEFAULT :playground);
118 use Getopt::Long qw(:config posix_default gnu_compat bundling);
122 die "bad usage: $m\n";
128 my @cmd = qw(git config -z);
129 push @cmd, qw(--get-all) if wantarray;
131 my $out = cmdoutput @cmd;
132 return split /\0/, $out;
139 my $data = git_cat_file $objid, 'commit';
140 $data =~ m/(?<=\n)\n/ or die "$objid ($data) ?";
144 sub D_UPS () { 0x02; } # upstream files
145 sub D_PAT_ADD () { 0x04; } # debian/patches/ extra patches at end
146 sub D_PAT_OTH () { 0x08; } # debian/patches other changes
147 sub D_DEB_CLOG () { 0x10; } # debian/ (not patches/ or changelog)
148 sub D_DEB_OTH () { 0x20; } # debian/changelog
149 sub DS_DEB () { D_DEB_CLOG | D_DEB_OTH; } # debian/ (not patches/)
151 our $playprefix = 'debrebase';
157 sub in_workarea ($) {
160 my $r = eval { $sub->(); };
161 { local $@; changedir $maindir; }
165 sub fresh_workarea () {
166 $workarea = fresh_playground "$playprefix/work";
167 in_workarea sub { playtree_setup };
170 sub get_differs ($$) {
172 # This resembles quiltify_trees_differ, in dgit, a bit.
173 # But we don't care about modes, or dpkg-source-unrepresentable
174 # changes, and we don't need the plethora of different modes.
175 # Conversely we need to distinguish different kinds of changes to
176 # debian/ and debian/patches/.
181 my ($opts, $limits, $fn) = @_;
182 my @cmd = (@git, qw(diff-tree -z --no-renames));
184 push @cmd, "$_:" foreach $x, $y;
186 my $diffs = cmdoutput @cmd;
187 foreach (split /\0/, $diffs) { $fn->(); }
190 $rundiff->([qw(--name-only)], [], sub {
191 $differs |= $_ eq 'debian' ? DS_DEB : D_UPS;
194 if ($differs & DS_DEB) {
196 $rundiff->([qw(--name-only -r)], [qw(debian)], sub {
198 m{^debian/patches/} ? D_PAT_OTH :
199 $_ eq 'debian/changelog' ? D_DEB_CLOG :
202 die "mysterious debian changes $x..$y"
203 unless $differs & (D_PAT_OTH|DS_DEB);
206 if ($differs & D_PAT_OTH) {
208 $differs &= ~D_PAT_OTH;
210 $differs |= D_PAT_OTH;
211 no warnings qw(exiting); last;
213 $rundiff->([qw(--name-status -r)], [qw(debian/patches/)], sub {
214 no warnings qw(exiting);
215 if (!defined $mode) {
218 die unless s{^debian/patches/}{};
220 if ($mode eq 'A' && !m/\.series$/s) {
222 } elsif ($mode eq 'M' && $_ eq 'series') {
223 my $x_s = git_cat_file "$x:debian/patches/series", 'blob';
224 my $y_s = git_cat_file "$y:debian/patches/series", 'blob';
225 chomp $x_s; $x_s .= "\n";
226 $ok = $x_s eq substr($y_s, 0, length $x_s);
231 $differs |= $ok ? D_PAT_ADD : D_PAT_OTH;
233 die "mysterious debian/patches changes $x..$y"
234 unless $differs & (D_PAT_ADD|D_PAT_OTH);
237 printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
242 sub commit_pr_info ($) {
244 return Data::Dumper->dump([$r], [qw(commit)]);
247 sub calculate_committer_authline () {
248 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
249 'DUMMY COMMIT (git-debrebase)', "HEAD:";
250 my ($h,$m) = get_commit $c;
251 $h =~ m/^committer .*$/m or confess "($h) ?";
255 sub rm_subdir_cached ($) {
257 runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
260 # classify returns an info hash like this
262 # Hdr => # commit headers, including 1 final newline
263 # Msg => # commit message (so one newline is dropped)
265 # Type => (see below)
267 # Ix => $index # ie 0, 1, 2, ...
269 # Differs => return value from get_differs
271 # IsDggitImport => 'orig' 'tarball' 'unpatched' 'package' (as from dgit)
273 # NewMsg => # commit message, but with any [dgit import ...] edited
274 # # to say "[was: ...]"
285 # has additional entres in classification result
286 # Overwritten = [ subset of Parents ]
287 # Contributor = $the_remaining_Parent
289 # DgitImportUnpatched
290 # has additional entry in classification result
291 # OrigParents = [ subset of Parents ]
293 # BreakwaterUpstreamMerge
294 # has additional entry in classification result
295 # OrigParents = [ subset of Parents ] # singleton list
297 sub parsecommit ($;$) {
298 my ($objid, $p_ref) = @_;
299 # => hash with CommitId Hdr Msg Tree Parents
300 # Parents entries have only Ix CommitId
301 # $p_ref, if provided, must be [] and is used as a base for Parents
306 my ($h,$m) = get_commit $objid;
308 my ($t) = $h =~ m/^tree (\w+)$/m or die $objid;
309 my (@ph) = $h =~ m/^parent (\w+)$/mg;
319 foreach my $ph (@ph) {
333 my $r = parsecommit($objid, \@p);
337 $p->{Differs} => (get_differs $p->{CommitId}, $t),
340 printdebug "classify $objid \$t=$t \@p",
341 (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
345 my ($type, @rest) = @_;
346 $r = { %$r, Type => $type, @rest };
348 my $dd = new Data::Dumper [ $r ];
349 Terse $dd 1; Indent $dd 0; Useqq $dd 1;
350 printdebug " = $type ".(Dump $dd)."\n";
356 $r = { %$r, Type => qw(Unknown) };
357 printdebug " ** Unknown\n";
362 my $d = $r->{Parents}[0]{Differs};
363 if ($d == D_PAT_ADD) {
364 return $classify->(qw(AddPatches));
365 } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
366 return $unknown->("edits debian/patches");
367 } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
368 my ($ty,$dummy) = git_cat_file "$ph[0]:debian";
370 if ($d == D_DEB_CLOG) {
371 return $classify->(qw(Changelog));
373 return $classify->(qw(Packaging));
375 } elsif ($ty eq 'missing') {
376 return $classify->(qw(BreakwaterStart));
378 return $unknown->("parent's debian is not a directory");
380 } elsif ($d == D_UPS) {
381 return $classify->(qw(Upstream));
382 } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
383 return $classify->(qw(Mixed));
385 return $unknown->("no changes");
387 confess "internal error $objid ?";
391 return $unknown->("origin commit");
394 my @identical = grep { !$_->{Differs} } @p;
395 if (@p == 2 && @identical == 1) {
396 my @overwritten = grep { $_->{Differs} } @p;
397 confess "internal error $objid ?" unless @overwritten==1;
398 return $classify->(qw(Pseudomerge),
399 Overwritten => $overwritten[0],
400 Contributor => $identical[0]);
402 if (@p == 2 && @identical == 2) {
403 my @bytime = nsort_by {
404 my ($ph,$pm) = get_commit $_->{CommitId};
405 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
408 return $classify->(qw(Pseudomerge),
409 SubType => qw(Ambiguous),
410 Overwritten => $bytime[0],
411 Contributor => $bytime[1]);
414 my ($p_h, $p_m) = get_commit $p->{CommitId};
415 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
416 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
418 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
420 if (!(grep { !$_->{IsOrigin} } @p) and
421 (@orig_ps >= @p - 1) and
422 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
424 return $classify->(qw(DgitImportUnpatched),
425 OrigParents => \@orig_ps);
428 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
429 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
431 # How to decide about l/r ordering of breakwater merges ? git
432 # --topo-order prefers to expand 2nd parent first. There's
433 # already an easy rune to look for debian/ history anyway (git log
434 # debian/) so debian breakwater branch should be 1st parent; that
435 # way also there's also an easy rune to look for the upstream
436 # patches (--topo-order).
438 # The above tells us which way *we* will generate them. But we
439 # might encounter ad-hoc breakwater merges generated manually,
440 # which might be the other way around. In principle, in some odd
441 # situations, a breakwater merge might have two identical parents.
442 # In that case we guess which way round it is (ie, which parent
443 # has the upstream history). The order of the 2-iteration loop
444 # controls which guess we make.
446 foreach my $prevbrw (qw(0 1)) {
449 !$p[$prevbrw]{IsOrigin} && # breakwater never starts with an origin
450 !($p[$prevbrw]{Differs} & ~DS_DEB) &&
451 !($p[!$prevbrw]{Differs} & ~D_UPS)) {
452 return $classify->(qw(BreakwaterUpstreamMerge),
453 OrigParents => [ $p[!$prevbrw] ]);
457 # multi-orig upstreams are represented with a breakwater merge
458 # from a single upstream commit which combines the orig tarballs
460 return $unknown->("complex merge");
466 $nogenerate,$report) = @_;
467 # => ($tip, $breakwater_tip)
468 # (or nothing, if $nogenerate)
470 # go through commits backwards
471 # we generate two lists of commits to apply:
472 # breakwater branch and upstream patches
473 my (@brw_cl, @upp_cl, @processed);
480 my ($appendinfo) = @_;
483 $ms .= "\n\n[git-debrebase $appendinfo]\n";
486 my $rewrite_from_here = sub {
487 my $sp_cl = { SpecialMethod => 'StartRewrite' };
488 push @brw_cl, $sp_cl;
489 push @processed, $sp_cl;
494 my $prprdelim = sub { print $report $prdelim if $report; $prdelim=""; };
497 return unless $report;
498 print $report $prdelim, @_;
502 my $bomb = sub { # usage: return $bomb->();
503 print $report " Unprocessable" if $report;
506 return (undef,undef);
508 die "commit $cur: Cannot cope with this commit (d.".
509 (join ' ', map { sprintf "%#x", $_->{Differs} }
510 @{ $cl->{Parents} }). ")";
516 my $build_start = sub {
517 my ($msg, $parent) = @_;
520 no warnings qw(exiting); last;
525 my $ty = $cl->{Type};
526 my $st = $cl->{SubType};
527 $prline->("$cl->{CommitId} $cl->{Type}");
528 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
529 push @processed, $cl;
530 my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
531 if ($ty eq 'AddPatches') {
533 $rewrite_from_here->();
535 } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
539 } elsif ($ty eq 'BreakwaterStart') {
540 $build_start->('FirstPackaging', $cur);
541 } elsif ($ty eq 'Upstream') {
545 } elsif ($ty eq 'Mixed') {
548 my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
551 $queue->(\@brw_cl, "debian");
552 $queue->(\@upp_cl, "upstream");
553 $rewrite_from_here->();
556 } elsif ($ty eq 'Pseudomerge') {
557 my $contrib = $cl->{Contributor}{CommitId};
558 print $report " Contributor=$contrib" if $report;
559 push @pseudomerges, $cl;
560 $rewrite_from_here->();
563 } elsif ($ty eq 'BreakwaterUpstreamMerge') {
564 $build_start->("PreviousBreakwater", $cur);
565 } elsif ($ty eq 'DgitImportUnpatched') {
566 my $pm = $pseudomerges[-1];
568 # To an extent, this is heuristic. Imports don't have
569 # a useful history of the debian/ branch. We assume
570 # that the first pseudomerge after an import has a
571 # useful history of debian/, and ignore the histories
572 # from later pseudomerges. Often the first pseudomerge
573 # will be the dgit import of the upload to the actual
574 # suite intended by the non-dgit NMUer, and later
575 # pseudomerges may represent in-archive copies.
576 my $ovwrs = $pm->{Overwritten};
577 printf $report " PM=%s \@Overwr:%d", $pm, (scalar @$ovwrs)
582 my $ovwr = $ovwrs->[0]{CommitId};
583 printf $report " Overwr=%s", $ovwr if $report;
584 # This import has a tree which is just like a
585 # breakwater tree, but it has the wrong history. It
586 # ought to have the previous breakwater (which the
587 # pseudomerge overwrote) as an ancestor. That will
588 # make the history of the debian/ files correct. As
589 # for the upstream version: either it's the same as
590 # was ovewritten (ie, same as the previous
591 # breakwater), in which case that history is precisely
592 # right; or, otherwise, it was a non-gitish upload of a
593 # new upstream version. We can tell these apart by
594 # looking at the tree of the supposed upstream.
597 SpecialMethod => 'DgitImportDebianUpdate',
598 $xmsg->("convert dgit import: debian changes")
600 my $differs = (get_differs $ovwr, $cl->{Tree});
601 printf $report " Differs=%#x", $differs if $report;
602 if ($differs & D_UPS) {
603 printf $report " D_UPS" if $report;
604 # This will also trigger if a non-dgit git-based NMU
605 # deleted .gitignore (which is a thing that some of
606 # the existing git tools do if the user doesn't
607 # somehow tell them not to). Ah well.
610 SpecialMethod => 'DgitImportUpstreamUpdate',
611 $xmsg->("convert dgit import: upstream changes")
614 $prline->(" Import");
615 $rewrite_from_here->();
616 $upp_limit //= $#upp_cl; # further, deeper, patches discarded
620 # Everything is from this import. This kind of import
621 # is already in valid breakwater format, with the
622 # patches as commits.
623 printf $report " NoPM" if $report;
624 # last thing we processed will have been the first patch,
625 # if there is one; which is fine, so no need to rewrite
626 # on account of this import
627 $build_start->("ImportOrigin", $cur);
635 return if $nogenerate;
637 # Now we build it back up again
643 my $read_tree_debian = sub {
645 rm_subdir_cached qw(debian);
646 runcmd @git, qw(read-tree --prefix=debian/), "$treeish:debian";
648 my $read_tree_upstream = sub {
650 runcmd @git, qw(read-tree), $treeish;
651 $read_tree_debian->($build);
654 my $committer_authline = calculate_committer_authline();
656 printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
658 confess "internal error" unless $build eq (pop @processed)->{CommitId};
661 mkdir $rd or $!==EEXIST or die $!;
663 runcmd @git, qw(read-tree), $build;
664 foreach my $cl (qw(Debian), (reverse @brw_cl),
665 { SpecialMethod => 'RecordBreakwaterTip' },
666 qw(Upstream), (reverse @upp_cl)) {
668 $current_method = $cl;
671 my $method = $cl->{SpecialMethod} // $current_method;
672 my @parents = ($build);
673 my $cltree = $cl->{CommitId};
674 printdebug "WALK BUILD ".($cltree//'undef').
675 " $method (rewriting=$rewriting)\n";
676 if ($method eq 'Debian') {
677 $read_tree_debian->($cltree);
678 } elsif ($method eq 'Upstream') {
679 $read_tree_upstream->($cltree);
680 } elsif ($method eq 'StartRewrite') {
683 } elsif ($method eq 'RecordBreakwaterTip') {
684 $breakwater = $build;
686 } elsif ($method eq 'DgitImportDebianUpdate') {
687 $read_tree_debian->($cltree);
688 rm_subdir_cached qw(debian/patches);
689 } elsif ($method eq 'DgitImportUpstreamUpdate') {
690 $read_tree_upstream->($cltree);
691 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
696 my $procd = (pop @processed) // 'UNDEF';
699 printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
702 my $newtree = cmdoutput @git, qw(write-tree);
704 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
705 $ch =~ s{^parent .*\n}{}m;
706 $ch =~ s{(?=^author)}{
707 join '', map { "parent $_\n" } @parents
708 }me or confess "$ch ?";
710 $ch =~ s{^committer .*$}{$committer_authline}m
713 my $cf = "$rd/m$rewriting";
714 open CD, ">", $cf or die $!;
715 print CD $ch, "\n", $cl->{Msg} or die $!;
717 my @cmd = (@git, qw(hash-object));
718 push @cmd, qw(-w) if $rewriting;
719 push @cmd, qw(-t commit), $cf;
720 my $newcommit = cmdoutput @cmd;
721 confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
726 my $final_check = get_differs $build, $input;
727 die sprintf "internal error %#x %s %s", $final_check, $build, $input
728 if $final_check & ~D_PAT_ADD;
730 return ($build, $breakwater);
733 sub get_head () { return git_rev_parse qw(HEAD); }
735 sub update_head ($$$) {
736 my ($old, $new, $mrest) = @_;
737 runcmd @git, qw(update-ref -m), "debrebase: $mrest", 'HEAD', $new, $old;
740 sub update_head_checkout ($$$) {
741 my ($old, $new, $mrest) = @_;
742 my $symref = git_get_symref();
743 runcmd @git, qw(checkout), $new, qw(.);
744 update_head $old, $new, $mrest;
748 badusage "no arguments to launder allowed" if @ARGV;
749 my $old = get_head();
750 my ($tip,$breakwater) = walk $old;
751 update_head $old, $tip, 'launder';
752 # no tree changes except debian/patches
753 runcmd @git, qw(rm --quiet --ignore-unmatch -rf debian/patches);
754 printf "# breakwater tip\n%s\n", $breakwater;
755 printf "# working tip\n%s\n", $tip;
759 die if ($ARGV[0]//'') =~ m/^-/;
760 badusage "too many arguments to analyse" if @ARGV>1;
763 $old = git_rev_parse $old;
767 my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
768 STDOUT->error and die $!;
771 sub cmd_new_upstream_v0 () {
773 "need NEW-VERSION ORIG-COMMITISH [EXTRA-ORIG-NAME EXTRA-ORIG-COMMITISH...]"
774 unless @ARGV % 2 == 0 and @ARGV >= 2;
775 # tree should be clean and this is not checked
776 # automatically and unconditionally launders before rebasing
777 # if rebase --abort is used, laundering has still been done
779 # parse args - low commitment
780 my $new_version = new Dpkg::Version scalar(shift @ARGV), 1;
781 my $new_upstream_version = $new_version->version();
782 my $new_orig_commitish = git_rev_parse shift @ARGV;
787 New => git_rev_parse shift @ARGV,
789 die unless $xo->{Name} =~ m/^$extra_orig_namepart_re$/;
790 push @extra_origs, $xo;
793 # now we need to investigate the branch this generates the
794 # laundered version but we don't switch to it yet
795 my $old = get_head();
796 my ($laundered_tip,$breakwater) = walk $old;
798 my $breakwater_cl = classify $breakwater;
799 my $old_orig_pi = $breakwater_cl->{OrigParents}[0];
808 print STDERR "upstream not fast forward: $msg\n";
812 # check fast forward, and make new combined-orig commit
813 my $old_orig_ci = parsecommit $old_orig_pi->{CommitId};
814 my $n_old_origs = scalar @{ $old_orig_cp->{Parents} };
815 @{ $n_old_origs } == @extra_origs+1 or
817 "previous breakwater upstream has %d parents".
818 " but new upstream has %d pieces, cannot check ff",
820 (1 + scalar @extra_origs));
823 my @upstream_merge_parents;
825 foreach my $piece_ix (0..$n_old_origs-1) {
826 my $prevpc = $breakwater.'^'.($old_orig_pi->{Ix} + 1);
828 $prevpc .= '^'.($piece_ix + 1);
830 die unless $ git_rev_parse $prevpc;
831 my ($newpc,$newdesc,$pcname);
833 $newpc = $new_orig_commitish;
834 $newdesc = 'new main upstream piece';
836 $newpc = $extra_origs[$piece_ix+1]{New};
837 $pcname = $extra_origs[$piece_ix-1]{Name}
838 $newdesc = "new upstream extra piece \`$pcname";
840 $ffwant->($prevpc, "previous upstream piece ($prevpc)",
841 $newpc, "newdesc ($newpc)");
843 push @upstream_merge_parents, $newpc;
845 my @cmd = @git, qw(read-tree);
846 if (defined $pcname) {
847 push @cmd, "-prefix=$pcname/";
848 runcmd @git, qw(rm --cached -f --ignore-unmatch), $pcname;
854 # index now contains the new upstream
856 die "upstreams not fast forward, stopping".
857 " (xxx should be an override option)";
861 # need to make the upstream subtree merge commit
862 my $us_tree = cmdoutput @git, qw(write-tree);
863 my @cmd = @git, qw(commit-tree), $us_tree;
867 die 'do we want to make ff from previous upstream comb?"';
869 push @cmd, qw(-p), $_ foreach @upstream_merge_parents;
870 push @cmd, qw(-m), "Combine upstreams for $new_upstream_version";
872 "[git-debrebase combine-upstreams . ".
873 (join " ", map { $_->{Name} } @extra_upstreams)."]";
874 my $combined = cmdoutput @cmd;
882 xxx check new orig version is reasonable;
883 xxx decorate new orig version to get new debian version;
886 sub cmd_downstream_rebase_launder_v0 () {
887 badusage "needs 1 argument, the baseline" unless @ARGV==1;
889 $base = git_rev_parse $base;
890 my $old_head = get_head();
891 my $current = $old_head;
894 if ($current eq $base) {
895 $topmost_keep //= $current;
896 print " $current BASE stop\n";
899 my $cl = classify $current;
900 print " $current $cl->{Type}";
902 my $p0 = $cl->{Parents}[0]{CommitId};
904 if ($cl->{Type} eq 'Pseudomerge') {
905 print " ^".($cl->{Contributor}{Ix}+1);
906 $next = $cl->{Contributor}{CommitId};
907 } elsif ($cl->{Type} eq 'AddPatches' or
908 $cl->{Type} eq 'Changelog') {
918 $topmost_keep //= $current;
920 die "to-be stripped changes not on top of the branch\n"
925 if ($topmost_keep eq $old_head) {
928 print "updating to $topmost_keep\n";
930 $old_head, $topmost_keep,
931 'downstream-rebase-launder-v0';
935 GetOptions("D+" => \$debuglevel) or die badusage "bad options\n";
936 initdebug('git-debrebase ');
937 enabledebug if $debuglevel;
939 my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);
940 chdir $toplevel or die "chdir $toplevel: $!";
942 $rd = fresh_playground "$playprefix/misc";
944 my $cmd = shift @ARGV;
947 $cmdfn = ${*::}{"cmd_$cmdfn"};
949 $cmdfn or badusage "unknown git-debrebase sub-operation $cmd";