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/>.
22 # git-debrebase launder # prints breakwater tip
23 # git-debrebase analyse
24 # git-debrebase start # like ffrebase start + debrebase launder
25 # git-debrebase new-upstream [-f] UPSTREAM
26 # git-debrebase <git-rebase options> # does debrebase start if necessary
28 # git-ffrebase start [BASE] # records previous HEAD so it can be overwritten
29 # # records base for future git-ffrebase
30 # git-ffrebase set-base BASE
31 # git-ffrebase <git-rebase options>
33 # git-ffrebase status [BRANCH]
35 # refs/ffrebase-prev/BRANCH BRANCH may be refs/...; if not it means
36 # refs/ffrebase-base/BRANCH refs/heads/BRANCH
37 # zero, one, or both of these may exist
44 use Debian::Dgit qw(:DEFAULT $wa);
49 my @cmd = qw(git config -z);
50 push @cmd, qw(--get-all) if wantarray;
52 my $out = cmdoutput @cmd;
53 return split /\0/, $out;
60 my ($type,$data) = git_cat_file $objid;
61 die unless $type eq 'commit';
62 $data =~ m/(?<=\n)\n/;
66 sub D_DEB () { return 0x1; }
67 sub D_UPS () { return 0x2; }
68 sub D_PAT_ADD () { return 0x4; }
69 sub D_PAT_OTH () { return 0x8; }
71 our $rd = ".git/git-debrebase";
74 sub commit_pr_info ($) {
76 return Data::Dumper->dump([$r], [qw(commit)]);
79 sub calculate_committer_authline () {
80 my $c = cmdoutput @git, qw(commit-tree --no-gpg-sign -m),
81 'XXX DUMMY COMMIT (git-debrebase)', "$basis:";
82 my ($h,$m) = get_commit $c;
83 $h =~ m/^committer .*$/m or confess "($h) ?";
90 my ($h,$m) = get_commit $objid;
92 my ($t) = $h =~ m/^tree (\w+)$/m or die $cur;
93 my (@ph) = $h =~ m/^parent (\w+)$/m;
104 foreach my $ph (@ph) {
108 Differs => (get_differs $t, $ph),
113 my ($type, @rest) = @_;
114 $r = { %r, Type => $type, @rest };
119 $r = { %r, Type => Unknown };
124 my $d = $r->{Parents}[0]{Differs};
125 if ($d == D_DPAT_ADD) {
126 return $classify->(qw(AddPatches));
127 } elsif ($d & (D_DPAT_ADD|D_DPAT_OTH)) {
128 return $unknown->("edits debian/patches");
129 } elsif ($d == D_DEB) {
130 return $classify->(qw(Packaging));
131 } elsif ($d == D_UPS) {
132 return $classify->(qw(Upstream));
133 } elsif ($d == D_DEB|D_UPS) {
134 return $classify->(qw(Mixed));
136 return $unknown->("no changes");
138 confess "internal error $objid ?";
142 return $unknown->("origin commit");
145 my @identical = grep { !$_->{Differs} } @p;
146 if (@p == 2 && @identical == 1) {
147 my @overwritten = grep { $_->{Differs} } @p;
148 confess "internal error $objid ?" unless @overwritten==1;
149 return $classify->(qw(Pseudomerge),
150 Overwritten => $overwritten[0],
151 Contributor => $identical[0]);
153 if (@p == 2 && @identical == 2) {
154 my @bytime = nsort_by {
155 my ($ph,$pm) = get_commit $_->{CommitId};
156 $ph =~ m/^committer .* (\d+) [-+]\d+$/m or die "$_->{CommitId} ?";
159 return $classify->(qw(Pseudomerge),
160 SubType => qw(Ambiguous),
161 Overwritten => $bytime[0],
162 Contributor => $bytime[1]);
165 my ($p_h, $p_m) = get_commit $p;
166 $p->{IsOrigin} = $p_h !~ m/^parent \w+$/m;
167 ($p->{IsDgitImport},) = $p_m =~ m/^\[dgit import ([0-9a-z]+) .*\]$/m;
169 my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' };
171 if (!(grep { !$_->{IsOrigin} } @p) and
172 (@origs >= @p - 1) and
173 $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
175 return $classify->(qw(DgitImportUnpatched),
176 OrigParents => \@orig_ps);
179 my ($stype, $series) = git_cat_file "$t:debian/patches/series";
180 my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
182 # How to decide about l/r ordering of breakwater merges ? git
183 # --topo-order prefers to expand 2nd parent first. There's
184 # already an easy rune to look for debian/ history anyway (git log
185 # debian/) so debian breakwater branch should be 1st parent; that
186 # way also there's also an easy rune to look for the upstream
187 # patches (--topo-order).
190 !$p[0]{IsOrigin} && # breakwater merge never starts with an origin
191 !($p[0]{Differs} & ~D_DEB) &&
192 !($p[1]{Differs} & ~D_UPS)) {
193 return $classify->(qw(BreakwaterUpstreamMerge),
197 return $unknown->("complex merge");
201 my ($cur, $pseudos_must_overwrite, $wantdebonly) = @_;
202 # go through commits backwards
203 # we generate two lists of commits to apply
204 my (@deb_cl, @ups_cl);
210 my ($appendinfo) = @_;
213 $ms .= "\n\n[git-debrebase $appendinfo]\n";
219 my $ty = $cl->{Type};
220 my $st = $cl->{SubType};
221 $found{$ty. ( defined($st) ? "-$st" : '' )}++;
222 my $p0 = $cl->{Parents}[0]{CommitId};
223 if ($ty eq 'AddPatches') {
226 } elsif ($ty eq 'Packaging') {
230 } elsif ($ty eq 'Upstream') {
234 } elsif ($ty eq 'Mixed') {
237 my $cls = { $cl, $xmsg->("split mixed commit: $wh part") };
240 $queue->(\@deb_cl, "debian");
241 $queue->(\@ups_cl, "upstream");
243 } elsif ($ty eq 'Pseudomerge') {
244 if (defined $pseudos_must_overwrite) {
246 push @pseudomerges, $cl;
247 $cur = $ty->{Contributor};
249 } elsif ($ty eq 'BreakwaterUpstreamMerge') {
252 } elsif ($ty eq 'DgitImportUnpatched' &&
253 @pseudomerges == 1) {
254 # This import has a tree which is just like a breakwater
255 # tree, but it has the wrong history. Its ought to have
256 # the previous breakwater (which dgit ought to have
257 # generated a pseudomerge to overwrite) as an ancestor.
258 # That will make the history of the debian/ files correct.
259 # As for the upstream version: either it's the same upstream
260 # as the previous breakwater, in which case that history is
261 # precisely right. Otherwise, it was a non-gitish upload
262 # of a new upstream version. We can tell these apart
263 # by looking at the tree of the supposed upstream.
264 if ($differs & D_UPS) {
267 SpecialMethod => 'DgitImportUpstreamUpdate',
268 $xmsg->("convert dgit import: debian changes")
273 SpecialMethod => 'DgitImportDebianUpdate',
274 $xmsg->("convert dgit import: upstream changes")
276 my $differs = get_differs $previous_breakwater, $cl->{Tree};
277 $basis = launder $pseudomerges[0]{Overwritten}, 1;
280 die "Reached difficult commit $cur: ".Dumper($cl);
283 # Now we build it back up again
286 in_workarea sub { xxx attributes xxx };
290 my $rm_tree_cached = sub {
292 runcmd @git, qw(rm --quiet -rf --cached), $subdir;
294 my $read_tree_debian = sub {
296 $rm_tree_cached->(qw(debian));
297 runcmd @git, qw(read-tree --prefix=debian/), "$treeish:debian";
299 my $read_tree_upstream = sub {
301 runcmd @git, qw(read-tree), $treeish;
302 $read_tree_debian->($build);
305 my $committer_authline = calculate_committer_authline();
308 mkdir $rd or $!==EEXIST or die $!;
311 foreach my $cl (qw(Debian), @deb_cl, qw(Upstream), @ups_cl) {
313 $current_method = $cl;
316 $method = $cl->{SpecialMethod} // $current_method;
317 my @parents = ($build);
318 my $cltree = $cl->{CommitId}
319 if ($method eq 'Debian') {
320 $read_tree_debian->($cltree);
321 } elsif ($method eq 'Upstream') {
322 $read_tree_upstream->($cltree);
323 } elsif ($method eq 'DgitImportDebianUpdate') {
324 $read_tree_debian->($cltree);
325 $rm_tree_cached(qw(debian/patches));
326 } elsif ($method eq 'DgitImportUpstreamUpdate') {
327 $read_tree_upstream->($cltree);
328 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
332 my $newtree = cmdoutput @git, qw(write-tree);
334 $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
335 $ch =~ s{^committer .*$}{$committer_authline}m or confess "$ch ?";
336 open CD, ">", "$rd/m" or die $!;
337 print CD $ch, "\n", $cl->{Msg}; or die $!;
339 my $newcommit = cmdoutput @git, qw(hash-object -t commit), "$rd/m";
350 if ($ARGV[0] eq 'launder') {
355 print Dumper(cfg('wombat.foo.bar'));
358 ((git_cat_file "$t:debian/patches/series"
369 # changes on debian/patches, discard it
384 when starting must record original start (for ff)