chiark / gitweb /
a00b4b5589f95617c1a5b265453c832ebcaecaac
[dgit.git] / git-debrebase
1 #!/usr/bin/perl -w
2 # git-debrebase
3 # Script helping make fast-forwarding histories while still rebasing
4 # upstream deltas when working on Debian packaging
5 #
6 # Copyright (C)2017 Ian Jackson
7 #
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.
12 #
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.
17 #
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/>.
20
21 # usages:
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
27 #
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>
32 #    git-ffrebase finish
33 #    git-ffrebase status [BRANCH]
34 #
35 #  refs/ffrebase-prev/BRANCH    BRANCH may be refs/...; if not it means
36 #  refs/ffrebase-base/BRANCH      refs/heads/BRANCH
37
38
39 use strict;
40
41 use Memoize;
42 use Data::Dumper;
43
44 use Debian::Dgit qw(:DEFAULT $wa);
45
46 sub cfg ($) {
47     my ($k) = @_;
48     $/ = "\0";
49     my @cmd = qw(git config -z);
50     push @cmd, qw(--get-all) if wantarray;
51     push @cmd, $k;
52     my $out = cmdoutput @cmd;
53     return split /\0/, $out;
54 }
55
56 memoize('cfg');
57
58 sub get_commit ($) {
59     my ($objid) = @_;
60     my ($type,$data) = git_cat_file $objid;
61     die unless $type eq 'commit';
62     $data =~ m/(?<=\n)\n/;
63     return ($`,$');
64 }
65
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; }
70
71 our $rd = ".git/git-debrebase";
72 our $ud = "$rd/work";
73
74 sub commit_pr_info ($) {
75     my ($r) = @_;
76     return Data::Dumper->dump([$r], [qw(commit)]);
77 }
78
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) ?";
84     return $&;
85 }
86
87 sub classify ($) {
88     my ($objid) = @_;
89
90     my ($h,$m) = get_commit $objid;
91
92     my ($t) = $h =~ m/^tree (\w+)$/m or die $cur;
93     my (@ph) = $h =~ m/^parent (\w+)$/m;
94     my @p;
95
96     my $r = {
97         CommitId => $objid,
98         Hdr => $hdr,
99         Msg => $m,
100         Tree => $t,
101         Parents => \@p,
102     };
103
104     foreach my $ph (@ph) {
105         push @p, {
106             Ix => $#p,
107             CommitId => $ph,
108             Differs => (get_differs $t, $ph),
109         };
110     }
111
112     my $classify = sub {
113         my ($type, @rest) = @_;
114         $r = { %r, Type => $type, @rest };
115         return $r;
116     };
117     my $unknown = sub {
118         my ($why) = @_;
119         $r = { %r, Type => Unknown };
120         return $r;
121     }
122
123     if (@p == 1) {
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));
135         } elsif ($d == 0) {
136             return $unknown->("no changes");
137         } else {
138             confess "internal error $objid ?";
139         }
140     }
141     if (!@p) {
142         return $unknown->("origin commit");
143     }
144
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]);
152     }
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} ?";
157             $1;
158         } @p;
159         return $classify->(qw(Pseudomerge),
160                            SubType => qw(Ambiguous),
161                            Overwritten => $bytime[0],
162                            Contributor => $bytime[1]);
163     }!
164     foreach my $p (@p) {
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;
168     }
169     my @orig_ps = grep { ($_->{IsDgitImport}//'X') eq 'orig' };
170     my $m2 = $m;
171     if (!(grep { !$_->{IsOrigin} } @p) and
172         (@origs >= @p - 1) and
173         $m2 =~ s{^\[(dgit import unpatched .*)\]$}{[was: $1]}m) {
174         $r->{NewMsg} = $m2;
175         return $classify->(qw(DgitImportUnpatched),
176                            OrigParents => \@orig_ps);
177     }
178
179     my ($stype, $series) = git_cat_file "$t:debian/patches/series";
180     my $haspatches = $stype ne 'missing' && $series =~ m/^\s*[^#\n\t ]/m;
181
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).
188     if (@p == 2 &&
189         !$haspatches &&
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),
194                            Upstream => $p[1]);
195     }
196
197     return $unknown->("complex merge");
198 }
199
200 sub launder ($;$) {
201     my ($cur, $wantdebonly) = @_;
202     # go through commits backwards
203     # we generate two lists of commits to apply
204     my (@deb_cl, @ups_cl);
205     my %found;
206     my @pseudomerges;
207
208     my $cl;
209     my $xmsg = sub {
210         my ($appendinfo) = @_;
211         my $ms = $cl->{Msg};
212         chomp $ms;
213         $ms .= "\n\n[git-debrebase $appendinfo]\n";
214         return (Msg => $ms);
215     };
216
217     for (;;) {
218         $cl = classify $cur;
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') {
224             $cur = $p0;
225             next;
226         } elsif ($ty eq 'Packaging') {
227             push @deb_cl, $cl;
228             $cur = $p0;
229             next;
230         } elsif ($ty eq 'Upstream') {
231             push @ups_cl, $cl;
232             $cur = $p0;
233             next;
234         } elsif ($ty eq 'Mixed') {
235             my $queue = sub {
236                 my ($q, $wh) = @_;
237                 my $cls = { $cl, $xmsg->("split mixed commit: $wh part") };
238                 push @$q, $cls;
239             };
240             $queue->(\@deb_cl, "debian");
241             $queue->(\@ups_cl, "upstream");
242             next;
243         } elsif ($ty eq 'Pseudomerge') {
244             push @pseudomerges, $cl;
245             $cur = $ty->{Contributor};
246             next;
247         } elsif ($ty eq 'BreakwaterUpstreamMerge') {
248             $basis = $cur;
249             last;
250         } elsif ($ty eq 'DgitImportUnpatched' &&
251                  @pseudomerges == 1) {
252             # This import has a tree which is just like a breakwater
253             # tree, but it has the wrong history.  Its ought to have
254             # the previous breakwater (which dgit ought to have
255             # generated a pseudomerge to overwrite) as an ancestor.
256             # That will make the history of the debian/ files correct.
257             # As for the upstream version: either it's the same upstream
258             # as the previous breakwater, in which case that history is
259             # precisely right.  Otherwise, it was a non-gitish upload
260             # of a new upstream version.  We can tell these apart
261             # by looking at the tree of the supposed upstream.
262             if ($differs & D_UPS) {
263                 push @deb_cl, {
264                     %r,
265                     SpecialMethod => 'DgitImportUpstreamUpdate',
266                     $xmsg->("convert dgit import: debian changes")
267                 };
268             }
269             push @deb_cl, {
270                 %r,
271                 SpecialMethod => 'DgitImportDebianUpdate',
272                 $xmsg->("convert dgit import: upstream changes")
273             };
274             my $differs = get_differs $previous_breakwater, $cl->{Tree};
275             $basis = launder $pseudomerges[0]{Overwritten}, 1;
276             last;
277         } else {
278             die "Reached difficult commit $cur: ".Dumper($cl);
279         }
280     }
281     # Now we build it back up again
282
283     workarea_fresh();
284     in_workarea sub { xxx attributes xxx };
285
286     my $build = $basis;
287
288     my $rm_tree_cached = sub {
289         my ($subdir) = @_;
290         runcmd @git, qw(rm --quiet -rf --cached), $subdir;
291     };
292     my $read_tree_debian = sub {
293         my ($treeish) = @_;
294         $rm_tree_cached->(qw(debian));
295         runcmd @git, qw(read-tree --prefix=debian/), "$treeish:debian";
296     };
297     my $read_tree_upstream = sub {
298         my ($treeish) = @_;
299         runcmd @git, qw(read-tree), $treeish;
300         $read_tree_debian->($build);
301     };
302  
303     my $committer_authline = calculate_committer_authline();
304
305     in_workarea sub {
306         mkdir $rd or $!==EEXIST or die $!;
307
308         my $current_method;
309         foreach my $cl (qw(Debian), @deb_cl, qw(Upstream), @ups_cl) {
310             if (!ref $cl) {
311                 $current_method = $cl;
312                 next;
313             }
314             $method = $cl->{SpecialMethod} // $current_method;
315             my @parents = ($build);
316             my $cltree = $cl->{CommitId}
317             if ($method eq 'Debian') {
318                 $read_tree_debian->($cltree);
319             } elsif ($method eq 'Upstream') {
320                 $read_tree_upstream->($cltree);
321             } elsif ($method eq 'DgitImportDebianUpdate') {
322                 $read_tree_debian->($cltree);
323                 $rm_tree_cached(qw(debian/patches));
324             } elsif ($method eq 'DgitImportUpstreamUpdate') {
325                 $read_tree_upstream->($cltree);
326                 push @parents, map { $_->{CommitId} } @{ $cl->{OrigParents} };
327             } else {
328                 confess "$method ?";
329             }
330             my $newtree = cmdoutput @git, qw(write-tree);
331             my $ch = $cl->{Msg};
332             $ch =~ s{^tree .*}{tree $newtree}m or confess "$ch ?";
333             $ch =~ s{^committer .*$}{$committer_authline}m or confess "$ch ?";
334             open CD, ">", "$rd/m" or die $!;
335             print CD $ch, "\n", $cl->{Msg}; or die $!;
336             close CD or die $!;
337             my $newcommit = cmdoutput @git, qw(hash-object -t commit), "$rd/m";
338             $build = $newcommit;
339         }
340     };
341 }
342
343
344
345 chdir $GIT_DIR
346
347                                
348 if ($ARGV[0] eq 'launder') {
349     launder();
350 }
351
352 use Data::Dumper;
353 print Dumper(cfg('wombat.foo.bar'));
354
355
356         ((git_cat_file "$t:debian/patches/series"
357         
358     
359     my @
360                            
361         
362
363             return $r;
364             $r->{Type} = '
365             $r->{Type} = '
366             return 
367             # changes on debian/patches, discard it
368             
369             $cur = $p[0];
370             next;
371         }
372         if ($d & DPAT) {
373             
374
375     ($r->{Tree},) = 
376     
377                     
378
379
380
381
382  when starting must record original start (for ff)
383  and new rebase basis
384