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);
125 die "bad usage: $m\n";
131 my @cmd = qw(git config -z);
132 push @cmd, qw(--get-all) if wantarray;
134 my $out = cmdoutput @cmd;
135 return split /\0/, $out;
142 my $data = git_cat_file $objid, 'commit';
143 $data =~ m/(?<=\n)\n/ or die "$objid ($data) ?";
147 sub D_UPS () { 0x02; } # upstream files
148 sub D_PAT_ADD () { 0x04; } # debian/patches/ extra patches at end
149 sub D_PAT_OTH () { 0x08; } # debian/patches other changes
150 sub D_DEB_CLOG () { 0x10; } # debian/ (not patches/ or changelog)
151 sub D_DEB_OTH () { 0x20; } # debian/changelog
152 sub DS_DEB () { D_DEB_CLOG | D_DEB_OTH; } # debian/ (not patches/)
154 our $playprefix = 'debrebase';
160 sub in_workarea ($) {
163 my $r = eval { $sub->(); };
164 { local $@; changedir $maindir; }
168 sub fresh_workarea () {
169 $workarea = fresh_playground "$playprefix/work";
170 in_workarea sub { playtree_setup };
173 sub get_differs ($$) {
175 # This resembles quiltify_trees_differ, in dgit, a bit.
176 # But we don't care about modes, or dpkg-source-unrepresentable
177 # changes, and we don't need the plethora of different modes.
178 # Conversely we need to distinguish different kinds of changes to
179 # debian/ and debian/patches/.
184 my ($opts, $limits, $fn) = @_;
185 my @cmd = (@git, qw(diff-tree -z --no-renames));
187 push @cmd, "$_:" foreach $x, $y;
189 my $diffs = cmdoutput @cmd;
190 foreach (split /\0/, $diffs) { $fn->(); }
193 $rundiff->([qw(--name-only)], [], sub {
194 $differs |= $_ eq 'debian' ? DS_DEB : D_UPS;
197 if ($differs & DS_DEB) {
199 $rundiff->([qw(--name-only -r)], [qw(debian)], sub {
201 m{^debian/patches/} ? D_PAT_OTH :
202 $_ eq 'debian/changelog' ? D_DEB_CLOG :
205 die "mysterious debian changes $x..$y"
206 unless $differs & (D_PAT_OTH|DS_DEB);
209 if ($differs & D_PAT_OTH) {
211 $differs &= ~D_PAT_OTH;
213 $differs |= D_PAT_OTH;
214 no warnings qw(exiting); last;
216 $rundiff->([qw(--name-status -r)], [qw(debian/patches/)], sub {
217 no warnings qw(exiting);
218 if (!defined $mode) {
221 die unless s{^debian/patches/}{};
223 if ($mode eq 'A' && !m/\.series$/s) {
225 } elsif ($mode eq 'M' && $_ eq 'series') {
226 my $x_s = git_cat_file "$x:debian/patches/series", 'blob';
227 my $y_s = git_cat_file "$y:debian/patches/series", 'blob';
228 chomp $x_s; $x_s .= "\n";
229 $ok = $x_s eq substr($y_s, 0, length $x_s);
234 $differs |= $ok ? D_PAT_ADD : D_PAT_OTH;
236 die "mysterious debian/patches changes $x..$y"
237 unless $differs & (D_PAT_ADD|D_PAT_OTH);
240 printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
245 sub commit_pr_info ($) {
247 return Data::Dumper->dump([$r], [qw(commit)]);
250 sub calculate_committer_authline () {
251 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
252 'DUMMY COMMIT (git-debrebase)', "HEAD:";
253 my ($h,$m) = get_commit $c;
254 $h =~ m/^committer .*$/m or confess "($h) ?";
258 sub rm_subdir_cached ($) {
260 runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
263 sub read_tree_subdir ($$) {
264 my ($subdir, $new_tree_object) = @_;
265 rm_subdir_cached $subdir;
266 runcmd @git, qw(read-tree), "--prefix=$subdir/", $new_tree_object;
269 # classify returns an info hash like this
271 # Hdr => # commit headers, including 1 final newline
272 # Msg => # commit message (so one newline is dropped)
274 # Type => (see below)
276 # Ix => $index # ie 0, 1, 2, ...
278 # Differs => return value from get_differs
280 # IsDggitImport => 'orig' 'tarball' 'unpatched' 'package' (as from dgit)
282 # NewMsg => # commit message, but with any [dgit import ...] edited
283 # # to say "[was: ...]"
294 # has additional entres in classification result
295 # Overwritten = [ subset of Parents ]
296 # Contributor = $the_remaining_Parent
298 # DgitImportUnpatched
299 # has additional entry in classification result
300 # OrigParents = [ subset of Parents ]
302 # BreakwaterUpstreamMerge
303 # has additional entry in classification result
304 # OrigParents = [ subset of Parents ] # singleton list
306 sub parsecommit ($;$) {
307 my ($objid, $p_ref) = @_;
308 # => hash with CommitId Hdr Msg Tree Parents
309 # Parents entries have only Ix CommitId
310 # $p_ref, if provided, must be [] and is used as a base for Parents
315 my ($h,$m) = get_commit $objid;
317 my ($t) = $h =~ m/^tree (\w+)$/m or die $objid;
318 my (@ph) = $h =~ m/^parent (\w+)$/mg;
328 foreach my $ph (@ph) {
330 Ix => scalar @$p_ref,
342 my $r = parsecommit($objid, \@p);
346 $p->{Differs} = (get_differs $p->{CommitId}, $t),
349 printdebug "classify $objid \$t=$t \@p",
350 (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
354 my ($type, @rest) = @_;
355 $r = { %$r, Type => $type, @rest };
357 my $dd = new Data::Dumper [ $r ];
358 Terse $dd 1; Indent $dd 0; Useqq $dd 1;
359 printdebug " = $type ".(Dump $dd)."\n";
365 $r = { %$r, Type => qw(Unknown) };
366 printdebug " ** Unknown\n";
371 my $d = $r->{Parents}[0]{Differs};
372 if ($d == D_PAT_ADD) {
373 return $classify->(qw(AddPatches));
374 } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
375 return $unknown->("edits debian/patches");
376 } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
377 my ($ty,$dummy) = git_cat_file "$p[0]{CommitId}:debian";
379 if ($d == D_DEB_CLOG) {
380 return $classify->(qw(Changelog));
382 return $classify->(qw(Packaging));
384 } elsif ($ty eq 'missing') {
385 return $classify->(qw(BreakwaterStart));
387 return $unknown->("parent's debian is not a directory");
389 } elsif ($d == D_UPS) {
390 return $classify->(qw(Upstream));
391 } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
392 return $classify->(qw(Mixed));
394 return $unknown->("no changes");
396 confess "internal error $objid ?";
400 return $unknown->("origin commit");
403 my @identical = grep { !$_->{Differs} } @p;
404 if (@p == 2 && @identical == 1) {
405 my @overwritten = grep { $_->{Differs} } @p;
406 confess "internal error $objid ?" unless @overwritten==1;
407 return $classify->(qw(Pseudomerge),
408 Overwritten => $overwritten[0],
409 Contributor => $identical[0]);
411 if (@p == 2 && @identical == 2) {
412 my @bytime = nsort_by {
413 my ($ph,$pm) = get_commit $_->{CommitId};
414 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
417 return $classify->(qw(Pseudomerge),
418 SubType => qw(Ambiguous),
419 Overwritten => $bytime[0],
420 Contributor => $bytime[1]);
423 my ($p_h, $p_m) = get_commit $p->{CommitId};
424 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
425 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
427 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
429 if (!(grep { !$_->{IsOrigin} } @p) and
430 (@orig_ps >= @p - 1) and
431 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
433 return $classify->(qw(DgitImportUnpatched),
434 OrigParents => \@orig_ps);
437 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
438 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
440 # How to decide about l/r ordering of breakwater merges ? git
441 # --topo-order prefers to expand 2nd parent first. There's
442 # already an easy rune to look for debian/ history anyway (git log
443 # debian/) so debian breakwater branch should be 1st parent; that
444 # way also there's also an easy rune to look for the upstream
445 # patches (--topo-order).
447 # The above tells us which way *we* will generate them. But we
448 # might encounter ad-hoc breakwater merges generated manually,
449 # which might be the other way around. In principle, in some odd
450 # situations, a breakwater merge might have two identical parents.
451 # In that case we guess which way round it is (ie, which parent
452 # has the upstream history). The order of the 2-iteration loop
453 # controls which guess we make.
455 foreach my $prevbrw (qw(0 1)) {
458 !$p[$prevbrw]{IsOrigin} && # breakwater never starts with an origin
459 !($p[!$prevbrw]{Differs} & ~DS_DEB) && # no non-debian changess
460 !($p[$prevbrw]{Differs} & ~D_UPS)) { # no non-upstream changes
461 return $classify->(qw(BreakwaterUpstreamMerge),
462 OrigParents => [ $p[!$prevbrw] ]);
466 # multi-orig upstreams are represented with a breakwater merge
467 # from a single upstream commit which combines the orig tarballs
469 return $unknown->("complex merge");
475 $nogenerate,$report) = @_;
476 # => ($tip, $breakwater_tip, $last_upstream_merge_in_breakwater)
477 # (or nothing, if $nogenerate)
479 printdebug "*** WALK $input ".($nogenerate//0)." ".($report//'-')."\n";
481 # go through commits backwards
482 # we generate two lists of commits to apply:
483 # breakwater branch and upstream patches
484 my (@brw_cl, @upp_cl, @processed);
491 my ($appendinfo) = @_;
494 $ms .= "\n\n[git-debrebase $appendinfo]\n";
497 my $rewrite_from_here = sub {
498 my $sp_cl = { SpecialMethod => 'StartRewrite' };
499 push @brw_cl, $sp_cl;
500 push @processed, $sp_cl;
505 my $prprdelim = sub { print $report $prdelim if $report; $prdelim=""; };
508 return unless $report;
509 print $report $prdelim, @_;
513 my $bomb = sub { # usage: return $bomb->();
514 print $report " Unprocessable" if $report;
517 return (undef,undef);
519 die "commit $cur: Cannot cope with this commit (d.".
520 (join ' ', map { sprintf "%#x", $_->{Differs} }
521 @{ $cl->{Parents} }). ")";
527 my $build_start = sub {
528 my ($msg, $parent) = @_;
531 no warnings qw(exiting); last;
534 my $last_upstream_update;
538 my $ty = $cl->{Type};
539 my $st = $cl->{SubType};
540 $prline->("$cl->{CommitId} $cl->{Type}");
541 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
542 push @processed, $cl;
543 my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
544 if ($ty eq 'AddPatches') {
546 $rewrite_from_here->();
548 } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
552 } elsif ($ty eq 'BreakwaterStart') {
553 $last_upstream_update = $cur;
554 $build_start->('FirstPackaging', $cur);
555 } elsif ($ty eq 'Upstream') {
559 } elsif ($ty eq 'Mixed') {
562 my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
565 $queue->(\@brw_cl, "debian");
566 $queue->(\@upp_cl, "upstream");
567 $rewrite_from_here->();
570 } elsif ($ty eq 'Pseudomerge') {
571 my $contrib = $cl->{Contributor}{CommitId};
572 print $report " Contributor=$contrib" if $report;
573 push @pseudomerges, $cl;
574 $rewrite_from_here->();
577 } elsif ($ty eq 'BreakwaterUpstreamMerge') {
578 $last_upstream_update = $cur;
579 $build_start->("PreviousBreakwater", $cur);
580 } elsif ($ty eq 'DgitImportUnpatched') {
581 my $pm = $pseudomerges[-1];
583 # To an extent, this is heuristic. Imports don't have
584 # a useful history of the debian/ branch. We assume
585 # that the first pseudomerge after an import has a
586 # useful history of debian/, and ignore the histories
587 # from later pseudomerges. Often the first pseudomerge
588 # will be the dgit import of the upload to the actual
589 # suite intended by the non-dgit NMUer, and later
590 # pseudomerges may represent in-archive copies.
591 my $ovwrs = $pm->{Overwritten};
592 printf $report " PM=%s \@Overwr:%d", $pm, (scalar @$ovwrs)
595 printdebug "*** WALK BOMB DgitImportUnpatched\n";
598 my $ovwr = $ovwrs->[0]{CommitId};
599 printf $report " Overwr=%s", $ovwr if $report;
600 # This import has a tree which is just like a
601 # breakwater tree, but it has the wrong history. It
602 # ought to have the previous breakwater (which the
603 # pseudomerge overwrote) as an ancestor. That will
604 # make the history of the debian/ files correct. As
605 # for the upstream version: either it's the same as
606 # was ovewritten (ie, same as the previous
607 # breakwater), in which case that history is precisely
608 # right; or, otherwise, it was a non-gitish upload of a
609 # new upstream version. We can tell these apart by
610 # looking at the tree of the supposed upstream.
613 SpecialMethod => 'DgitImportDebianUpdate',
614 $xmsg->("convert dgit import: debian changes")
616 my $differs = (get_differs $ovwr, $cl->{Tree});
617 printf $report " Differs=%#x", $differs if $report;
618 if ($differs & D_UPS) {
619 printf $report " D_UPS" if $report;
620 # This will also trigger if a non-dgit git-based NMU
621 # deleted .gitignore (which is a thing that some of
622 # the existing git tools do if the user doesn't
623 # somehow tell them not to). Ah well.
626 SpecialMethod => 'DgitImportUpstreamUpdate',
627 $xmsg->("convert dgit import: upstream changes")
630 $prline->(" Import");
631 $rewrite_from_here->();
632 $upp_limit //= $#upp_cl; # further, deeper, patches discarded
633 die 'BUG $upp_limit is not used anywhere?';
637 # Everything is from this import. This kind of import
638 # is already in valid breakwater format, with the
639 # patches as commits.
640 printf $report " NoPM" if $report;
641 # last thing we processed will have been the first patch,
642 # if there is one; which is fine, so no need to rewrite
643 # on account of this import
644 $build_start->("ImportOrigin", $cur);
648 printdebug "*** WALK BOMB unrecognised\n";
654 printdebug "*** WALK prep done cur=$cur".
655 " brw $#brw_cl upp $#upp_cl proc $#processed pm $#pseudomerges\n";
657 return if $nogenerate;
659 # Now we build it back up again
665 my $read_tree_debian = sub {
667 read_tree_subdir 'debian', "$treeish:debian";
668 rm_subdir_cached 'debian/patches';
670 my $read_tree_upstream = sub {
672 runcmd @git, qw(read-tree), $treeish;
673 $read_tree_debian->($build);
676 my $committer_authline = calculate_committer_authline();
678 printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
680 confess "internal error" unless $build eq (pop @processed)->{CommitId};
683 mkdir $rd or $!==EEXIST or die $!;
685 runcmd @git, qw(read-tree), $build;
686 foreach my $cl (qw(Debian), (reverse @brw_cl),
687 { SpecialMethod => 'RecordBreakwaterTip' },
688 qw(Upstream), (reverse @upp_cl)) {
690 $current_method = $cl;
693 my $method = $cl->{SpecialMethod} // $current_method;
694 my @parents = ($build);
695 my $cltree = $cl->{CommitId};
696 printdebug "WALK BUILD ".($cltree//'undef').
697 " $method (rewriting=$rewriting)\n";
698 if ($method eq 'Debian') {
699 $read_tree_debian->($cltree);
700 } elsif ($method eq 'Upstream') {
701 $read_tree_upstream->($cltree);
702 } elsif ($method eq 'StartRewrite') {
705 } elsif ($method eq 'RecordBreakwaterTip') {
706 $breakwater = $build;
708 } elsif ($method eq 'DgitImportDebianUpdate') {
709 $read_tree_debian->($cltree);
710 rm_subdir_cached qw(debian/patches);
711 } elsif ($method eq 'DgitImportUpstreamUpdate') {
712 $read_tree_upstream->($cltree);
713 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
718 my $procd = (pop @processed) // 'UNDEF';
721 printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
724 my $newtree = cmdoutput @git, qw(write-tree);
726 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
727 $ch =~ s{^parent .*\n}{}m;
728 $ch =~ s{(?=^author)}{
729 join '', map { "parent $_\n" } @parents
730 }me or confess "$ch ?";
732 $ch =~ s{^committer .*$}{$committer_authline}m
735 my $cf = "$rd/m$rewriting";
736 open CD, ">", $cf or die $!;
737 print CD $ch, "\n", $cl->{Msg} or die $!;
739 my @cmd = (@git, qw(hash-object));
740 push @cmd, qw(-w) if $rewriting;
741 push @cmd, qw(-t commit), $cf;
742 my $newcommit = cmdoutput @cmd;
743 confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
745 if (grep { $method eq $_ } qw(DgitImportUpstreamUpdate)) {
746 $last_upstream_update = $cur;
751 my $final_check = get_differs $build, $input;
752 die sprintf "internal error %#x %s %s", $final_check, $build, $input
753 if $final_check & ~D_PAT_ADD;
755 my @r = ($build, $breakwater, $last_upstream_update);
756 printdebug "*** WALK RETURN @r\n";
760 sub get_head () { return git_rev_parse qw(HEAD); }
762 sub update_head ($$$) {
763 my ($old, $new, $mrest) = @_;
764 runcmd @git, qw(update-ref -m), "debrebase: $mrest", 'HEAD', $new, $old;
767 sub update_head_checkout ($$$) {
768 my ($old, $new, $mrest) = @_;
769 my $symref = git_get_symref();
770 runcmd @git, qw(checkout), $new, qw(.);
771 update_head $old, $new, $mrest;
775 badusage "no arguments to launder allowed" if @ARGV;
776 my $old = get_head();
777 my ($tip,$breakwater,$last_upstream_merge) = walk $old;
778 update_head $old, $tip, 'launder';
779 # no tree changes except debian/patches
780 runcmd @git, qw(rm --quiet --ignore-unmatch -rf debian/patches);
781 printf "# breakwater tip\n%s\n", $breakwater;
782 printf "# working tip\n%s\n", $tip;
783 printf "# last upstream merge\n%s\n", $last_upstream_merge;
787 die if ($ARGV[0]//'') =~ m/^-/;
788 badusage "too many arguments to analyse" if @ARGV>1;
791 $old = git_rev_parse $old;
795 my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
796 STDOUT->error and die $!;
799 sub cmd_new_upstream_v0 () {
800 # xxx would like to support more git-rebase options
802 "need NEW-VERSION UPS-COMMITISH [EXTRA-UPS-NAME EXTRA-UPS-COMMITISH...]"
803 unless @ARGV % 2 == 0 and @ARGV >= 2;
804 # tree should be clean and this is not checked
805 # automatically and unconditionally launders before rebasing
806 # if rebase --abort is used, laundering has still been done
810 # parse args - low commitment
811 my $new_version = (new Dpkg::Version scalar(shift @ARGV), check => 1);
812 my $new_upstream_version = $new_version->version();
814 my $new_upstream = git_rev_parse shift @ARGV;
817 my ($n, @x) = @_; # may be ''
818 my $pc = $pieces{$n} //= {
820 Desc => ($n ? "upstream piece $n" : "upstream (main piece"),
822 while (my $k = shift @x) { $pc->{$k} = shift @x; }
827 my ($n, @x) = @_; # may be ''
828 my $pc = $piece->($n, @x, NewIx => (scalar @newpieces));
829 push @newpieces, $pc;
834 New => $new_upstream,
838 my $c = git_rev_parse shift @ARGV;
839 die unless $n =~ m/^$extra_orig_namepart_re$/;
840 $newpiece->($n, New => $c);
843 # now we need to investigate the branch this generates the
844 # laundered version but we don't switch to it yet
845 my $old_head = get_head();
846 my ($old_laundered_tip,$old_bw,$old_upstream_update) = walk $old_head;
848 my $old_bw_cl = classify $old_bw;
849 my $old_upstream_update_cl = classify $old_upstream_update;
850 confess unless $old_upstream_update_cl->{OrigParents};
851 my $old_upstream = parsecommit
852 $old_upstream_update_cl->{OrigParents}[0]{CommitId};
858 print STDERR "preflight check failed: $msg\n";
861 $piece->('', Old => $old_upstream->{CommitId});
863 if ($old_upstream->{Msg} =~ m{^\[git-debrebase }m) {
864 if ($old_upstream->{Msg} =~
865 m{^\[git-debrebase new-upstream combine \.((?: $extra_orig_namepart_re)+)\]}
867 my @oldpieces = ('', split / /, $1);
868 my $parentix = -1 + scalar @{ $old_upstream->{Parents} };
869 foreach my $i (0..$#oldpieces) {
870 my $n = $oldpieces[$i];
871 $piece->($n, Old => $old_upstream->{CommitId}.'^'.$parentix);
874 $problem->("previous upstream $old_upstream->{CommitId} is from".
875 " git-debrebase but not a \`new-upstream combine' commit");
879 foreach my $pc (values %pieces) {
881 $problem->("introducing upstream piece $pc->{Name}");
882 } elsif (!$pc->{New}) {
883 $problem->("dropping upstream piece $pc->{Name}");
884 } elsif (!is_fast_fwd $pc->{Old}, $pc->{New}) {
885 $problem->("not fast forward: $pc->{Name} $pc->{Old}..$pc->{New}");
892 "preflight check failures (%d) overriden by --force\n",
896 "preflight check failures (%d) (you could --force)",
905 my @upstream_merge_parents;
908 push @upstream_merge_parents, $old_upstream->{CommitId};
911 foreach my $pc (@newpieces) { # always has '' first
913 read_tree_subdir $pc->{Name}, $pc->{New};
915 runcmd @git, qw(read-tree), $pc->{New};
917 push @upstream_merge_parents, $pc->{New};
920 # index now contains the new upstream
922 if (@newpieces > 1) {
923 # need to make the upstream subtree merge commit
924 my $us_tree = cmdoutput @git, qw(write-tree);
925 my @cmd = (@git, qw(commit-tree), $us_tree);
926 push @cmd, qw(-p), $_ foreach @upstream_merge_parents;
927 push @cmd, qw(-m), "Combine upstreams for $new_upstream_version";
929 "[git-debrebase new-upstream combine . ".
930 (join " ", map { $_->{Name} } @newpieces[1..$#newpieces]).
932 $new_upstream = cmdoutput @cmd;
935 # $new_upstream is either the single upstream commit, or the
936 # combined commit we just made. Either way it will be the
937 # "upstream" parent of the breakwater special merge.
939 read_tree_subdir 'debian', "$old_bw:debian";
941 # index now contains the breakwater merge contents
943 my $bw_tree = cmdoutput @git, qw(write_tree);
944 my @cmd = (@git, qw(commit-tree), $bw_tree);
945 push @cmd, qw(-p), $old_bw, qw(-p), $new_upstream;
946 push @cmd, qw(-m), "Update to upstream $new_upstream_version";
948 "[git-debrebase new-upstream breakwater $new_upstream_version]";
949 $new_bw = cmdoutput @git;
951 # Now we have to add a changelog stanza so the Debian version
954 die if unlink "debian";
955 die unless $!==ENOTEMPTY;
956 unlink "debian/changelog" or die $!;
957 open CN, ">", "debian/changelog" or die $!;
958 my $oldclog = git_cat_file ":debian/changelog";
959 $oldclog =~ m/^($package_re) \(\S+\) / or
960 fail "cannot parse old changelog to get package name";
962 print CN <<END, $oldclog or die $!;
963 $p ($new_version) UNRELEASED; urgency=medium
965 * Update to new upstream version $new_upstream_version.
971 runcmd @git, qw(update-index --add --replace), 'debian/changelog';
973 # Now we have the final new breakwater branch in the index
975 $bw_tree = cmdoutput @git, qw(write_tree);
976 @cmd = (@git, qw(commit-tree), $bw_tree);
977 push @cmd, qw(-p), $new_bw;
979 "Update changelog for new upstream $new_upstream_version";
981 "[git-debrebase new-upstream changelog $new_upstream_version]";
982 $new_bw = cmdoutput @git;
985 # we have constructed the new breakwater. we now need to commit to
986 # the laundering output, because git-rebase can't easily be made
987 # to make a replay list which is based on some other branch
989 update_head $old_head, $old_laundered_tip, 'launder for new upstream';
991 my @cmd = (@git, qw(rebase --onto), $new_bw, $old_bw);
993 # now it's for the user to sort out
996 sub cmd_downstream_rebase_launder_v0 () {
997 badusage "needs 1 argument, the baseline" unless @ARGV==1;
999 $base = git_rev_parse $base;
1000 my $old_head = get_head();
1001 my $current = $old_head;
1004 if ($current eq $base) {
1005 $topmost_keep //= $current;
1006 print " $current BASE stop\n";
1009 my $cl = classify $current;
1010 print " $current $cl->{Type}";
1012 my $p0 = $cl->{Parents}[0]{CommitId};
1014 if ($cl->{Type} eq 'Pseudomerge') {
1015 print " ^".($cl->{Contributor}{Ix}+1);
1016 $next = $cl->{Contributor}{CommitId};
1017 } elsif ($cl->{Type} eq 'AddPatches' or
1018 $cl->{Type} eq 'Changelog') {
1028 $topmost_keep //= $current;
1030 die "to-be stripped changes not on top of the branch\n"
1035 if ($topmost_keep eq $old_head) {
1036 print "unchanged\n";
1038 print "updating to $topmost_keep\n";
1039 update_head_checkout
1040 $old_head, $topmost_keep,
1041 'downstream-rebase-launder-v0';
1045 GetOptions("D+" => \$debuglevel,
1046 'force!') or die badusage "bad options\n";
1047 initdebug('git-debrebase ');
1048 enabledebug if $debuglevel;
1050 my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);
1051 chdir $toplevel or die "chdir $toplevel: $!";
1053 $rd = fresh_playground "$playprefix/misc";
1055 my $cmd = shift @ARGV;
1058 $cmdfn = ${*::}{"cmd_$cmdfn"};
1060 $cmdfn or badusage "unknown git-debrebase sub-operation $cmd";