chiark / gitweb /
wip found
[topbloke.git] / tb-update.pl
1 #!/usr/bin/perl
2 # usage: tb-update [options]
3
4 use warnings;
5 use strict;
6
7 use Getopt::Long;
8 use Topbloke;
9
10 Getopt::Long::Configure(qw(bundling));
11
12 die "bad usage\n" if @ARGV;
13
14 check_clean_tree();
15
16
17 sub memo ($$$) { 
18     my ($memos,$key,$code) = @_;
19     return $memos->{$key} if exists $memos->{$key};
20     debug("----- $key");
21     $memos->{$key} = $code->();
22 }
23
24 sub merge_base ($$) {
25     my ($r,$s) = @_; # refs, ideally
26     our %memos;
27     return memo(\%memos, "$r $s", sub {
28         run_git_1line(qw(merge-base), $r, $s);
29                 });
30 }
31
32 sub commit_date ($) {
33     my ($ref) = @_;
34     my $l = run_git_1line(qw(git-log --date=raw -n1 --pretty=format:%cd), $ref);
35     $l =~ m/^(\d+)\s/ or die;
36     return $l;
37 }
38
39 sub compare_source_ages ($$) {
40     my ($r,$s) = @_; # refs, returns something like  age($r) cmp age($s)
41     our %memos;
42     return memo(\%memos, "$r $s", sub {
43         my $mb = merge_base($r, $s);
44         return -($mb eq $r) cmp ($mb eq $s)
45             # if merge base is $a then $a must be before $b
46             # ie the commit equal to the merge base is earlier
47             or (commit_date($r) cmp commit_date($s));
48     });
49 }
50
51 sub update_base ($) {
52     my ($patch) = @_;
53
54     for (;;) {
55         check_baseref_metadata("$baserefs/$patch");
56
57         my $head = git_run_1line(qw(rev-parse),"$baserefs/$patch");
58
59         # 2.i. Compute set of desired included deps
60         my %desired;
61         my $add_desired = sub {
62             my ($sub) = @_;
63             my ($obk,$deps) = git_get_object("$baserefs/$sub:.topbloke/deps");
64             die "$sub $obk ??" unless $obk eq 'blob';
65             foreach my $depline (split /\n/, $deps) {
66                 next if exists $desired{$depline};
67                 $desired{$depline} = 1;
68                 if ($depline =~ m/^- /) {
69                 } elsif ($depline =~ m/^-/) {
70                     die "$depline ?";
71                 } else {
72                     check_baseref_metadata("$baserefs/$depline");
73                     $add_desired->($depline);
74                 }
75             }
76         };
77         $add_desired->($patch);
78
79         # 2.ii. do the merges
80         # first, find the list of sources
81
82         my @sources;
83         my ($obk,$deps) = git_get_object("$baserefs/$patch:.topbloke/deps");
84         die "$patch $obk ??" unless $obk eq 'blob';
85         foreach my $depline (split /\n/, $deps) {
86             if ($depline =~ m/^- /) {
87                 push @sources, { Ref => "$'", Kind => 'foreign' };
88             } elsif ($depline =~ m/^-/) {
89                 die "$depline ?"; # should have failed earlier
90             } else {
91                 push @sources, { 
92                     Name => $depline,
93                     Ref => "$tiprefs/$depline", 
94                     Kind => 'topbloke',
95                 };
96             }
97         }
98
99         my ($obk,$tg) = git_get_object("$baserefs/$patch:.topbloke/topgit-");
100         if ($obk ne 'missing') {
101             $obj eq 'blob' or die "$patch $obk ??";
102             chomp $tg or die "$patch ??";
103             push @sources, {
104                 Name => "-topgit $tg",
105                 Ref => "refs/top-bases/$tg", 
106                 Kind => 'topgit',
107             };
108         }
109
110         # This bit involves rather too much history walking
111         # and could perhaps be optimised.
112
113         # Find the merge base for each source
114         foreach my $source (@sources) {
115             $source->{Head} = run_git_1line(qw(rev-parse), $source->{Ref});
116             $source->{MergeBase} = merge_base($head, "$baserefs/$patch");
117         }
118         # The merge base is contained in $head, so if it is equal
119         # to the source's head, the source is contained in $head -
120         # ie we are ahead of the source.  Skip those sources.
121         @sources = grep { $source->{MergeBase} ne $source->{Head} } @sources;
122
123         if (!@sources) {
124             print "$patch base is up to date\n" or die $!;
125             last;
126         }
127
128         my $best = $sources[0];
129         foreach my $source (@sources[1..$#sources]) {
130             next if compare_source_ages($best->{Ref}, $source->{Ref}) <= 0;
131             $best = $source;
132         }
133
134         my $sref = $source->{Ref};
135
136         if ($source->{Kind} eq 'topbloke') {
137             # Check for unwanted dependency removals
138             my (%source_inc,%anc_inc);
139             $source_inc{$_}=1 foreach split /\n/, 
140                 git_get_object("$sref:.topbloke/+included");
141             $anc_inc{$_}=1 foreach split /\n/, 
142                 git_get_object("$source->{MergeBase}:.topbloke/+included");
143             my @unwanted_dr;
144             foreach my $dep (keys %desired) {
145                 next if $source_inc{$dep};
146                 unless unless $anc_inc{$dep};
147                 my $unw_dr = { Name => $dep };
148                 run_git(sub {
149                     my ($cand, @parents) = split;
150                     return if dep_included_in($dep, $cand);
151                     return if grep { !dep_included_in($dep, $_) } @parents;
152                     # OK, it's missing from $cand but included in
153                     # all of $cand's parents.
154                     
155                     },
156                         qw(git-rev-list --date-order --remove-empty)
157                         '--pretty=format:%H %P%n',
158                         $dep, '--', '.topbloke/+included');
159                 
160                 
161                 push @unwanted_dr, { Name => $dep };
162             
163
164 sub done ($) { 
165     our %done; 
166     return 1 if $done{$_[0]}++;
167     debug("----- $key");
168 }
169
170 sub memo ($$) { 
171     my ($key,$code) = @_;
172     our %memo;
173     return $memo{$key} if exists $memo{$key};
174     debug("----- $key");
175     $memo{$key} = $code->();
176 }
177
178 sub tip_sources_core
179
180 sub tip_sources ($) {
181     my ($patch) = @_;
182     my @sources = ();
183     push @sources, { How => 'base', Ref => "$baserefs/$patch" };
184     foreach my $remote (get_configured_remotes()) {
185         push @sources, { How => 'remote', 
186                          Ref => "refs/remotes/$remote/topbloke-tips/$patch" };
187     }
188
189 sub compute_desired_deps ($) {
190     my ($patch) = @_;
191  return memo("compute_desired_deps $patch", {
192      foreach my $sourceref (tip_source_refs($patch)) {
193          die...
194  });
195 }
196
197 sub update_base ($) {
198     my ($patch) = @_;
199     return if done("update_base $patch");
200     my @desired_deps = compute_desired_deps($patch);
201     die...
202
203 sub update_deps ($) {
204     my $patch = @_;
205     return if done("update_deps $patch");
206     my $deps = git_get_object("$baserefs/$patch:.topbloke/deps");
207     foreach my $dep (split /\n/, $deps) {
208         if ($dep =~ m/^tb /) {
209             my $dep_patch = $'; #';
210             update_patch($dep_patch);
211         }
212     }
213 }
214
215 sub update_patch ($) {
216     my $patch = @_;
217     update_deps($patch);
218     update_base($patch);
219     update_tip($patch);
220 }
221
222 our $current = current_branch();
223 if ($current->{Kind} eq 'tip') {
224     update_patch($current);
225 } elsif ($current->{Kind} eq 'base') {
226     update_deps($current);
227     update_base($current);
228 } else {
229     die "Not a topbloke branch ($current->{Kind})\n";
230 }