3 # Script helping make fast-forwarding histories while still rebasing
4 # upstream deltas when working on Debian packaging
6 # Copyright (C)2017,2018 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/>.
24 # git-debrebase [<options>] new-upstream-v0 \
25 # <new-version> <orig-commitish> \
26 # [<extra-orig-name> <extra-orig-commitish> ...] \
27 # [<git-rebase options>...]
29 # git-debrebase [<options> --] [<git-rebase options...>]
30 # git-debrebase [<options>] analyse
31 # git-debrebase [<options>] launder # prints breakwater tip etc.
32 # git-debrebase [<options>] downstream-rebase-launder-v0 # experimental
34 # git-debrebase [<options>] gbp2debrebase-v0 \
37 # problems / outstanding questions:
39 # * dgit push with a `3.0 (quilt)' package means doing quilt
40 # fixup. Usually this involves recommitting the whole patch
41 # series, one at a time, with dpkg-source --commit. This is
42 # terribly terribly slow. (Maybe this should be fixed in dgit.)
44 # * dgit push usually needs to (re)make a pseudomerge. The "first"
45 # git-debrebase stripped out the previous pseudomerge and could
46 # have remembeed the HEAD. But it's not quite clear what history
47 # ought to be preserved and what should be discarded. For now
48 # the user will have to tell dgit --overwrite.
50 # To fix this, do we need a new push hook for dgit ?
52 # * Workflow is currently clumsy. Lots of spurious runes to type.
53 # There's not even a guide.
55 # * There are no tests.
57 # * new-upstream-v0 has a terrible UI. You end up with giant
58 # runic command lines.
60 # One consequence of the lack of richness it can need --force in
61 # fairly sensible situations and there is no way to tell it what
62 # you are really trying to do, other than just --force. There
63 # should be an interface with some default branch names.
65 # * There should be a standard convention for the version number,
66 # and unfinalised or not changelog, after new-upstream.
68 # * Handing of multi-orig dgit new-upstream .dsc imports is known to
69 # be broken. They may be not recognised, improperly converted, or
70 # their conversion may be unrecognised.
72 # * Docs need writing and updating. Even README.git-debrebase
73 # describes a design but may not reflect the implementation.
75 # * We need to develop a plausible model that works for derivatives,
76 # who probably want to maintain their stack on top of Debian's.
77 # downstream-rebase-launder-v0 may be a starting point?
81 use Debian::Dgit qw(:DEFAULT :playground);
88 use Getopt::Long qw(:config posix_default gnu_compat bundling);
95 die "bad usage: $m\n";
101 my @cmd = qw(git config -z);
102 push @cmd, qw(--get-all) if wantarray;
104 my $out = cmdoutput @cmd;
105 return split /\0/, $out;
112 my $dd = new Data::Dumper [ $v ];
113 Terse $dd 1; Indent $dd 0; Useqq $dd 1;
119 my $data = git_cat_file $objid, 'commit';
120 $data =~ m/(?<=\n)\n/ or die "$objid ($data) ?";
124 sub D_UPS () { 0x02; } # upstream files
125 sub D_PAT_ADD () { 0x04; } # debian/patches/ extra patches at end
126 sub D_PAT_OTH () { 0x08; } # debian/patches other changes
127 sub D_DEB_CLOG () { 0x10; } # debian/ (not patches/ or changelog)
128 sub D_DEB_OTH () { 0x20; } # debian/changelog
129 sub DS_DEB () { D_DEB_CLOG | D_DEB_OTH; } # debian/ (not patches/)
131 our $playprefix = 'debrebase';
137 sub in_workarea ($) {
140 my $r = eval { $sub->(); };
141 { local $@; changedir $maindir; }
145 sub fresh_workarea () {
146 $workarea = fresh_playground "$playprefix/work";
147 in_workarea sub { playtree_setup };
150 sub get_differs ($$) {
152 # This resembles quiltify_trees_differ, in dgit, a bit.
153 # But we don't care about modes, or dpkg-source-unrepresentable
154 # changes, and we don't need the plethora of different modes.
155 # Conversely we need to distinguish different kinds of changes to
156 # debian/ and debian/patches/.
161 my ($opts, $limits, $fn) = @_;
162 my @cmd = (@git, qw(diff-tree -z --no-renames));
164 push @cmd, "$_:" foreach $x, $y;
166 my $diffs = cmdoutput @cmd;
167 foreach (split /\0/, $diffs) { $fn->(); }
170 $rundiff->([qw(--name-only)], [], sub {
171 $differs |= $_ eq 'debian' ? DS_DEB : D_UPS;
174 if ($differs & DS_DEB) {
176 $rundiff->([qw(--name-only -r)], [qw(debian)], sub {
178 m{^debian/patches/} ? D_PAT_OTH :
179 $_ eq 'debian/changelog' ? D_DEB_CLOG :
182 die "mysterious debian changes $x..$y"
183 unless $differs & (D_PAT_OTH|DS_DEB);
186 if ($differs & D_PAT_OTH) {
188 $differs &= ~D_PAT_OTH;
190 $differs |= D_PAT_OTH;
191 no warnings qw(exiting); last;
193 $rundiff->([qw(--name-status -r)], [qw(debian/patches/)], sub {
194 no warnings qw(exiting);
195 if (!defined $mode) {
198 die unless s{^debian/patches/}{};
200 if ($mode eq 'A' && !m/\.series$/s) {
202 } elsif ($mode eq 'M' && $_ eq 'series') {
203 my $x_s = git_cat_file "$x:debian/patches/series", 'blob';
204 my $y_s = git_cat_file "$y:debian/patches/series", 'blob';
205 chomp $x_s; $x_s .= "\n";
206 $ok = $x_s eq substr($y_s, 0, length $x_s);
211 $differs |= $ok ? D_PAT_ADD : D_PAT_OTH;
213 die "mysterious debian/patches changes $x..$y"
214 unless $differs & (D_PAT_ADD|D_PAT_OTH);
217 printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
222 sub commit_pr_info ($) {
224 return Data::Dumper->dump([$r], [qw(commit)]);
227 sub calculate_committer_authline () {
228 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
229 'DUMMY COMMIT (git-debrebase)', "HEAD:";
230 my ($h,$m) = get_commit $c;
231 $h =~ m/^committer .*$/m or confess "($h) ?";
235 sub rm_subdir_cached ($) {
237 runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
240 sub read_tree_subdir ($$) {
241 my ($subdir, $new_tree_object) = @_;
242 rm_subdir_cached $subdir;
243 runcmd @git, qw(read-tree), "--prefix=$subdir/", $new_tree_object;
246 sub make_commit ($$) {
247 my ($parents, $message_paras) = @_;
248 my $tree = cmdoutput @git, qw(write-tree);
249 my @cmd = (@git, qw(commit-tree), $tree);
250 push @cmd, qw(-p), $_ foreach @$parents;
251 push @cmd, qw(-m), $_ foreach @$message_paras;
252 return cmdoutput @cmd;
255 # classify returns an info hash like this
257 # Hdr => # commit headers, including 1 final newline
258 # Msg => # commit message (so one newline is dropped)
260 # Type => (see below)
262 # Ix => $index # ie 0, 1, 2, ...
264 # Differs => return value from get_differs
266 # IsDggitImport => 'orig' 'tarball' 'unpatched' 'package' (as from dgit)
268 # NewMsg => # commit message, but with any [dgit import ...] edited
269 # # to say "[was: ...]"
280 # has additional entres in classification result
281 # Overwritten = [ subset of Parents ]
282 # Contributor = $the_remaining_Parent
284 # DgitImportUnpatched
285 # has additional entry in classification result
286 # OrigParents = [ subset of Parents ]
288 # BreakwaterUpstreamMerge
289 # has additional entry in classification result
290 # OrigParents = [ subset of Parents ] # singleton list
292 sub parsecommit ($;$) {
293 my ($objid, $p_ref) = @_;
294 # => hash with CommitId Hdr Msg Tree Parents
295 # Parents entries have only Ix CommitId
296 # $p_ref, if provided, must be [] and is used as a base for Parents
301 my ($h,$m) = get_commit $objid;
303 my ($t) = $h =~ m/^tree (\w+)$/m or die $objid;
304 my (@ph) = $h =~ m/^parent (\w+)$/mg;
314 foreach my $ph (@ph) {
316 Ix => scalar @$p_ref,
328 my $r = parsecommit($objid, \@p);
332 $p->{Differs} = (get_differs $p->{CommitId}, $t),
335 printdebug "classify $objid \$t=$t \@p",
336 (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
340 my ($type, @rest) = @_;
341 $r = { %$r, Type => $type, @rest };
343 printdebug " = $type ".(dd $r)."\n";
349 $r = { %$r, Type => qw(Unknown) };
350 printdebug " ** Unknown\n";
355 my $d = $r->{Parents}[0]{Differs};
356 if ($d == D_PAT_ADD) {
357 return $classify->(qw(AddPatches));
358 } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
359 return $unknown->("edits debian/patches");
360 } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
361 my ($ty,$dummy) = git_cat_file "$p[0]{CommitId}:debian";
363 if ($d == D_DEB_CLOG) {
364 return $classify->(qw(Changelog));
366 return $classify->(qw(Packaging));
368 } elsif ($ty eq 'missing') {
369 return $classify->(qw(BreakwaterStart));
371 return $unknown->("parent's debian is not a directory");
373 } elsif ($d == D_UPS) {
374 return $classify->(qw(Upstream));
375 } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
376 return $classify->(qw(Mixed));
378 return $unknown->("no changes");
380 confess "internal error $objid ?";
384 return $unknown->("origin commit");
387 my @identical = grep { !$_->{Differs} } @p;
388 if (@p == 2 && @identical == 1) {
389 my @overwritten = grep { $_->{Differs} } @p;
390 confess "internal error $objid ?" unless @overwritten==1;
391 return $classify->(qw(Pseudomerge),
392 Overwritten => $overwritten[0],
393 Contributor => $identical[0]);
395 if (@p == 2 && @identical == 2) {
396 my @bytime = nsort_by {
397 my ($ph,$pm) = get_commit $_->{CommitId};
398 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
401 return $classify->(qw(Pseudomerge),
402 SubType => qw(Ambiguous),
403 Overwritten => $bytime[0],
404 Contributor => $bytime[1]);
407 my ($p_h, $p_m) = get_commit $p->{CommitId};
408 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
409 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
411 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
413 if (!(grep { !$_->{IsOrigin} } @p) and
414 (@orig_ps >= @p - 1) and
415 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
417 return $classify->(qw(DgitImportUnpatched),
418 OrigParents => \@orig_ps);
421 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
422 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
424 # How to decide about l/r ordering of breakwater merges ? git
425 # --topo-order prefers to expand 2nd parent first. There's
426 # already an easy rune to look for debian/ history anyway (git log
427 # debian/) so debian breakwater branch should be 1st parent; that
428 # way also there's also an easy rune to look for the upstream
429 # patches (--topo-order).
431 # The above tells us which way *we* will generate them. But we
432 # might encounter ad-hoc breakwater merges generated manually,
433 # which might be the other way around. In principle, in some odd
434 # situations, a breakwater merge might have two identical parents.
435 # In that case we guess which way round it is (ie, which parent
436 # has the upstream history). The order of the 2-iteration loop
437 # controls which guess we make.
439 foreach my $prevbrw (qw(0 1)) {
442 !$p[$prevbrw]{IsOrigin} && # breakwater never starts with an origin
443 !($p[!$prevbrw]{Differs} & ~DS_DEB) && # no non-debian changess
444 !($p[$prevbrw]{Differs} & ~D_UPS)) { # no non-upstream changes
445 return $classify->(qw(BreakwaterUpstreamMerge),
446 OrigParents => [ $p[!$prevbrw] ]);
450 # multi-orig upstreams are represented with a breakwater merge
451 # from a single upstream commit which combines the orig tarballs
453 return $unknown->("complex merge");
459 $nogenerate,$report) = @_;
460 # => ($tip, $breakwater_tip, $last_upstream_merge_in_breakwater)
461 # (or nothing, if $nogenerate)
463 printdebug "*** WALK $input ".($nogenerate//0)." ".($report//'-')."\n";
465 # go through commits backwards
466 # we generate two lists of commits to apply:
467 # breakwater branch and upstream patches
468 my (@brw_cl, @upp_cl, @processed);
475 my ($appendinfo) = @_;
478 $ms .= "\n\n[git-debrebase $appendinfo]\n";
481 my $rewrite_from_here = sub {
482 my $sp_cl = { SpecialMethod => 'StartRewrite' };
483 push @brw_cl, $sp_cl;
484 push @processed, $sp_cl;
489 my $prprdelim = sub { print $report $prdelim if $report; $prdelim=""; };
492 return unless $report;
493 print $report $prdelim, @_;
497 my $bomb = sub { # usage: return $bomb->();
498 print $report " Unprocessable" if $report;
501 return (undef,undef);
503 die "commit $cur: Cannot cope with this commit (d.".
504 (join ' ', map { sprintf "%#x", $_->{Differs} }
505 @{ $cl->{Parents} }). ")";
511 my $build_start = sub {
512 my ($msg, $parent) = @_;
515 no warnings qw(exiting); last;
518 my $last_upstream_update;
522 my $ty = $cl->{Type};
523 my $st = $cl->{SubType};
524 $prline->("$cl->{CommitId} $cl->{Type}");
525 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
526 push @processed, $cl;
527 my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
528 if ($ty eq 'AddPatches') {
530 $rewrite_from_here->();
532 } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
536 } elsif ($ty eq 'BreakwaterStart') {
537 $last_upstream_update = $cur;
538 $build_start->('FirstPackaging', $cur);
539 } elsif ($ty eq 'Upstream') {
543 } elsif ($ty eq 'Mixed') {
546 my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
549 $queue->(\@brw_cl, "debian");
550 $queue->(\@upp_cl, "upstream");
551 $rewrite_from_here->();
554 } elsif ($ty eq 'Pseudomerge') {
555 my $contrib = $cl->{Contributor}{CommitId};
556 print $report " Contributor=$contrib" if $report;
557 push @pseudomerges, $cl;
558 $rewrite_from_here->();
561 } elsif ($ty eq 'BreakwaterUpstreamMerge') {
562 $last_upstream_update = $cur;
563 $build_start->("PreviousBreakwater", $cur);
564 } elsif ($ty eq 'DgitImportUnpatched') {
565 my $pm = $pseudomerges[-1];
567 # To an extent, this is heuristic. Imports don't have
568 # a useful history of the debian/ branch. We assume
569 # that the first pseudomerge after an import has a
570 # useful history of debian/, and ignore the histories
571 # from later pseudomerges. Often the first pseudomerge
572 # will be the dgit import of the upload to the actual
573 # suite intended by the non-dgit NMUer, and later
574 # pseudomerges may represent in-archive copies.
575 my $ovwrs = $pm->{Overwritten};
576 printf $report " PM=%s \@Overwr:%d", $pm, (scalar @$ovwrs)
579 printdebug "*** WALK BOMB DgitImportUnpatched\n";
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
617 die 'BUG $upp_limit is not used anywhere?';
621 # Everything is from this import. This kind of import
622 # is already in valid breakwater format, with the
623 # patches as commits.
624 printf $report " NoPM" if $report;
625 # last thing we processed will have been the first patch,
626 # if there is one; which is fine, so no need to rewrite
627 # on account of this import
628 $build_start->("ImportOrigin", $cur);
632 printdebug "*** WALK BOMB unrecognised\n";
638 printdebug "*** WALK prep done cur=$cur".
639 " brw $#brw_cl upp $#upp_cl proc $#processed pm $#pseudomerges\n";
641 return if $nogenerate;
643 # Now we build it back up again
649 my $read_tree_debian = sub {
651 read_tree_subdir 'debian', "$treeish:debian";
652 rm_subdir_cached 'debian/patches';
654 my $read_tree_upstream = sub {
656 runcmd @git, qw(read-tree), $treeish;
657 $read_tree_debian->($build);
660 my $committer_authline = calculate_committer_authline();
662 printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
664 confess "internal error" unless $build eq (pop @processed)->{CommitId};
667 mkdir $rd or $!==EEXIST or die $!;
669 runcmd @git, qw(read-tree), $build;
670 foreach my $cl (qw(Debian), (reverse @brw_cl),
671 { SpecialMethod => 'RecordBreakwaterTip' },
672 qw(Upstream), (reverse @upp_cl)) {
674 $current_method = $cl;
677 my $method = $cl->{SpecialMethod} // $current_method;
678 my @parents = ($build);
679 my $cltree = $cl->{CommitId};
680 printdebug "WALK BUILD ".($cltree//'undef').
681 " $method (rewriting=$rewriting)\n";
682 if ($method eq 'Debian') {
683 $read_tree_debian->($cltree);
684 } elsif ($method eq 'Upstream') {
685 $read_tree_upstream->($cltree);
686 } elsif ($method eq 'StartRewrite') {
689 } elsif ($method eq 'RecordBreakwaterTip') {
690 $breakwater = $build;
692 } elsif ($method eq 'DgitImportDebianUpdate') {
693 $read_tree_debian->($cltree);
694 rm_subdir_cached qw(debian/patches);
695 } elsif ($method eq 'DgitImportUpstreamUpdate') {
696 $read_tree_upstream->($cltree);
697 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
702 my $procd = (pop @processed) // 'UNDEF';
705 printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
708 my $newtree = cmdoutput @git, qw(write-tree);
710 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
711 $ch =~ s{^parent .*\n}{}m;
712 $ch =~ s{(?=^author)}{
713 join '', map { "parent $_\n" } @parents
714 }me or confess "$ch ?";
716 $ch =~ s{^committer .*$}{$committer_authline}m
719 my $cf = "$rd/m$rewriting";
720 open CD, ">", $cf or die $!;
721 print CD $ch, "\n", $cl->{Msg} or die $!;
723 my @cmd = (@git, qw(hash-object));
724 push @cmd, qw(-w) if $rewriting;
725 push @cmd, qw(-t commit), $cf;
726 my $newcommit = cmdoutput @cmd;
727 confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
729 if (grep { $method eq $_ } qw(DgitImportUpstreamUpdate)) {
730 $last_upstream_update = $cur;
735 my $final_check = get_differs $build, $input;
736 die sprintf "internal error %#x %s %s", $final_check, $build, $input
737 if $final_check & ~D_PAT_ADD;
739 my @r = ($build, $breakwater, $last_upstream_update);
740 printdebug "*** WALK RETURN @r\n";
744 sub get_head () { return git_rev_parse qw(HEAD); }
746 sub update_head ($$$) {
747 my ($old, $new, $mrest) = @_;
748 runcmd @git, qw(update-ref -m), "debrebase: $mrest", 'HEAD', $new, $old;
751 sub update_head_checkout ($$$) {
752 my ($old, $new, $mrest) = @_;
753 my $symref = git_get_symref();
754 runcmd @git, qw(checkout), $new, qw(.);
755 update_head $old, $new, $mrest;
758 sub update_head_postlaunder ($$$) {
759 my ($old, $tip, $reflogmsg) = @_;
760 return if $tip eq $old;
761 print "git-debrebase: laundered (head was $old)\n";
762 update_head $old, $tip, $reflogmsg;
763 # no tree changes except debian/patches
764 runcmd @git, qw(rm --quiet --ignore-unmatch -rf debian/patches);
768 badusage "no arguments to launder allowed" if @ARGV;
769 my $old = get_head();
770 my ($tip,$breakwater,$last_upstream_merge) = walk $old;
771 update_head_postlaunder $old, $tip, 'launder';
772 printf "# breakwater tip\n%s\n", $breakwater;
773 printf "# working tip\n%s\n", $tip;
774 printf "# last upstream merge\n%s\n", $last_upstream_merge;
777 sub defaultcmd_rebase () {
778 my $old = get_head();
779 my ($tip,$breakwater) = walk $old;
780 update_head_postlaunder $old, $tip, 'launder for rebase';
781 @ARGV = qw(-i) unless @ARGV; # make configurable
782 runcmd @git, qw(rebase), @ARGV, $breakwater;
786 die if ($ARGV[0]//'') =~ m/^-/;
787 badusage "too many arguments to analyse" if @ARGV>1;
790 $old = git_rev_parse $old;
794 my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
795 STDOUT->error and die $!;
798 sub cmd_new_upstream_v0 () {
799 # tree should be clean and this is not checked
800 # automatically and unconditionally launders before rebasing
801 # if rebase --abort is used, laundering has still been done
805 badusage "need NEW-VERSION UPS-COMMITTISH" unless @ARGV >= 2;
807 # parse args - low commitment
808 my $new_version = (new Dpkg::Version scalar(shift @ARGV), check => 1);
809 my $new_upstream_version = $new_version->version();
811 my $new_upstream = git_rev_parse shift @ARGV;
814 my ($n, @x) = @_; # may be ''
815 my $pc = $pieces{$n} //= {
817 Desc => ($n ? "upstream piece \`$n'" : "upstream (main piece"),
819 while (my $k = shift @x) { $pc->{$k} = shift @x; }
825 my ($n, @x) = @_; # may be ''
826 my $pc = $piece->($n, @x, NewIx => (scalar @newpieces));
827 push @newpieces, $pc;
832 New => $new_upstream,
834 while (@ARGV && $ARGV[0] !~ m{^-}) {
837 badusage "for each EXTRA-UPS-NAME need EXTRA-UPS-COMMITISH"
838 unless @ARGV && $ARGV[0] !~ m{^-};
840 my $c = git_rev_parse shift @ARGV;
841 die unless $n =~ m/^$extra_orig_namepart_re$/;
842 $newpiece->($n, New => $c);
845 # now we need to investigate the branch this generates the
846 # laundered version but we don't switch to it yet
847 my $old_head = get_head();
848 my ($old_laundered_tip,$old_bw,$old_upstream_update) = walk $old_head;
850 my $old_bw_cl = classify $old_bw;
851 my $old_upstream_update_cl = classify $old_upstream_update;
852 confess unless $old_upstream_update_cl->{OrigParents};
853 my $old_upstream = parsecommit
854 $old_upstream_update_cl->{OrigParents}[0]{CommitId};
860 print STDERR "preflight check failed: $msg\n";
863 $piece->('', Old => $old_upstream->{CommitId});
865 if ($old_upstream->{Msg} =~ m{^\[git-debrebase }m) {
866 if ($old_upstream->{Msg} =~
867 m{^\[git-debrebase new-upstream combine \.((?: $extra_orig_namepart_re)+)\]}
869 my @oldpieces = ('', split / /, $1);
870 my $parentix = -1 + scalar @{ $old_upstream->{Parents} };
871 foreach my $i (0..$#oldpieces) {
872 my $n = $oldpieces[$i];
873 $piece->($n, Old => $old_upstream->{CommitId}.'^'.$parentix);
876 $problem->("previous upstream $old_upstream->{CommitId} is from".
877 " git-debrebase but not a \`new-upstream combine' commit");
881 foreach my $pc (values %pieces) {
883 $problem->("introducing upstream piece \`$pc->{Name}'");
884 } elsif (!$pc->{New}) {
885 $problem->("dropping upstream piece \`$pc->{Name}'");
886 } elsif (!is_fast_fwd $pc->{Old}, $pc->{New}) {
887 $problem->("not fast forward: $pc->{Name} $pc->{Old}..$pc->{New}");
891 printdebug "%pieces = ", (dd \%pieces), "\n";
892 printdebug "\@newpieces = ", (dd \@newpieces), "\n";
897 "preflight check failures (%d) overriden by --force\n",
901 "preflight check failures (%d) (you could --force)",
910 my @upstream_merge_parents;
913 push @upstream_merge_parents, $old_upstream->{CommitId};
916 foreach my $pc (@newpieces) { # always has '' first
918 read_tree_subdir $pc->{Name}, $pc->{New};
920 runcmd @git, qw(read-tree), $pc->{New};
922 push @upstream_merge_parents, $pc->{New};
925 # index now contains the new upstream
927 if (@newpieces > 1) {
928 # need to make the upstream subtree merge commit
929 $new_upstream = make_commit \@upstream_merge_parents,
930 [ "Combine upstreams for $new_upstream_version",
931 ("[git-debrebase new-upstream combine . ".
932 (join " ", map { $_->{Name} } @newpieces[1..$#newpieces]).
937 # $new_upstream is either the single upstream commit, or the
938 # combined commit we just made. Either way it will be the
939 # "upstream" parent of the breakwater special merge.
941 read_tree_subdir 'debian', "$old_bw:debian";
943 # index now contains the breakwater merge contents
944 $new_bw = make_commit [ $old_bw, $new_upstream ],
945 [ "Update to upstream $new_upstream_version",
946 "[git-debrebase new-upstream breakwater $new_upstream_version]",
949 # Now we have to add a changelog stanza so the Debian version
951 die if unlink "debian";
952 die $! unless $!==ENOENT or $!==ENOTEMPTY;
953 unlink "debian/changelog" or $!==ENOENT or die $!;
954 mkdir "debian" or die $!;
955 open CN, ">", "debian/changelog" or die $!;
956 my $oldclog = git_cat_file ":debian/changelog";
957 $oldclog =~ m/^($package_re) \(\S+\) / or
958 fail "cannot parse old changelog to get package name";
960 print CN <<END, $oldclog or die $!;
961 $p ($new_version) UNRELEASED; urgency=medium
963 * Update to new upstream version $new_upstream_version.
969 runcmd @git, qw(update-index --add --replace), 'debian/changelog';
971 # Now we have the final new breakwater branch in the index
972 $new_bw = make_commit [ $new_bw ],
973 [ "Update changelog for new upstream $new_upstream_version",
974 "[git-debrebase new-upstream changelog $new_upstream_version]",
978 # we have constructed the new breakwater. we now need to commit to
979 # the laundering output, because git-rebase can't easily be made
980 # to make a replay list which is based on some other branch
982 update_head_postlaunder $old_head, $old_laundered_tip,
983 'launder for new upstream';
985 my @cmd = (@git, qw(rebase --onto), $new_bw, $old_bw, @ARGV);
987 # now it's for the user to sort out
990 sub cmd_downstream_rebase_launder_v0 () {
991 badusage "needs 1 argument, the baseline" unless @ARGV==1;
993 $base = git_rev_parse $base;
994 my $old_head = get_head();
995 my $current = $old_head;
998 if ($current eq $base) {
999 $topmost_keep //= $current;
1000 print " $current BASE stop\n";
1003 my $cl = classify $current;
1004 print " $current $cl->{Type}";
1006 my $p0 = $cl->{Parents}[0]{CommitId};
1008 if ($cl->{Type} eq 'Pseudomerge') {
1009 print " ^".($cl->{Contributor}{Ix}+1);
1010 $next = $cl->{Contributor}{CommitId};
1011 } elsif ($cl->{Type} eq 'AddPatches' or
1012 $cl->{Type} eq 'Changelog') {
1022 $topmost_keep //= $current;
1024 die "to-be stripped changes not on top of the branch\n"
1029 if ($topmost_keep eq $old_head) {
1030 print "unchanged\n";
1032 print "updating to $topmost_keep\n";
1033 update_head_checkout
1034 $old_head, $topmost_keep,
1035 'downstream-rebase-launder-v0';
1039 GetOptions("D+" => \$debuglevel,
1040 'force!') or die badusage "bad options\n";
1041 initdebug('git-debrebase ');
1042 enabledebug if $debuglevel;
1044 my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);
1045 chdir $toplevel or die "chdir $toplevel: $!";
1047 $rd = fresh_playground "$playprefix/misc";
1049 if (!@ARGV || $ARGV[0] =~ m{^-}) {
1050 defaultcmd_rebase();
1052 my $cmd = shift @ARGV;
1055 $cmdfn = ${*::}{"cmd_$cmdfn"};
1057 $cmdfn or badusage "unknown git-debrebase sub-operation $cmd";