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);
124 die "bad usage: $m\n";
130 my @cmd = qw(git config -z);
131 push @cmd, qw(--get-all) if wantarray;
133 my $out = cmdoutput @cmd;
134 return split /\0/, $out;
141 my $data = git_cat_file $objid, 'commit';
142 $data =~ m/(?<=\n)\n/ or die "$objid ($data) ?";
146 sub D_UPS () { 0x02; } # upstream files
147 sub D_PAT_ADD () { 0x04; } # debian/patches/ extra patches at end
148 sub D_PAT_OTH () { 0x08; } # debian/patches other changes
149 sub D_DEB_CLOG () { 0x10; } # debian/ (not patches/ or changelog)
150 sub D_DEB_OTH () { 0x20; } # debian/changelog
151 sub DS_DEB () { D_DEB_CLOG | D_DEB_OTH; } # debian/ (not patches/)
153 our $playprefix = 'debrebase';
159 sub in_workarea ($) {
162 my $r = eval { $sub->(); };
163 { local $@; changedir $maindir; }
167 sub fresh_workarea () {
168 $workarea = fresh_playground "$playprefix/work";
169 in_workarea sub { playtree_setup };
172 sub get_differs ($$) {
174 # This resembles quiltify_trees_differ, in dgit, a bit.
175 # But we don't care about modes, or dpkg-source-unrepresentable
176 # changes, and we don't need the plethora of different modes.
177 # Conversely we need to distinguish different kinds of changes to
178 # debian/ and debian/patches/.
183 my ($opts, $limits, $fn) = @_;
184 my @cmd = (@git, qw(diff-tree -z --no-renames));
186 push @cmd, "$_:" foreach $x, $y;
188 my $diffs = cmdoutput @cmd;
189 foreach (split /\0/, $diffs) { $fn->(); }
192 $rundiff->([qw(--name-only)], [], sub {
193 $differs |= $_ eq 'debian' ? DS_DEB : D_UPS;
196 if ($differs & DS_DEB) {
198 $rundiff->([qw(--name-only -r)], [qw(debian)], sub {
200 m{^debian/patches/} ? D_PAT_OTH :
201 $_ eq 'debian/changelog' ? D_DEB_CLOG :
204 die "mysterious debian changes $x..$y"
205 unless $differs & (D_PAT_OTH|DS_DEB);
208 if ($differs & D_PAT_OTH) {
210 $differs &= ~D_PAT_OTH;
212 $differs |= D_PAT_OTH;
213 no warnings qw(exiting); last;
215 $rundiff->([qw(--name-status -r)], [qw(debian/patches/)], sub {
216 no warnings qw(exiting);
217 if (!defined $mode) {
220 die unless s{^debian/patches/}{};
222 if ($mode eq 'A' && !m/\.series$/s) {
224 } elsif ($mode eq 'M' && $_ eq 'series') {
225 my $x_s = git_cat_file "$x:debian/patches/series", 'blob';
226 my $y_s = git_cat_file "$y:debian/patches/series", 'blob';
227 chomp $x_s; $x_s .= "\n";
228 $ok = $x_s eq substr($y_s, 0, length $x_s);
233 $differs |= $ok ? D_PAT_ADD : D_PAT_OTH;
235 die "mysterious debian/patches changes $x..$y"
236 unless $differs & (D_PAT_ADD|D_PAT_OTH);
239 printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
244 sub commit_pr_info ($) {
246 return Data::Dumper->dump([$r], [qw(commit)]);
249 sub calculate_committer_authline () {
250 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
251 'DUMMY COMMIT (git-debrebase)', "HEAD:";
252 my ($h,$m) = get_commit $c;
253 $h =~ m/^committer .*$/m or confess "($h) ?";
257 sub rm_subdir_cached ($) {
259 runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
262 sub read_tree_subdir ($$) {
263 my ($subdir, $new_tree_object) = @_;
264 rm_subdir_cached $subdir;
265 runcmd @git, qw(read-tree), "--prefix=$subdir/";
268 # classify returns an info hash like this
270 # Hdr => # commit headers, including 1 final newline
271 # Msg => # commit message (so one newline is dropped)
273 # Type => (see below)
275 # Ix => $index # ie 0, 1, 2, ...
277 # Differs => return value from get_differs
279 # IsDggitImport => 'orig' 'tarball' 'unpatched' 'package' (as from dgit)
281 # NewMsg => # commit message, but with any [dgit import ...] edited
282 # # to say "[was: ...]"
293 # has additional entres in classification result
294 # Overwritten = [ subset of Parents ]
295 # Contributor = $the_remaining_Parent
297 # DgitImportUnpatched
298 # has additional entry in classification result
299 # OrigParents = [ subset of Parents ]
301 # BreakwaterUpstreamMerge
302 # has additional entry in classification result
303 # OrigParents = [ subset of Parents ] # singleton list
305 sub parsecommit ($;$) {
306 my ($objid, $p_ref) = @_;
307 # => hash with CommitId Hdr Msg Tree Parents
308 # Parents entries have only Ix CommitId
309 # $p_ref, if provided, must be [] and is used as a base for Parents
314 my ($h,$m) = get_commit $objid;
316 my ($t) = $h =~ m/^tree (\w+)$/m or die $objid;
317 my (@ph) = $h =~ m/^parent (\w+)$/mg;
327 foreach my $ph (@ph) {
341 my $r = parsecommit($objid, \@p);
345 $p->{Differs} => (get_differs $p->{CommitId}, $t),
348 printdebug "classify $objid \$t=$t \@p",
349 (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
353 my ($type, @rest) = @_;
354 $r = { %$r, Type => $type, @rest };
356 my $dd = new Data::Dumper [ $r ];
357 Terse $dd 1; Indent $dd 0; Useqq $dd 1;
358 printdebug " = $type ".(Dump $dd)."\n";
364 $r = { %$r, Type => qw(Unknown) };
365 printdebug " ** Unknown\n";
370 my $d = $r->{Parents}[0]{Differs};
371 if ($d == D_PAT_ADD) {
372 return $classify->(qw(AddPatches));
373 } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
374 return $unknown->("edits debian/patches");
375 } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
376 my ($ty,$dummy) = git_cat_file "$ph[0]:debian";
378 if ($d == D_DEB_CLOG) {
379 return $classify->(qw(Changelog));
381 return $classify->(qw(Packaging));
383 } elsif ($ty eq 'missing') {
384 return $classify->(qw(BreakwaterStart));
386 return $unknown->("parent's debian is not a directory");
388 } elsif ($d == D_UPS) {
389 return $classify->(qw(Upstream));
390 } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
391 return $classify->(qw(Mixed));
393 return $unknown->("no changes");
395 confess "internal error $objid ?";
399 return $unknown->("origin commit");
402 my @identical = grep { !$_->{Differs} } @p;
403 if (@p == 2 && @identical == 1) {
404 my @overwritten = grep { $_->{Differs} } @p;
405 confess "internal error $objid ?" unless @overwritten==1;
406 return $classify->(qw(Pseudomerge),
407 Overwritten => $overwritten[0],
408 Contributor => $identical[0]);
410 if (@p == 2 && @identical == 2) {
411 my @bytime = nsort_by {
412 my ($ph,$pm) = get_commit $_->{CommitId};
413 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
416 return $classify->(qw(Pseudomerge),
417 SubType => qw(Ambiguous),
418 Overwritten => $bytime[0],
419 Contributor => $bytime[1]);
422 my ($p_h, $p_m) = get_commit $p->{CommitId};
423 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
424 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
426 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
428 if (!(grep { !$_->{IsOrigin} } @p) and
429 (@orig_ps >= @p - 1) and
430 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
432 return $classify->(qw(DgitImportUnpatched),
433 OrigParents => \@orig_ps);
436 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
437 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
439 # How to decide about l/r ordering of breakwater merges ? git
440 # --topo-order prefers to expand 2nd parent first. There's
441 # already an easy rune to look for debian/ history anyway (git log
442 # debian/) so debian breakwater branch should be 1st parent; that
443 # way also there's also an easy rune to look for the upstream
444 # patches (--topo-order).
446 # The above tells us which way *we* will generate them. But we
447 # might encounter ad-hoc breakwater merges generated manually,
448 # which might be the other way around. In principle, in some odd
449 # situations, a breakwater merge might have two identical parents.
450 # In that case we guess which way round it is (ie, which parent
451 # has the upstream history). The order of the 2-iteration loop
452 # controls which guess we make.
454 foreach my $prevbrw (qw(0 1)) {
457 !$p[$prevbrw]{IsOrigin} && # breakwater never starts with an origin
458 !($p[$prevbrw]{Differs} & ~DS_DEB) &&
459 !($p[!$prevbrw]{Differs} & ~D_UPS)) {
460 return $classify->(qw(BreakwaterUpstreamMerge),
461 OrigParents => [ $p[!$prevbrw] ]);
465 # multi-orig upstreams are represented with a breakwater merge
466 # from a single upstream commit which combines the orig tarballs
468 return $unknown->("complex merge");
474 $nogenerate,$report) = @_;
475 # => ($tip, $breakwater_tip)
476 # (or nothing, if $nogenerate)
478 # go through commits backwards
479 # we generate two lists of commits to apply:
480 # breakwater branch and upstream patches
481 my (@brw_cl, @upp_cl, @processed);
488 my ($appendinfo) = @_;
491 $ms .= "\n\n[git-debrebase $appendinfo]\n";
494 my $rewrite_from_here = sub {
495 my $sp_cl = { SpecialMethod => 'StartRewrite' };
496 push @brw_cl, $sp_cl;
497 push @processed, $sp_cl;
502 my $prprdelim = sub { print $report $prdelim if $report; $prdelim=""; };
505 return unless $report;
506 print $report $prdelim, @_;
510 my $bomb = sub { # usage: return $bomb->();
511 print $report " Unprocessable" if $report;
514 return (undef,undef);
516 die "commit $cur: Cannot cope with this commit (d.".
517 (join ' ', map { sprintf "%#x", $_->{Differs} }
518 @{ $cl->{Parents} }). ")";
524 my $build_start = sub {
525 my ($msg, $parent) = @_;
528 no warnings qw(exiting); last;
533 my $ty = $cl->{Type};
534 my $st = $cl->{SubType};
535 $prline->("$cl->{CommitId} $cl->{Type}");
536 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
537 push @processed, $cl;
538 my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
539 if ($ty eq 'AddPatches') {
541 $rewrite_from_here->();
543 } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
547 } elsif ($ty eq 'BreakwaterStart') {
548 $build_start->('FirstPackaging', $cur);
549 } elsif ($ty eq 'Upstream') {
553 } elsif ($ty eq 'Mixed') {
556 my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
559 $queue->(\@brw_cl, "debian");
560 $queue->(\@upp_cl, "upstream");
561 $rewrite_from_here->();
564 } elsif ($ty eq 'Pseudomerge') {
565 my $contrib = $cl->{Contributor}{CommitId};
566 print $report " Contributor=$contrib" if $report;
567 push @pseudomerges, $cl;
568 $rewrite_from_here->();
571 } elsif ($ty eq 'BreakwaterUpstreamMerge') {
572 $build_start->("PreviousBreakwater", $cur);
573 } elsif ($ty eq 'DgitImportUnpatched') {
574 my $pm = $pseudomerges[-1];
576 # To an extent, this is heuristic. Imports don't have
577 # a useful history of the debian/ branch. We assume
578 # that the first pseudomerge after an import has a
579 # useful history of debian/, and ignore the histories
580 # from later pseudomerges. Often the first pseudomerge
581 # will be the dgit import of the upload to the actual
582 # suite intended by the non-dgit NMUer, and later
583 # pseudomerges may represent in-archive copies.
584 my $ovwrs = $pm->{Overwritten};
585 printf $report " PM=%s \@Overwr:%d", $pm, (scalar @$ovwrs)
590 my $ovwr = $ovwrs->[0]{CommitId};
591 printf $report " Overwr=%s", $ovwr if $report;
592 # This import has a tree which is just like a
593 # breakwater tree, but it has the wrong history. It
594 # ought to have the previous breakwater (which the
595 # pseudomerge overwrote) as an ancestor. That will
596 # make the history of the debian/ files correct. As
597 # for the upstream version: either it's the same as
598 # was ovewritten (ie, same as the previous
599 # breakwater), in which case that history is precisely
600 # right; or, otherwise, it was a non-gitish upload of a
601 # new upstream version. We can tell these apart by
602 # looking at the tree of the supposed upstream.
605 SpecialMethod => 'DgitImportDebianUpdate',
606 $xmsg->("convert dgit import: debian changes")
608 my $differs = (get_differs $ovwr, $cl->{Tree});
609 printf $report " Differs=%#x", $differs if $report;
610 if ($differs & D_UPS) {
611 printf $report " D_UPS" if $report;
612 # This will also trigger if a non-dgit git-based NMU
613 # deleted .gitignore (which is a thing that some of
614 # the existing git tools do if the user doesn't
615 # somehow tell them not to). Ah well.
618 SpecialMethod => 'DgitImportUpstreamUpdate',
619 $xmsg->("convert dgit import: upstream changes")
622 $prline->(" Import");
623 $rewrite_from_here->();
624 $upp_limit //= $#upp_cl; # further, deeper, patches discarded
628 # Everything is from this import. This kind of import
629 # is already in valid breakwater format, with the
630 # patches as commits.
631 printf $report " NoPM" if $report;
632 # last thing we processed will have been the first patch,
633 # if there is one; which is fine, so no need to rewrite
634 # on account of this import
635 $build_start->("ImportOrigin", $cur);
643 return if $nogenerate;
645 # Now we build it back up again
651 my $read_tree_debian = sub {
653 read_tree_subdir 'debian', "$treeish:debian";
655 my $read_tree_upstream = sub {
657 runcmd @git, qw(read-tree), $treeish;
658 $read_tree_debian->($build);
661 my $committer_authline = calculate_committer_authline();
663 printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
665 confess "internal error" unless $build eq (pop @processed)->{CommitId};
668 mkdir $rd or $!==EEXIST or die $!;
670 runcmd @git, qw(read-tree), $build;
671 foreach my $cl (qw(Debian), (reverse @brw_cl),
672 { SpecialMethod => 'RecordBreakwaterTip' },
673 qw(Upstream), (reverse @upp_cl)) {
675 $current_method = $cl;
678 my $method = $cl->{SpecialMethod} // $current_method;
679 my @parents = ($build);
680 my $cltree = $cl->{CommitId};
681 printdebug "WALK BUILD ".($cltree//'undef').
682 " $method (rewriting=$rewriting)\n";
683 if ($method eq 'Debian') {
684 $read_tree_debian->($cltree);
685 } elsif ($method eq 'Upstream') {
686 $read_tree_upstream->($cltree);
687 } elsif ($method eq 'StartRewrite') {
690 } elsif ($method eq 'RecordBreakwaterTip') {
691 $breakwater = $build;
693 } elsif ($method eq 'DgitImportDebianUpdate') {
694 $read_tree_debian->($cltree);
695 rm_subdir_cached qw(debian/patches);
696 } elsif ($method eq 'DgitImportUpstreamUpdate') {
697 $read_tree_upstream->($cltree);
698 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
703 my $procd = (pop @processed) // 'UNDEF';
706 printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
709 my $newtree = cmdoutput @git, qw(write-tree);
711 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
712 $ch =~ s{^parent .*\n}{}m;
713 $ch =~ s{(?=^author)}{
714 join '', map { "parent $_\n" } @parents
715 }me or confess "$ch ?";
717 $ch =~ s{^committer .*$}{$committer_authline}m
720 my $cf = "$rd/m$rewriting";
721 open CD, ">", $cf or die $!;
722 print CD $ch, "\n", $cl->{Msg} or die $!;
724 my @cmd = (@git, qw(hash-object));
725 push @cmd, qw(-w) if $rewriting;
726 push @cmd, qw(-t commit), $cf;
727 my $newcommit = cmdoutput @cmd;
728 confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
733 my $final_check = get_differs $build, $input;
734 die sprintf "internal error %#x %s %s", $final_check, $build, $input
735 if $final_check & ~D_PAT_ADD;
737 return ($build, $breakwater);
740 sub get_head () { return git_rev_parse qw(HEAD); }
742 sub update_head ($$$) {
743 my ($old, $new, $mrest) = @_;
744 runcmd @git, qw(update-ref -m), "debrebase: $mrest", 'HEAD', $new, $old;
747 sub update_head_checkout ($$$) {
748 my ($old, $new, $mrest) = @_;
749 my $symref = git_get_symref();
750 runcmd @git, qw(checkout), $new, qw(.);
751 update_head $old, $new, $mrest;
755 badusage "no arguments to launder allowed" if @ARGV;
756 my $old = get_head();
757 my ($tip,$breakwater) = walk $old;
758 update_head $old, $tip, 'launder';
759 # no tree changes except debian/patches
760 runcmd @git, qw(rm --quiet --ignore-unmatch -rf debian/patches);
761 printf "# breakwater tip\n%s\n", $breakwater;
762 printf "# working tip\n%s\n", $tip;
766 die if ($ARGV[0]//'') =~ m/^-/;
767 badusage "too many arguments to analyse" if @ARGV>1;
770 $old = git_rev_parse $old;
774 my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
775 STDOUT->error and die $!;
778 sub cmd_new_upstream_v0 () {
780 "need NEW-VERSION UPS-COMMITISH [EXTRA-UPS-NAME EXTRA-UPS-COMMITISH...]"
781 unless @ARGV % 2 == 0 and @ARGV >= 2;
782 # tree should be clean and this is not checked
783 # automatically and unconditionally launders before rebasing
784 # if rebase --abort is used, laundering has still been done
788 # parse args - low commitment
789 my $new_version = (new Dpkg::Version scalar(shift @ARGV), 1);
790 my $new_upstream_version = $new_version->version();
792 my $new_upstream = git_rev_parse shift @ARGV;
795 my ($n, @x) = @_; # may be ''
796 my $pc = $pieces{$n} //= {
798 Desc => ($n ? "upstream piece $n" : "upstream (main piece"),
800 while (my $k = shift @x) { $pc->{$k} = shift @x; }
805 my ($n, @x) = @_; # may be ''
806 $pc = $piece->($n, @x, NewIx => (scalar @newpieces));
807 push @newpieces, $pc;
812 New => $new_upstream,
816 my $c = git_rev_parse shift @ARGV;
817 die unless $n =~ m/^$extra_orig_namepart_re$/;
818 $newpiece->($n, New => $c);
821 # now we need to investigate the branch this generates the
822 # laundered version but we don't switch to it yet
823 my $old = get_head();
824 my ($laundered_tip,$breakwater) = walk $old;
826 my $breakwater_cl = classify $breakwater;
827 my $old_orig = parsecommit $breakwater_cl->{OrigParents}[0]{CommitId};
833 print STDERR "preflight check failed: $msg\n";
836 $piece->('', Old => $old_orig);
838 if ($old_orig_ci =~ m{^\[git-debrebase }m) {
840 m{^\[git-debrebase new-upstream combine \.((?: $extra_orig_namepart_re)+)\]}
842 my @oldpieces = ('', split / /, $1);
843 my $parentix = -1 + scalar @{ $old_orig->{Parents} };
844 foreach my $i (0..$#$oldpieces) {
845 my $n = $oldpieces[$i];
846 $piece->($n, Old => $old_orig.'^'.$parentix);
849 $problem->("previous upstream $old_orig->{CommitId} is from".
850 " git-debrebase but not a \`new-upstream combine' commit");
854 foreach my $pc (values %pieces) {
856 $problem->("introducing upstream piece $pc->{Name}");
857 } elsif (!$pc->{New}) {
858 $problem->("dropping upstream piece $pc->{Name}");
859 } elsif (!is_fast_fwd $pc->{Old}, $pc->{New}) {
860 $problem->("not fast forward: $pc->{Name} $pc->{Old}..$pc->{New}");
867 "preflight check failures (%d) overriden by --force\n",
871 "preflight check failures (%d) (you could --force)",
878 my @upstream_merge_parents;
881 push @upstream_merge_parents, $old_orig->{CommitId};
884 foreach my $pc (@newpieces) { # always has '' first
886 read_tree_subdir $pcname, $pc->{New];
888 runcmd @git, qw(read-tree), $pc->{New};
890 push @upstream_merge_parents, $pc->{New};
893 # index now contains the new upstream
895 if (@newpieces > 1) {
896 # need to make the upstream subtree merge commit
897 my $us_tree = cmdoutput @git, qw(write-tree);
898 my @cmd = (@git, qw(commit-tree), $us_tree);
899 push @cmd, qw(-p), $_ foreach @upstream_merge_parents;
900 push @cmd, qw(-m), "Combine upstreams for $new_upstream_version";
902 "[git-debrebase new-upstream combine . ".
903 (join " ", map { $_->{Name} } @newpieces[1..$#newpieces]).
905 $new_upstream = cmdoutput @cmd;
908 # $new_upstream is either the single upstream commit, or the
909 # combined commit we just made. Either way it will be the
910 # "upstream" parent of the breakwater special merge.
912 read_tree_subdir 'debian', "$breakwater:debian";
914 # index now contains the breakwater merge contents
916 my $bw_tree = cmdoutput @git, qw(write_tree);
917 my @cmd = (@git, qw(commit-tree), $bw_tree);
918 push @cmd, qw(-p), $breakwater, qw(-p), $new_upstream;
919 push @cmd, qw(-m), "Update to upstream $new_upstream_version";
921 "[git-debrebase new-upstream breakwater $new_upstream_version]";
922 my $new_bw = cmdoutput @git;
924 # Now we have to add a changelog stanza so the Debian version
927 die if unlink "debian";
928 die unless $!==ENOTEMPTY;
929 unlink "debian/changelog" or die $!;
930 open CN, ">", "debian/changelog" or die $!;
931 my $oldclog = git_cat_file ":debian/changelog";
932 $oldclog =~ m/^($package_re) \(\S+\) / or
933 fail "cannot parse old changelog to get package name";
935 print CN, <<END, $oldclog or die $!;
936 $p ($new_version) UNRELEASED; urgency=medium
938 * Update to new upstream version $new_upstream_version.
944 runcmd @git, qw(update-index --add --replace), 'debian/changelog';
946 # Now we have the final new breakwater branch in the index
948 $bw_tree = cmdoutput @git, qw(write_tree);
949 @cmd = (@git, qw(commit-tree), $bw_tree);
950 push @cmd, qw(-p), $new_bw;
952 "Update changelog for new upstream $new_upstream_version";
954 "[git-debrebase new-upstream changelog $new_upstream_version]";
955 $new_bw = cmdoutput @git;
959 xxx check new orig version is reasonable;
960 xxx decorate new orig version to get new debian version;
963 sub cmd_downstream_rebase_launder_v0 () {
964 badusage "needs 1 argument, the baseline" unless @ARGV==1;
966 $base = git_rev_parse $base;
967 my $old_head = get_head();
968 my $current = $old_head;
971 if ($current eq $base) {
972 $topmost_keep //= $current;
973 print " $current BASE stop\n";
976 my $cl = classify $current;
977 print " $current $cl->{Type}";
979 my $p0 = $cl->{Parents}[0]{CommitId};
981 if ($cl->{Type} eq 'Pseudomerge') {
982 print " ^".($cl->{Contributor}{Ix}+1);
983 $next = $cl->{Contributor}{CommitId};
984 } elsif ($cl->{Type} eq 'AddPatches' or
985 $cl->{Type} eq 'Changelog') {
995 $topmost_keep //= $current;
997 die "to-be stripped changes not on top of the branch\n"
1002 if ($topmost_keep eq $old_head) {
1003 print "unchanged\n";
1005 print "updating to $topmost_keep\n";
1006 update_head_checkout
1007 $old_head, $topmost_keep,
1008 'downstream-rebase-launder-v0';
1012 GetOptions("D+" => \$debuglevel,
1013 'force!') or die badusage "bad options\n";
1014 initdebug('git-debrebase ');
1015 enabledebug if $debuglevel;
1017 my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);
1018 chdir $toplevel or die "chdir $toplevel: $!";
1020 $rd = fresh_playground "$playprefix/misc";
1022 my $cmd = shift @ARGV;
1025 $cmdfn = ${*::}{"cmd_$cmdfn"};
1027 $cmdfn or badusage "unknown git-debrebase sub-operation $cmd";