14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
19 run_git run_git_1line run_git_check_nooutput
20 run_git_test_anyoutput
21 git_config git_dir chdir_toplevel
22 current_branch parse_patch_spec
23 setup_config check_no_unwanted_metadata
26 wf_start wf wf_abort wf_done wf_contents);
31 our $git_command = 'git';
35 print STDERR "DEBUG: $msg\n" or die $!;
39 # takes optional prefix arguments:
40 # coderef hook to call for each line read,
41 # with $_ containing chomped line; if not supplied,
43 # scalarref place to store exit status; if not supplied,
44 # nonzero exit status is fatal
45 my ($estatusr,$linecallr);
48 if (ref $ref eq 'SCALAR') {
50 } elsif (ref $ref eq 'CODE') {
53 die ref($ref)." @_ ?";
56 open GIT, "-|", $git_command, @_ or die $!;
59 chomp or die "$git_command @_ gave $_ ?";
65 die "$git_command @_ $!" if $!;
67 die "$git_command @_ ($?)" unless $estatusr;
70 $$estatusr = 0 if $estatusr;
76 run_git(sub { $l = $_; }, @_);
77 die "git @_ ?" unless defined $l;
81 sub run_git_check_nooutput {
82 my ($what) = shift @_;
83 run_git(sub { die "$what $_\n"; }, @_);
86 sub run_git_test_anyoutput {
88 run_git(sub { $any=1; }, @_);
92 sub git_get_object ($) {
94 our ($gro_pid, $gro_out, $gro_in);
96 $gro_pid = IPC::Open2::open2($gro_out, $gro_in,
97 $git_command, qw(cat-file --batch))
100 $SIG{'PIPE'} = 'IGN';
101 print $gro_in $objname,"\n" or die $!;
102 $gro_in->flush or die "$objname $!";
103 $SIG{'PIPE'} = 'DFL';
105 chomp $l or die "$objname $l ?";
106 if ($l =~ m/ missing$/) {
108 } elsif (my ($type,$bytes) = $l =~ m/^\S+ (\w+) (\d+)$/) {
110 read $gro_out, $data, $bytes == $bytes or die "$objname $!";
111 return ($type, $data);
117 sub git_config ($$) {
118 my ($cfgvar, $default) = @_;
120 run_git(\$estatus, sub {
123 qw(config), $cfgvar);
125 die "$cfgvar ($estatus)" if $estatus;
128 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
135 if (!defined $git_dir) {
136 $git_dir = run_git_1line(qw(rev-parse --git-dir));
141 sub chdir_toplevel () {
143 run_git(sub { $toplevel = $_; },
144 qw(rev-parse --show-toplevel));
145 die "not in working tree?\n" unless defined $toplevel;
146 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
149 sub current_branch () {
150 open R, git_dir().'/HEAD' or die "open HEAD $!";
151 my $ref = <R>; defined $ref or die $!;
154 if ($ref !~ s#^ref: ##) {
160 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
168 DepSpec => "$2\@$3/$4/$'",
170 } elsif ($ref =~ m#^refs/heads/#) {
184 sub parse_patch_name ($) {
186 my ($eaddr, $date, $nick) = split /\//, $patch, 3;
187 defined $nick && length $nick or die "$patch ?";
188 my ($email, $domain) = $eaddr =~ m/^(.*)\@([^\@]+)$/
189 or die "$patch eaddr ?";
198 sub parse_patch_spec ($) {
201 my $spec = { }; # Email Domain DatePrefix DateNear Nick
203 my ($key,$val,$whats) = @_;
204 die "multiple $whats in patch spec\n" if exists $spec->{$key};
205 $spec->{$key} = $val;
209 if (s#([^/\@]*)\@([^/\@]*)/##) {
210 $set->('Email', $1, "email local parts") if length $1;
211 $set->('Domain', $2, "email domains") if length $1;
212 } elsif (s#([^/]*\~[^/]*)/##) {
215 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
217 close DATE or die "date parsing failed\n";
219 $set->('DateNear', $l, 'nearby dates');
220 } elsif (s#^([0-9][^/]*)/##) {
223 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
224 or die "bad date prefix \`$dspec'\n";
225 $set->('DatePrefix', $dspec, 'date prefixes');
228 } elsif (s#^\.\./##) {
235 if (defined $rel_levels) {
236 my $branch = current_branch();
237 if (!defined $branch->{Nick}) {
238 die "relative patch spec \`$orig',".
239 " but current branch not a topbloke patch\n";
241 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
243 die "relative patch spec \`$orig' has too many ../s\n";
244 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
251 sub setup_config () {
252 my (@files) = (qw(msg deps included flags pflags));
254 foreach my $iteration (qw(0 1)) {
255 foreach my $file (@files) {
256 my $cfgname = "merge.topbloke-$file";
257 my ($current, $current_estatus);
258 run_git(\$current_estatus,
259 sub { $current = $_; },
260 qw(config), "$cfgname.driver");
261 $current = "## failed $current_estatus" if $current_estatus;
262 next if $current =~ m/^topbloke-merge-driver --v$version /o;
263 die "$file $current ?" if $iteration;
264 debug("setting merge driver $file");
265 run_git(qw(config), "$cfgname.name",
266 "topbloke merge driver for $file");
267 run_git(qw(config), "$cfgname.driver",
268 "topbloke-merge-driver --v$version".
269 " $file %O %A %B %L");
271 my ($newattrs, $attrsfile);
272 foreach my $file (@files) {
273 my $path = ".topbloke/$file";
274 my $current = run_git_1line(qw(check-attr merge), $path);
275 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
276 my $want = "topbloke-$file";
277 next if $current eq $want;
278 die "$file $current ?" unless $current eq 'unspecified';
279 die "$file $current ?" if $iteration;
281 $attrsfile = git_dir()."/info/attributes";
282 $newattrs = new IO::File "$attrsfile.tmp", 'w'
283 or die "$attrsfile.tmp: $!";
284 if (!open OA, '<', "$attrsfile") {
285 die "$attrsfile $!" unless $!==&ENOENT;
288 print $newattrs $_ or die $!;
289 print "\n" or die $! unless chomp;
292 die $! unless close OA;
295 print $newattrs "$path\tmerge=$want\n" or die $!;
298 close $newattrs or die $!;
299 rename "$attrsfile.tmp", "$attrsfile" or die $!;
303 sub check_no_unwanted_metadata ($) {
304 # for checking foreign branches aren't contaminated
305 my ($gitbranch) = @_;
306 run_git_check_nooutput('foreign unexpectedly contains',
307 qw(ls-tree --name-only),
312 sub foreach_patch ($$$$) {
313 my ($spec, $deleted_ok, $want, $body) = @_;
314 # runs $body->($fullname, \%flags, \%deps, \%pflags, \%included)
316 # where $deps->{$fullname} etc. are 1 for true or nonexistent for false
317 # and if $want->[$item] is not true, the corresponding item may be undef
319 debug("foreach_patch considering $_");
323 my $patch = substr($',19); #');
325 $want->[0] ||= !$deleted_ok;
326 foreach my $file (qw(flags deps pflags included)) {
328 if ($file eq 'deps') {
329 # do this check after checking for deleted patches,
330 # so we don't parse deleted patches' names
331 # right, check the spec next
333 my $have = parse_patch_name($patch);
334 foreach my $k (qw(Email Domain Nick)) {
335 debug("foreach_patch mismatch $k"), return
336 if defined $spec->{$k} &&
337 $have->{$k} ne $spec->{$k};
339 debug("foreach_patch mismatch DatePrefix"), return
340 if defined $spec->{DatePrefix} &&
341 substr($have->{Date}, 0, length $spec->{DatePrefix})
342 ne $spec->{DatePrefix};
351 my ($got, $data) = git_get_object("$objname:.topbloke/$file");
352 die "$patch $file ?" unless defined $data;
354 $data{$_}=1 foreach split /\n/, $data;
356 if ($file eq 'flags') {
357 debug("foreach_patch Deleted"), return
358 if !$deleted_ok && $data{Deleted};
363 debug("foreach_patch YES @out"), return
366 qw(for-each-ref --format), '%(objectname) %(refname)',
367 qw(refs/topbloke-tips));
370 sub flagsfile_add_flag ($$) {
371 # works on "deps" too
372 my ($flagsfile, $flag) = @_;
373 my $wf = wf_start(".topbloke/$flagsfile");
374 open FI, '<', ".topbloke/$flagsfile" or die $!;
377 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
380 FI->error and die $!;
388 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
389 return [ $fh, $path ];
393 my ($wf, $data) = @_;
394 my ($fh, $path) = @$wf;
395 print $fh $data or die "write $path.tmp: $!\n";
400 my ($fh, $path) = @$wf;
402 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
407 my ($fh, $path) = @$wf;
408 close $fh or die "finish writing $path.tmp: $!\n";
409 rename "$path.tmp", $path or die "install new $path: $!\n";
412 sub wf_contents ($$) {
413 my ($path,$contents) = @_;
414 my $wf = wf_start($path);