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/>.
21 END { $? = $Debian::Dgit::ExitStatus::desired // -1; };
22 use Debian::Dgit::ExitStatus;
26 use Debian::Dgit qw(:DEFAULT :playground);
33 use Getopt::Long qw(:config posix_default gnu_compat bundling);
35 use File::FnMatch qw(:fnmatch);
37 our ($opt_force, $opt_noop_ok, @opt_anchors);
38 our ($opt_defaultcmd_interactive);
40 our $us = qw(git-debrebase);
44 print STDERR "bad usage: $m\n";
49 my ($k, $optional) = @_;
51 my @cmd = qw(git config -z);
52 push @cmd, qw(--get-all) if wantarray;
54 my $out = cmdoutput_errok @cmd;
56 fail "missing required git config $k" unless $optional;
59 my @l = split /\0/, $out;
60 return wantarray ? @l : $l[0];
67 my $dd = new Data::Dumper [ $v ];
68 Terse $dd 1; Indent $dd 0; Useqq $dd 1;
74 my $data = (git_cat_file $objid, 'commit');
75 $data =~ m/(?<=\n)\n/ or die "$objid ($data) ?";
79 sub D_UPS () { 0x02; } # upstream files
80 sub D_PAT_ADD () { 0x04; } # debian/patches/ extra patches at end
81 sub D_PAT_OTH () { 0x08; } # debian/patches other changes
82 sub D_DEB_CLOG () { 0x10; } # debian/ (not patches/ or changelog)
83 sub D_DEB_OTH () { 0x20; } # debian/changelog
84 sub DS_DEB () { D_DEB_CLOG | D_DEB_OTH; } # debian/ (not patches/)
86 our $playprefix = 'debrebase';
95 my $r = eval { $sub->(); };
96 { local $@; changedir $maindir; }
100 sub fresh_workarea () {
101 $workarea = fresh_playground "$playprefix/work";
102 in_workarea sub { playtree_setup };
105 our $snags_forced = 0;
106 our $snags_tripped = 0;
107 our $snags_summarised = 0;
108 our @deferred_updates;
109 our @deferred_update_messages;
111 sub all_snags_summarised () {
112 $snags_forced + $snags_tripped == $snags_summarised;
114 sub run_deferred_updates ($) {
117 confess 'dangerous internal error' unless all_snags_summarised();
119 my @upd_cmd = (@git, qw(update-ref --stdin -m), "debrebase: $mrest");
120 debugcmd '>|', @upd_cmd;
121 open U, "|-", @upd_cmd or die $!;
122 foreach (@deferred_updates) {
123 printdebug ">= ", $_, "\n";
124 print U $_, "\n" or die $!;
127 close U or failedcmd @upd_cmd;
129 print $_, "\n" foreach @deferred_update_messages;
131 @deferred_updates = ();
132 @deferred_update_messages = ();
135 sub get_differs ($$) {
137 # This resembles quiltify_trees_differ, in dgit, a bit.
138 # But we don't care about modes, or dpkg-source-unrepresentable
139 # changes, and we don't need the plethora of different modes.
140 # Conversely we need to distinguish different kinds of changes to
141 # debian/ and debian/patches/.
146 my ($opts, $limits, $fn) = @_;
147 my @cmd = (@git, qw(diff-tree -z --no-renames));
149 push @cmd, "$_:" foreach $x, $y;
150 push @cmd, '--', @$limits;
151 my $diffs = cmdoutput @cmd;
152 foreach (split /\0/, $diffs) { $fn->(); }
155 $rundiff->([qw(--name-only)], [], sub {
156 $differs |= $_ eq 'debian' ? DS_DEB : D_UPS;
159 if ($differs & DS_DEB) {
161 $rundiff->([qw(--name-only -r)], [qw(debian)], sub {
163 m{^debian/patches/} ? D_PAT_OTH :
164 $_ eq 'debian/changelog' ? D_DEB_CLOG :
167 die "mysterious debian changes $x..$y"
168 unless $differs & (D_PAT_OTH|DS_DEB);
171 if ($differs & D_PAT_OTH) {
173 $differs &= ~D_PAT_OTH;
175 $differs |= D_PAT_OTH;
176 no warnings qw(exiting); last;
178 $rundiff->([qw(--name-status -r)], [qw(debian/patches/)], sub {
179 no warnings qw(exiting);
180 if (!defined $mode) {
183 die unless s{^debian/patches/}{};
185 if ($mode eq 'A' && !m/\.series$/s) {
187 } elsif ($mode eq 'M' && $_ eq 'series') {
188 my $x_s = (git_cat_file "$x:debian/patches/series", 'blob');
189 my $y_s = (git_cat_file "$y:debian/patches/series", 'blob');
190 chomp $x_s; $x_s .= "\n";
191 $ok = $x_s eq substr($y_s, 0, length $x_s);
196 $differs |= $ok ? D_PAT_ADD : D_PAT_OTH;
198 die "mysterious debian/patches changes $x..$y"
199 unless $differs & (D_PAT_ADD|D_PAT_OTH);
202 printdebug sprintf "get_differs %s, %s = %#x\n", $x, $y, $differs;
207 sub commit_pr_info ($) {
209 return Data::Dumper->dump([$r], [qw(commit)]);
212 sub calculate_committer_authline () {
213 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
214 'DUMMY COMMIT (git-debrebase)', "HEAD:";
215 my ($h,$m) = get_commit $c;
216 $h =~ m/^committer .*$/m or confess "($h) ?";
220 sub rm_subdir_cached ($) {
222 runcmd @git, qw(rm --quiet -rf --cached --ignore-unmatch), $subdir;
225 sub read_tree_subdir ($$) {
226 my ($subdir, $new_tree_object) = @_;
227 rm_subdir_cached $subdir;
228 runcmd @git, qw(read-tree), "--prefix=$subdir/", $new_tree_object;
231 sub make_commit ($$) {
232 my ($parents, $message_paras) = @_;
233 my $tree = cmdoutput @git, qw(write-tree);
234 my @cmd = (@git, qw(commit-tree), $tree);
235 push @cmd, qw(-p), $_ foreach @$parents;
236 push @cmd, qw(-m), $_ foreach @$message_paras;
237 return cmdoutput @cmd;
240 our @snag_force_opts;
242 my ($tag,$msg) = @_; # ignores extra args, for benefit of keycommits
243 if (grep { $_ eq $tag } @snag_force_opts) {
245 print STDERR "git-debrebase: snag ignored (-f$tag): $msg\n";
248 print STDERR "git-debrebase: snag detected (-f$tag): $msg\n";
252 # Important: all mainline code must call snags_maybe_bail after
253 # any point where snag might be called, but before making changes
254 # (eg before any call to run_deferred_updates). snags_maybe_bail
255 # may be called more than once if necessary (but this is not ideal
256 # because then the messages about number of snags may be confusing).
257 sub snags_maybe_bail () {
258 return if all_snags_summarised();
261 "%s: snags: %d overriden by individual -f options\n",
264 if ($snags_tripped) {
267 "%s: snags: %d overriden by global --force\n",
271 "%s: snags: %d blockers (you could -f<tag>, or --force)",
275 $snags_summarised = $snags_forced + $snags_tripped;
278 return $snags_forced || $snags_tripped;
281 # classify returns an info hash like this
283 # Hdr => # commit headers, including 1 final newline
284 # Msg => # commit message (so one newline is dropped)
286 # Type => (see below)
288 # Ix => $index # ie 0, 1, 2, ...
290 # Differs => return value from get_differs
292 # IsDggitImport => 'orig' 'tarball' 'unpatched' 'package' (as from dgit)
294 # NewMsg => # commit message, but with any [dgit import ...] edited
295 # # to say "[was: ...]"
305 # has additional entres in classification result
306 # Overwritten = [ subset of Parents ]
307 # Contributor = $the_remaining_Parent
309 # DgitImportUnpatched
310 # has additional entry in classification result
311 # OrigParents = [ subset of Parents ]
314 # has additional entry in classification result
315 # OrigParents = [ subset of Parents ] # singleton list
322 # has additional entry in classification result
325 sub parsecommit ($;$) {
326 my ($objid, $p_ref) = @_;
327 # => hash with CommitId Hdr Msg Tree Parents
328 # Parents entries have only Ix CommitId
329 # $p_ref, if provided, must be [] and is used as a base for Parents
334 my ($h,$m) = get_commit $objid;
336 my ($t) = $h =~ m/^tree (\w+)$/m or die $objid;
337 my (@ph) = $h =~ m/^parent (\w+)$/mg;
347 foreach my $ph (@ph) {
349 Ix => scalar @$p_ref,
361 my $r = parsecommit($objid, \@p);
365 $p->{Differs} = (get_differs $p->{CommitId}, $t),
368 printdebug "classify $objid \$t=$t \@p",
369 (map { sprintf " %s/%#x", $_->{CommitId}, $_->{Differs} } @p),
373 my ($type, @rest) = @_;
374 $r = { %$r, Type => $type, @rest };
376 printdebug " = $type ".(dd $r)."\n";
382 $r = { %$r, Type => qw(Unknown), Why => $why };
383 printdebug " ** Unknown\n";
387 if (grep { $_ eq $objid } @opt_anchors) {
388 return $classify->('TreatAsAnchor');
391 my @identical = grep { !$_->{Differs} } @p;
392 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
393 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
395 if ($r->{Msg} =~ m{^\[git-debrebase anchor.*\]$}m) {
396 # multi-orig upstreams are represented with an anchor merge
397 # from a single upstream commit which combines the orig tarballs
399 # Every anchor tagged this way must be a merge.
400 # We are relying on the
401 # [git-debrebase anchor: ...]
402 # commit message annotation in "declare" anchor merges (which
403 # do not have any upstream changes), to distinguish those
404 # anchor merges from ordinary pseudomerges (which we might
405 # just try to strip).
407 # However, the user is going to be doing git-rebase a lot. We
408 # really don't want them to rewrite an anchor commit.
409 # git-rebase trips up on merges, so that is a useful safety
412 # BreakwaterStart commits are also anchors in the terminology
413 # of git-debrebase(5), but they are untagged (and always
414 # manually generated).
416 # We cannot not tolerate any tagged linear commit (ie,
417 # BreakwaterStart commits tagged `[anchor:') because such a
418 # thing could result from an erroneous linearising raw git
419 # rebase of a merge anchor. That would represent a corruption
420 # of the branch. and we want to detect and reject the results
421 # of such corruption before it makes it out anywhere. If we
422 # reject it here then we avoid making the pseudomerge which
423 # would be needed to push it.
425 my $badanchor = sub { $unknown->("git-debrebase \`anchor' but @_"); };
426 @p == 2 or return $badanchor->("has other than two parents");
427 $haspatches and return $badanchor->("contains debian/patches");
429 # How to decide about l/r ordering of anchors ? git
430 # --topo-order prefers to expand 2nd parent first. There's
431 # already an easy rune to look for debian/ history anyway (git log
432 # debian/) so debian breakwater branch should be 1st parent; that
433 # way also there's also an easy rune to look for the upstream
434 # patches (--topo-order).
436 # Also this makes --first-parent be slightly more likely to
437 # be useful - it makes it provide a linearised breakwater history.
439 # Of course one can say somthing like
440 # gitk -- ':/' ':!/debian'
441 # to get _just_ the commits touching upstream files, and by
442 # the TREESAME logic in git-rev-list this will leave the
443 # breakwater into upstream at the first anchor. But that
444 # doesn't report debian/ changes at all.
446 # Other observations about gitk: by default, gitk seems to
447 # produce output in a different order to git-rev-list. I
448 # can't seem to find this documented anywhere. gitk
449 # --date-order DTRT. But, gitk always seems to put the
450 # parents from left to right, in order, so it's easy to see
451 # which way round a pseudomerge is.
453 $p[0]{IsOrigin} and $badanchor->("is an origin commit");
454 $p[1]{Differs} & ~DS_DEB and
455 $badanchor->("upstream files differ from left parent");
456 $p[0]{Differs} & ~D_UPS and
457 $badanchor->("debian/ differs from right parent");
459 return $classify->(qw(Anchor),
460 OrigParents => [ $p[1] ]);
464 my $d = $r->{Parents}[0]{Differs};
465 if ($d == D_PAT_ADD) {
466 return $classify->(qw(AddPatches));
467 } elsif ($d & (D_PAT_ADD|D_PAT_OTH)) {
468 return $unknown->("edits debian/patches");
469 } elsif ($d & DS_DEB and !($d & ~DS_DEB)) {
470 my ($ty,$dummy) = git_cat_file "$p[0]{CommitId}:debian";
472 if ($d == D_DEB_CLOG) {
473 return $classify->(qw(Changelog));
475 return $classify->(qw(Packaging));
477 } elsif ($ty eq 'missing') {
478 return $classify->(qw(BreakwaterStart));
480 return $unknown->("parent's debian is not a directory");
482 } elsif ($d == D_UPS) {
483 return $classify->(qw(Upstream));
484 } elsif ($d & DS_DEB and $d & D_UPS and !($d & ~(DS_DEB|D_UPS))) {
485 return $classify->(qw(Mixed));
487 return $unknown->("no changes");
489 confess "internal error $objid ?";
493 return $unknown->("origin commit");
496 if (@p == 2 && @identical == 1) {
497 my @overwritten = grep { $_->{Differs} } @p;
498 confess "internal error $objid ?" unless @overwritten==1;
499 return $classify->(qw(Pseudomerge),
500 Overwritten => [ $overwritten[0] ],
501 Contributor => $identical[0]);
503 if (@p == 2 && @identical == 2) {
505 my ($ph,$pm) = get_commit $_[0]{CommitId};
506 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
510 my $order = $get_t->($bytime[0]) <=> $get_t->($bytime[1]);
511 if ($order > 0) { # newer first
512 } elsif ($order < 0) {
513 @bytime = reverse @bytime;
515 # same age, default to order made by -s ours
516 # that is, commit was made by someone who preferred L
518 return $classify->(qw(Pseudomerge),
519 SubType => qw(Ambiguous),
520 Contributor => $bytime[0],
521 Overwritten => [ $bytime[1] ]);
524 my ($p_h, $p_m) = get_commit $p->{CommitId};
525 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
526 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
528 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' } @p;
530 if (!(grep { !$_->{IsOrigin} } @p) and
531 (@orig_ps >= @p - 1) and
532 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
534 return $classify->(qw(DgitImportUnpatched),
535 OrigParents => \@orig_ps);
538 return $unknown->("complex merge");
541 sub keycommits ($;$$$) {
542 my ($head, $furniture, $unclean, $trouble) = @_;
543 # => ($anchor, $breakwater)
545 # $unclean->("unclean-$tagsfx", $msg, $cl)
546 # $furniture->("unclean-$tagsfx", $msg, $cl)
547 # $dgitimport->("unclean-$tagsfx", $msg, $cl))
548 # is callled for each situation or commit that
549 # wouldn't be found in a laundered branch
550 # $furniture is for furniture commits such as might be found on an
551 # interchange branch (pseudomerge, d/patches, changelog)
552 # $trouble is for things whnich prevent the return of
553 # anchor and breakwater information; if that is ignored,
554 # then keycommits returns (undef, undef) instead.
556 # If a callback is undef, fail is called instead.
557 # If a callback is defined but false, the situation is ignored.
559 # no warnings qw(exiting); last;
560 # if the answer is no longer wanted.
562 my ($anchor, $breakwater);
566 my ($cb, $tagsfx, $why) = @_;
567 my $m = "branch needs laundering (run git-debrebase): $why";
568 fail $m unless defined $cb;
570 $cb->("unclean-$tagsfx", $why, $cl);
573 $cl = classify $head;
574 my $ty = $cl->{Type};
575 if ($ty eq 'Packaging') {
576 $breakwater //= $clogonly;
577 $breakwater //= $head;
578 } elsif ($ty eq 'Changelog') {
579 # this is going to count as the tip of the breakwater
580 # only if it has no upstream stuff before it
582 } elsif ($ty eq 'Anchor' or
583 $ty eq 'TreatAsAnchor' or
584 $ty eq 'BreakwaterStart') {
586 $breakwater //= $clogonly;
587 $breakwater //= $head;
589 } elsif ($ty eq 'Upstream') {
590 $x->($unclean, 'ordering',
591 "packaging change ($breakwater) follows upstream change (eg $head)")
592 if defined $breakwater;
595 } elsif ($ty eq 'Mixed') {
596 $x->($unclean, 'mixed',
597 "found mixed upstream/packaging commit ($head)");
600 } elsif ($ty eq 'Pseudomerge' or
601 $ty eq 'AddPatches') {
602 $x->($furniture, (lc $ty),
603 "found interchange bureaucracy commit ($ty, $head)");
604 } elsif ($ty eq 'DgitImportUnpatched') {
605 $x->($trouble, 'dgitimport',
606 "found dgit dsc import ($head)");
609 no warnings qw(exiting);
612 fail "found unprocessable commit, cannot cope: $head; $cl->{Why}";
614 $head = $cl->{Parents}[0]{CommitId};
616 return ($anchor, $breakwater);
622 $nogenerate,$report) = @_;
623 # => ($tip, $breakwater_tip, $last_anchor)
624 # (or nothing, if $nogenerate)
626 printdebug "*** WALK $input ".($nogenerate//0)." ".($report//'-')."\n";
628 # go through commits backwards
629 # we generate two lists of commits to apply:
630 # breakwater branch and upstream patches
631 my (@brw_cl, @upp_cl, @processed);
638 my ($prose, $info) = @_;
642 $ms .= "\n\n[git-debrebase$info: $prose]\n";
645 my $rewrite_from_here = sub {
647 my $sp_cl = { SpecialMethod => 'StartRewrite' };
649 push @processed, $sp_cl;
654 my $prprdelim = sub { print $report $prdelim if $report; $prdelim=""; };
657 return unless $report;
658 print $report $prdelim, @_;
662 my $bomb = sub { # usage: return $bomb->();
663 print $report " Unprocessable" if $report;
664 print $report " ($cl->{Why})" if $report && defined $cl->{Why};
667 return (undef,undef);
669 die "commit $cur: Cannot cope with this commit (d.".
670 (join ' ', map { sprintf "%#x", $_->{Differs} }
671 @{ $cl->{Parents} }).
672 (defined $cl->{Why} ? "; $cl->{Why}": '').
679 my $build_start = sub {
680 my ($msg, $parent) = @_;
683 no warnings qw(exiting); last;
690 my $ty = $cl->{Type};
691 my $st = $cl->{SubType};
692 $prline->("$cl->{CommitId} $cl->{Type}");
693 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
694 push @processed, $cl;
695 my $p0 = @{ $cl->{Parents} }==1 ? $cl->{Parents}[0]{CommitId} : undef;
696 if ($ty eq 'AddPatches') {
698 $rewrite_from_here->(\@upp_cl);
700 } elsif ($ty eq 'Packaging' or $ty eq 'Changelog') {
704 } elsif ($ty eq 'BreakwaterStart') {
706 $build_start->('FirstPackaging', $cur);
707 } elsif ($ty eq 'Upstream') {
711 } elsif ($ty eq 'Mixed') {
714 my $cls = { %$cl, $xmsg->("split mixed commit: $wh part") };
717 $queue->(\@brw_cl, "debian");
718 $queue->(\@upp_cl, "upstream");
719 $rewrite_from_here->(\@brw_cl);
722 } elsif ($ty eq 'Pseudomerge') {
723 my $contrib = $cl->{Contributor}{CommitId};
724 print $report " Contributor=$contrib" if $report;
725 push @pseudomerges, $cl;
726 $rewrite_from_here->(\@upp_cl);
729 } elsif ($ty eq 'Anchor' or $ty eq 'TreatAsAnchor') {
731 $build_start->("Anchor", $cur);
732 } elsif ($ty eq 'DgitImportUnpatched') {
733 my $pm = $pseudomerges[-1];
735 # To an extent, this is heuristic. Imports don't have
736 # a useful history of the debian/ branch. We assume
737 # that the first pseudomerge after an import has a
738 # useful history of debian/, and ignore the histories
739 # from later pseudomerges. Often the first pseudomerge
740 # will be the dgit import of the upload to the actual
741 # suite intended by the non-dgit NMUer, and later
742 # pseudomerges may represent in-archive copies.
743 my $ovwrs = $pm->{Overwritten};
744 printf $report " PM=%s \@Overwr:%d",
745 $pm->{CommitId}, (scalar @$ovwrs)
748 printdebug "*** WALK BOMB DgitImportUnpatched\n";
751 my $ovwr = $ovwrs->[0]{CommitId};
752 printf $report " Overwr=%s", $ovwr if $report;
753 # This import has a tree which is just like a
754 # breakwater tree, but it has the wrong history. It
755 # ought to have the previous breakwater (which the
756 # pseudomerge overwrote) as an ancestor. That will
757 # make the history of the debian/ files correct. As
758 # for the upstream version: either it's the same as
759 # was ovewritten (ie, same as the previous
760 # breakwater), in which case that history is precisely
761 # right; or, otherwise, it was a non-gitish upload of a
762 # new upstream version. We can tell these apart by
763 # looking at the tree of the supposed upstream.
766 SpecialMethod => 'DgitImportDebianUpdate',
767 $xmsg->("convert dgit import: debian changes")
770 SpecialMethod => 'DgitImportUpstreamUpdate',
771 $xmsg->("convert dgit import: upstream update",
774 $prline->(" Import");
775 $rewrite_from_here->(\@brw_cl);
776 $upp_limit //= $#upp_cl; # further, deeper, patches discarded
780 # Everything is from this import. This kind of import
781 # is already in valid breakwater format, with the
782 # patches as commits.
783 printf $report " NoPM" if $report;
784 # last thing we processed will have been the first patch,
785 # if there is one; which is fine, so no need to rewrite
786 # on account of this import
787 $build_start->("ImportOrigin", $cur);
791 printdebug "*** WALK BOMB unrecognised\n";
797 printdebug "*** WALK prep done cur=$cur".
798 " brw $#brw_cl upp $#upp_cl proc $#processed pm $#pseudomerges\n";
800 return if $nogenerate;
802 # Now we build it back up again
808 my $read_tree_debian = sub {
810 read_tree_subdir 'debian', "$treeish:debian";
811 rm_subdir_cached 'debian/patches';
813 my $read_tree_upstream = sub {
815 runcmd @git, qw(read-tree), $treeish;
816 $read_tree_debian->($build);
819 $#upp_cl = $upp_limit if defined $upp_limit;
821 my $committer_authline = calculate_committer_authline();
823 printdebug "WALK REBUILD $build ".(scalar @processed)."\n";
825 confess "internal error" unless $build eq (pop @processed)->{CommitId};
828 mkdir $rd or $!==EEXIST or die $!;
830 runcmd @git, qw(read-tree), $build;
831 foreach my $cl (qw(Debian), (reverse @brw_cl),
832 { SpecialMethod => 'RecordBreakwaterTip' },
833 qw(Upstream), (reverse @upp_cl)) {
835 $current_method = $cl;
838 my $method = $cl->{SpecialMethod} // $current_method;
839 my @parents = ($build);
840 my $cltree = $cl->{CommitId};
841 printdebug "WALK BUILD ".($cltree//'undef').
842 " $method (rewriting=$rewriting)\n";
843 if ($method eq 'Debian') {
844 $read_tree_debian->($cltree);
845 } elsif ($method eq 'Upstream') {
846 $read_tree_upstream->($cltree);
847 } elsif ($method eq 'StartRewrite') {
850 } elsif ($method eq 'RecordBreakwaterTip') {
851 $breakwater = $build;
853 } elsif ($method eq 'DgitImportDebianUpdate') {
854 $read_tree_debian->($cltree);
855 } elsif ($method eq 'DgitImportUpstreamUpdate') {
856 confess unless $rewriting;
857 my $differs = (get_differs $build, $cltree);
858 next unless $differs & D_UPS;
859 $read_tree_upstream->($cltree);
860 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
865 my $procd = (pop @processed) // 'UNDEF';
868 printdebug "WALK REWRITING NOW cl=$cl procd=$procd\n";
871 my $newtree = cmdoutput @git, qw(write-tree);
873 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
874 $ch =~ s{^parent .*\n}{}mg;
875 $ch =~ s{(?=^author)}{
876 join '', map { "parent $_\n" } @parents
877 }me or confess "$ch ?";
879 $ch =~ s{^committer .*$}{$committer_authline}m
882 my $cf = "$rd/m$rewriting";
883 open CD, ">", $cf or die $!;
884 print CD $ch, "\n", $cl->{Msg} or die $!;
886 my @cmd = (@git, qw(hash-object));
887 push @cmd, qw(-w) if $rewriting;
888 push @cmd, qw(-t commit), $cf;
889 my $newcommit = cmdoutput @cmd;
890 confess "$ch ?" unless $rewriting or $newcommit eq $cl->{CommitId};
892 if (grep { $method eq $_ } qw(DgitImportUpstreamUpdate)) {
898 my $final_check = get_differs $build, $input;
899 die sprintf "internal error %#x %s %s", $final_check, $build, $input
900 if $final_check & ~D_PAT_ADD;
902 my @r = ($build, $breakwater, $last_anchor);
903 printdebug "*** WALK RETURN @r\n";
908 git_check_unmodified();
909 return git_rev_parse qw(HEAD);
912 sub update_head ($$$) {
913 my ($old, $new, $mrest) = @_;
914 push @deferred_updates, "update HEAD $new $old";
915 run_deferred_updates $mrest;
918 sub update_head_checkout ($$$) {
919 my ($old, $new, $mrest) = @_;
920 update_head $old, $new, $mrest;
921 runcmd @git, qw(reset --hard);
924 sub update_head_postlaunder ($$$) {
925 my ($old, $tip, $reflogmsg) = @_;
926 return if $tip eq $old;
927 print "git-debrebase: laundered (head was $old)\n";
928 update_head $old, $tip, $reflogmsg;
929 # no tree changes except debian/patches
930 runcmd @git, qw(rm --quiet --ignore-unmatch -rf debian/patches);
933 sub do_launder_head ($) {
934 my ($reflogmsg) = @_;
935 my $old = get_head();
937 my ($tip,$breakwater) = walk $old;
939 update_head_postlaunder $old, $tip, $reflogmsg;
940 return ($tip,$breakwater);
943 sub cmd_launder_v0 () {
944 badusage "no arguments to launder-v0 allowed" if @ARGV;
945 my $old = get_head();
946 my ($tip,$breakwater,$last_anchor) = walk $old;
947 update_head_postlaunder $old, $tip, 'launder';
948 printf "# breakwater tip\n%s\n", $breakwater;
949 printf "# working tip\n%s\n", $tip;
950 printf "# last anchor\n%s\n", $last_anchor;
953 sub defaultcmd_rebase () {
954 push @ARGV, @{ $opt_defaultcmd_interactive // [] };
955 my ($tip,$breakwater) = do_launder_head 'launder for rebase';
956 runcmd @git, qw(rebase), @ARGV, $breakwater if @ARGV;
960 die if ($ARGV[0]//'') =~ m/^-/;
961 badusage "too many arguments to analyse" if @ARGV>1;
964 $old = git_rev_parse $old;
966 $old = git_rev_parse 'HEAD';
968 my ($dummy,$breakwater) = walk $old, 1,*STDOUT;
969 STDOUT->error and die $!;
972 sub ffq_prev_branchinfo () {
973 # => ('status', "message", [$current, $ffq_prev, $gdrlast])
975 # branch message is undef
976 # weird-symref } no $current,
977 # notbranch } no $ffq_prev
978 my $current = git_get_symref();
979 return ('detached', 'detached HEAD') unless defined $current;
980 return ('weird-symref', 'HEAD symref is not to refs/')
981 unless $current =~ m{^refs/};
982 my $ffq_prev = "refs/$ffq_refprefix/$'";
983 my $gdrlast = "refs/$gdrlast_refprefix/$'";
984 printdebug "ffq_prev_branchinfo branch current $current\n";
985 return ('branch', undef, $current, $ffq_prev, $gdrlast);
988 sub record_ffq_prev_deferred () {
989 # => ('status', "message")
991 # deferred message is undef
996 # if not ff from some branch we should be ff from, is an snag
997 # if "deferred", will have added something about that to
998 # @deferred_update_messages, and also maybe printed (already)
999 # some messages about ff checks
1000 my ($status, $message, $current, $ffq_prev, $gdrlast)
1001 = ffq_prev_branchinfo();
1002 return ($status, $message) unless $status eq 'branch';
1004 my $currentval = get_head();
1006 my $exists = git_get_ref $ffq_prev;
1007 return ('exists',"$ffq_prev already exists") if $exists;
1009 return ('not-branch', 'HEAD symref is not to refs/heads/')
1010 unless $current =~ m{^refs/heads/};
1013 my @check_specs = split /\;/, (cfg "branch.$branch.ffq-ffrefs",1) // '*';
1016 printdebug "ffq check_specs @check_specs\n";
1019 my ($lrref, $desc) = @_;
1020 printdebug "ffq might check $lrref ($desc)\n";
1022 for my $chk (@check_specs) {
1024 $invert = $glob =~ s{^[!^]}{};
1025 last if fnmatch $glob, $lrref;
1028 my $lrval = git_get_ref $lrref;
1029 return unless defined $lrval;
1031 if (is_fast_fwd $lrval, $currentval) {
1032 print "OK, you are ahead of $lrref\n" or die $!;
1033 $checked{$lrref} = 1;
1034 } elsif (is_fast_fwd $currentval, $lrval) {
1035 $checked{$lrref} = -1;
1036 snag 'behind', "you are behind $lrref, divergence risk";
1038 $checked{$lrref} = -1;
1039 snag 'diverged', "you have diverged from $lrref";
1043 my $merge = cfg "branch.$branch.merge",1;
1044 if (defined $merge and $merge =~ m{^refs/heads/}) {
1046 printdebug "ffq merge $rhs\n";
1047 my $check_remote = sub {
1048 my ($remote, $desc) = @_;
1049 printdebug "ffq check_remote ".($remote//'undef')." $desc\n";
1050 return unless defined $remote;
1051 $check->("refs/remotes/$remote/$rhs", $desc);
1053 $check_remote->((scalar cfg "branch.$branch.remote",1),
1054 'remote fetch/merge branch');
1055 $check_remote->((scalar cfg "branch.$branch.pushRemote",1) //
1056 (scalar cfg "branch.$branch.pushDefault",1),
1057 'remote push branch');
1059 if ($branch =~ m{^dgit/}) {
1060 $check->("refs/remotes/dgit/$branch", 'remote dgit branch');
1061 } elsif ($branch =~ m{^master$}) {
1062 $check->("refs/remotes/dgit/dgit/sid", 'remote dgit branch for sid');
1067 push @deferred_updates, "update $ffq_prev $currentval $git_null_obj";
1068 push @deferred_updates, "delete $gdrlast";
1069 push @deferred_update_messages, "Recorded current head for preservation";
1070 return ('deferred', undef);
1073 sub record_ffq_auto () {
1074 my ($status, $message) = record_ffq_prev_deferred();
1075 if ($status eq 'deferred' || $status eq 'exists') {
1077 snag $status, "could not record ffq-prev: $message";
1082 sub ffq_prev_info () {
1083 # => ($ffq_prev, $gdrlast, $ffq_prev_commitish)
1084 my ($status, $message, $current, $ffq_prev, $gdrlast)
1085 = ffq_prev_branchinfo();
1086 if ($status ne 'branch') {
1087 snag $status, "could not check ffq-prev: $message";
1090 my $ffq_prev_commitish = $ffq_prev && git_get_ref $ffq_prev;
1091 return ($ffq_prev, $gdrlast, $ffq_prev_commitish);
1094 sub stitch ($$$$$) {
1095 my ($old_head, $ffq_prev, $gdrlast, $ffq_prev_commitish, $prose) = @_;
1097 push @deferred_updates, "delete $ffq_prev $ffq_prev_commitish";
1099 if (is_fast_fwd $old_head, $ffq_prev_commitish) {
1100 my $differs = get_differs $old_head, $ffq_prev_commitish;
1101 unless ($differs & ~D_PAT_ADD) {
1102 # ffq-prev is ahead of us, and the only tree changes it has
1103 # are possibly addition of things in debian/patches/.
1104 # Just wind forwards rather than making a pointless pseudomerge.
1105 push @deferred_updates,
1106 "update $gdrlast $ffq_prev_commitish $git_null_obj";
1107 update_head_checkout $old_head, $ffq_prev_commitish,
1108 "stitch (fast forward)";
1113 # We make pseudomerges with L as the contributing parent.
1114 # This makes git rev-list --first-parent work properly.
1115 my $new_head = make_commit [ $old_head, $ffq_prev ], [
1116 'Declare fast forward / record previous work',
1117 "[git-debrebase pseudomerge: $prose]",
1119 push @deferred_updates, "update $gdrlast $new_head $git_null_obj";
1120 update_head $old_head, $new_head, "stitch: $prose";
1123 sub do_stitch ($;$) {
1124 my ($prose, $unclean) = @_;
1126 my ($ffq_prev, $gdrlast, $ffq_prev_commitish) = ffq_prev_info();
1127 if (!$ffq_prev_commitish) {
1128 fail "No ffq-prev to stitch." unless $opt_noop_ok;
1131 my $dangling_head = get_head();
1133 keycommits $dangling_head, $unclean,$unclean,$unclean;
1136 stitch($dangling_head, $ffq_prev, $gdrlast, $ffq_prev_commitish, $prose);
1139 sub cmd_new_upstream_v0 () {
1140 # automatically and unconditionally launders before rebasing
1141 # if rebase --abort is used, laundering has still been done
1145 badusage "need NEW-VERSION [UPS-COMMITTISH]" unless @ARGV >= 1;
1147 # parse args - low commitment
1148 my $new_version = (new Dpkg::Version scalar(shift @ARGV), check => 1);
1149 my $new_upstream_version = $new_version->version();
1151 my $new_upstream = git_rev_parse (shift @ARGV // 'upstream');
1156 my ($n, @x) = @_; # may be ''
1157 my $pc = $pieces{$n} //= {
1159 Desc => ($n ? "upstream piece \`$n'" : "upstream (main piece"),
1161 while (my $k = shift @x) { $pc->{$k} = shift @x; }
1166 my $newpiece = sub {
1167 my ($n, @x) = @_; # may be ''
1168 my $pc = $piece->($n, @x, NewIx => (scalar @newpieces));
1169 push @newpieces, $pc;
1174 New => $new_upstream,
1176 while (@ARGV && $ARGV[0] !~ m{^-}) {
1177 my $n = shift @ARGV;
1179 badusage "for each EXTRA-UPS-NAME need EXTRA-UPS-COMMITISH"
1180 unless @ARGV && $ARGV[0] !~ m{^-};
1182 my $c = git_rev_parse shift @ARGV;
1183 die unless $n =~ m/^$extra_orig_namepart_re$/;
1184 $newpiece->($n, New => $c);
1187 # now we need to investigate the branch this generates the
1188 # laundered version but we don't switch to it yet
1189 my $old_head = get_head();
1190 my ($old_laundered_tip,$old_bw,$old_anchor) = walk $old_head;
1192 my $old_bw_cl = classify $old_bw;
1193 my $old_anchor_cl = classify $old_anchor;
1195 if (!$old_anchor_cl->{OrigParents}) {
1196 snag 'anchor-treated',
1197 'old anchor is recognised due to --anchor, cannot check upstream';
1199 $old_upstream = parsecommit
1200 $old_anchor_cl->{OrigParents}[0]{CommitId};
1201 $piece->('', Old => $old_upstream->{CommitId});
1204 if ($old_upstream && $old_upstream->{Msg} =~ m{^\[git-debrebase }m) {
1205 if ($old_upstream->{Msg} =~
1206 m{^\[git-debrebase upstream-combine (\.(?: $extra_orig_namepart_re)+)\:.*\]$}m
1208 my @oldpieces = (split / /, $1);
1209 my $old_n_parents = scalar @{ $old_upstream->{Parents} };
1210 if (@oldpieces != $old_n_parents) {
1211 snag 'upstream-confusing', sprintf
1212 "previous upstream combine %s".
1213 " mentions %d pieces (each implying one orig commit)".
1214 " but has %d parents",
1215 $old_upstream->{CommitId},
1216 (scalar @oldpieces),
1218 } elsif ($oldpieces[0] ne '.') {
1219 snag 'upstream-confusing', sprintf
1220 "previous upstream combine %s".
1221 " first piece is not \`.'",
1225 foreach my $i (0..$#oldpieces) {
1226 my $n = $oldpieces[$i];
1227 $piece->($n, Old => $old_upstream->{CommitId}.'^'.($i+1));
1231 snag 'upstream-confusing',
1232 "previous upstream $old_upstream->{CommitId} is from".
1233 " git-debrebase but not an \`upstream-combine' commit";
1237 foreach my $pc (values %pieces) {
1238 if (!$old_upstream) {
1239 # we have complained already
1240 } elsif (!$pc->{Old}) {
1241 snag 'upstream-new-piece',
1242 "introducing upstream piece \`$pc->{Name}'";
1243 } elsif (!$pc->{New}) {
1244 snag 'upstream-rm-piece',
1245 "dropping upstream piece \`$pc->{Name}'";
1246 } elsif (!is_fast_fwd $pc->{Old}, $pc->{New}) {
1247 snag 'upstream-not-ff',
1248 "not fast forward: $pc->{Name} $pc->{Old}..$pc->{New}";
1252 printdebug "%pieces = ", (dd \%pieces), "\n";
1253 printdebug "\@newpieces = ", (dd \@newpieces), "\n";
1261 my @upstream_merge_parents;
1264 push @upstream_merge_parents, $old_upstream->{CommitId};
1267 foreach my $pc (@newpieces) { # always has '' first
1269 read_tree_subdir $pc->{Name}, $pc->{New};
1271 runcmd @git, qw(read-tree), $pc->{New};
1273 push @upstream_merge_parents, $pc->{New};
1276 # index now contains the new upstream
1278 if (@newpieces > 1) {
1279 # need to make the upstream subtree merge commit
1280 $new_upstream = make_commit \@upstream_merge_parents,
1281 [ "Combine upstreams for $new_upstream_version",
1282 ("[git-debrebase upstream-combine . ".
1283 (join " ", map { $_->{Name} } @newpieces[1..$#newpieces]).
1288 # $new_upstream is either the single upstream commit, or the
1289 # combined commit we just made. Either way it will be the
1290 # "upstream" parent of the anchor merge.
1292 read_tree_subdir 'debian', "$old_bw:debian";
1294 # index now contains the anchor merge contents
1295 $new_bw = make_commit [ $old_bw, $new_upstream ],
1296 [ "Update to upstream $new_upstream_version",
1297 "[git-debrebase anchor: new upstream $new_upstream_version, merge]",
1300 my $clogsignoff = cmdoutput qw(git show),
1301 '--pretty=format:%an <%ae> %aD',
1304 # Now we have to add a changelog stanza so the Debian version
1306 die if unlink "debian";
1307 die $! unless $!==ENOENT or $!==ENOTEMPTY;
1308 unlink "debian/changelog" or $!==ENOENT or die $!;
1309 mkdir "debian" or die $!;
1310 open CN, ">", "debian/changelog" or die $!;
1311 my $oldclog = git_cat_file ":debian/changelog";
1312 $oldclog =~ m/^($package_re) \(\S+\) / or
1313 fail "cannot parse old changelog to get package name";
1315 print CN <<END, $oldclog or die $!;
1316 $p ($new_version) UNRELEASED; urgency=medium
1318 * Update to new upstream version $new_upstream_version.
1324 runcmd @git, qw(update-index --add --replace), 'debian/changelog';
1326 # Now we have the final new breakwater branch in the index
1327 $new_bw = make_commit [ $new_bw ],
1328 [ "Update changelog for new upstream $new_upstream_version",
1329 "[git-debrebase: new upstream $new_upstream_version, changelog]",
1333 # we have constructed the new breakwater. we now need to commit to
1334 # the laundering output, because git-rebase can't easily be made
1335 # to make a replay list which is based on some other branch
1337 update_head_postlaunder $old_head, $old_laundered_tip,
1338 'launder for new upstream';
1340 my @cmd = (@git, qw(rebase --onto), $new_bw, $old_bw, @ARGV);
1342 # now it's for the user to sort out
1345 sub cmd_record_ffq_prev () {
1346 badusage "no arguments allowed" if @ARGV;
1347 my ($status, $msg) = record_ffq_prev_deferred();
1348 if ($status eq 'exists' && $opt_noop_ok) {
1349 print "Previous head already recorded\n" or die $!;
1350 } elsif ($status eq 'deferred') {
1351 run_deferred_updates 'record-ffq-prev';
1353 fail "Could not preserve: $msg";
1358 badusage "no arguments allowed" if @ARGV;
1359 my ($anchor, $bw) = keycommits +(git_rev_parse 'HEAD'), 0,0;
1360 print "$bw\n" or die $!;
1363 sub cmd_breakwater () {
1364 badusage "no arguments allowed" if @ARGV;
1365 my ($anchor, $bw) = keycommits +(git_rev_parse 'HEAD'), 0,0;
1366 print "$bw\n" or die $!;
1370 my $prose = 'stitch';
1371 GetOptions('prose=s', \$prose) or die badusage("bad options to stitch");
1372 badusage "no arguments allowed" if @ARGV;
1373 do_stitch $prose, 0;
1375 sub cmd_prepush () { cmd_stitch(); }
1378 badusage "no arguments allowed" if @ARGV;
1379 do_launder_head 'launder for git-debrebase quick';
1383 sub cmd_conclude () {
1384 my ($ffq_prev, $gdrlast, $ffq_prev_commitish) = ffq_prev_info();
1385 if (!$ffq_prev_commitish) {
1386 fail "No ongoing git-debrebase session." unless $opt_noop_ok;
1389 my $dangling_head = get_head();
1391 badusage "no arguments allowed" if @ARGV;
1392 do_launder_head 'launder for git-debrebase quick';
1396 sub make_patches_staged ($) {
1398 # Produces the patches that would result from $head if it were
1400 my ($secret_head, $secret_bw, $last_anchor) = walk $head;
1403 runcmd @git, qw(checkout -q -b bw), $secret_bw;
1404 runcmd @git, qw(checkout -q -b patch-queue/bw), $secret_head;
1405 runcmd qw(gbp pq export);
1406 runcmd @git, qw(add debian/patches);
1410 sub make_patches ($) {
1412 keycommits $head, 0, \&snag;
1413 make_patches_staged $head;
1416 my $ptree = cmdoutput @git, qw(write-tree --prefix=debian/patches/);
1417 runcmd @git, qw(read-tree), $head;
1418 read_tree_subdir 'debian/patches', $ptree;
1419 $out = make_commit [$head], [
1420 'Commit patch queue (exported by git-debrebase)',
1421 '[git-debrebase: export and commit patches]',
1424 my $d = get_differs $head, $out;
1426 return undef; # nothing to do
1427 } elsif ($d == D_PAT_ADD) {
1430 fail "Patch export produced patch amendments".
1431 " (abandoned output commit $out).".
1432 " Try laundering first.";
1436 sub cmd_make_patches () {
1437 badusage "no arguments allowed" if @ARGV;
1438 my $old_head = get_head();
1439 my $new = make_patches $old_head;
1442 fail "No (more) patches to export." unless $opt_noop_ok;
1445 update_head_checkout $old_head, $new, 'make-patches';
1448 sub cmd_convert_from_gbp () {
1449 badusage "needs 1 optional argument, the upstream git rev"
1451 my ($upstream_spec) = @ARGV;
1452 $upstream_spec //= 'refs/heads/upstream';
1453 my $upstream = git_rev_parse $upstream_spec;
1454 my $old_head = get_head();
1456 my $upsdiff = get_differs $upstream, $old_head;
1457 if ($upsdiff & D_UPS) {
1458 runcmd @git, qw(--no-pager diff),
1459 $upstream, $old_head,
1460 qw( -- :!/debian :/);
1461 fail "upstream ($upstream_spec) and HEAD are not identical in upstream files";
1464 if (!is_fast_fwd $upstream, $old_head) {
1465 snag 'upstream-not-ancestor',
1466 "upstream ($upstream) is not an ancestor of HEAD";
1468 my $wrong = cmdoutput
1469 (@git, qw(rev-list --ancestry-path), "$upstream..HEAD",
1470 qw(-- :/ :!/debian));
1471 if (length $wrong) {
1472 snag 'unexpected-upstream-changes',
1473 "history between upstream ($upstream) and HEAD contains direct changes to upstream files - are you sure this is a gbp (patches-unapplied) branch?";
1474 print STDERR "list expected changes with: git log --stat --ancestry-path $upstream_spec..HEAD -- :/ ':!/debian'\n";
1478 if ((git_cat_file "$upstream:debian")[0] ne 'missing') {
1479 snag 'upstream-has-debian',
1480 "upstream ($upstream) contains debian/ directory";
1489 runcmd @git, qw(checkout -q -b gdr-internal), $old_head;
1490 # make a branch out of the patch queue - we'll want this in a mo
1491 runcmd qw(gbp pq import);
1492 # strip the patches out
1493 runcmd @git, qw(checkout -q gdr-internal~0);
1494 rm_subdir_cached 'debian/patches';
1495 $work = make_commit ['HEAD'], [
1496 'git-debrebase convert-from-gbp: drop patches from tree',
1497 'Delete debian/patches, as part of converting to git-debrebase format.',
1498 '[git-debrebase convert-from-gbp: drop patches from tree]'
1500 # make the anchor merge
1501 # the tree is already exactly right
1502 $work = make_commit [$work, $upstream], [
1503 'git-debrebase import: declare upstream',
1504 'First breakwater merge.',
1505 '[git-debrebase anchor: declare upstream]'
1508 # rebase the patch queue onto the new breakwater
1509 runcmd @git, qw(reset --quiet --hard patch-queue/gdr-internal);
1510 runcmd @git, qw(rebase --quiet --onto), $work, qw(gdr-internal);
1511 $work = git_rev_parse 'HEAD';
1514 update_head_checkout $old_head, $work, 'convert-from-gbp';
1517 sub cmd_convert_to_gbp () {
1518 badusage "no arguments allowed" if @ARGV;
1519 my $head = get_head();
1520 my (undef, undef, undef, $ffq, $gdrlast) = ffq_prev_branchinfo();
1521 keycommits $head, 0;
1523 make_patches_staged $head;
1525 $out = make_commit ['HEAD'], [
1526 'Commit patch queue (converted from git-debrebase format)',
1527 '[git-debrebase convert-to-gbp: commit patches]',
1531 push @deferred_updates, "delete $ffq";
1532 push @deferred_updates, "delete $gdrlast";
1535 update_head_checkout $head, $out, "convert to gbp (v0)";
1536 print <<END or die $!;
1537 git-debrebase: converted to git-buildpackage branch format
1538 git-debrebase: WARNING: do not now run "git-debrebase" any more
1539 git-debrebase: WARNING: doing so would drop all upstream patches!
1543 sub cmd_downstream_rebase_launder_v0 () {
1544 badusage "needs 1 argument, the baseline" unless @ARGV==1;
1546 $base = git_rev_parse $base;
1547 my $old_head = get_head();
1548 my $current = $old_head;
1551 if ($current eq $base) {
1552 $topmost_keep //= $current;
1553 print " $current BASE stop\n";
1556 my $cl = classify $current;
1557 print " $current $cl->{Type}";
1559 my $p0 = $cl->{Parents}[0]{CommitId};
1561 if ($cl->{Type} eq 'Pseudomerge') {
1562 print " ^".($cl->{Contributor}{Ix}+1);
1563 $next = $cl->{Contributor}{CommitId};
1564 } elsif ($cl->{Type} eq 'AddPatches' or
1565 $cl->{Type} eq 'Changelog') {
1575 $topmost_keep //= $current;
1577 die "to-be stripped changes not on top of the branch\n"
1582 if ($topmost_keep eq $old_head) {
1583 print "unchanged\n";
1585 print "updating to $topmost_keep\n";
1586 update_head_checkout
1587 $old_head, $topmost_keep,
1588 'downstream-rebase-launder-v0';
1592 GetOptions("D+" => \$debuglevel,
1593 'noop-ok', => \$opt_noop_ok,
1594 'f=s' => \@snag_force_opts,
1595 'anchor=s' => \@opt_anchors,
1598 my ($opt,$val) = @_;
1599 badusage "git-debrebase: no cuddling to -i for git-rebase"
1601 die if $opt_defaultcmd_interactive; # should not happen
1602 $opt_defaultcmd_interactive = [ qw(-i) ];
1603 # This access to @ARGV is excessive familiarity with
1604 # Getopt::Long, but there isn't another sensible
1605 # approach. '-i=s{0,}' does not work with bundling.
1606 push @$opt_defaultcmd_interactive, @ARGV;
1608 }) or die badusage "bad options\n";
1609 initdebug('git-debrebase ');
1610 enabledebug if $debuglevel;
1612 my $toplevel = cmdoutput @git, qw(rev-parse --show-toplevel);
1613 chdir $toplevel or die "chdir $toplevel: $!";
1615 $rd = fresh_playground "$playprefix/misc";
1617 @opt_anchors = map { git_rev_parse $_ } @opt_anchors;
1619 if (!@ARGV || $opt_defaultcmd_interactive || $ARGV[0] =~ m{^-}) {
1620 defaultcmd_rebase();
1622 my $cmd = shift @ARGV;
1625 $cmdfn = ${*::}{"cmd_$cmdfn"};
1627 $cmdfn or badusage "unknown git-debrebase sub-operation $cmd";