14 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
19 run_git run_git_1line run_git_check_nooutput
20 run_git_test_anyoutput git_get_object
21 git_config git_dir chdir_toplevel
22 current_branch parse_patch_spec parse_patch_name
23 setup_config check_no_unwanted_metadata
27 wf_start wf wf_abort wf_done wf_contents
33 our $git_command = 'git';
37 print STDERR "DEBUG: $msg\n" or die $!;
41 # takes optional prefix arguments:
42 # coderef hook to call for each line read,
43 # with $_ containing chomped line; if not supplied,
45 # scalarref place to store exit status; if not supplied,
46 # nonzero exit status is fatal
47 my ($estatusr,$linecallr);
50 if (ref $ref eq 'SCALAR') {
52 } elsif (ref $ref eq 'CODE') {
55 die ref($ref)." @_ ?";
58 open GIT, "-|", $git_command, @_ or die $!;
61 chomp or die "$git_command @_ gave $_ ?";
67 die "$git_command @_ $!" if $!;
69 die "$git_command @_ ($?)" unless $estatusr;
72 $$estatusr = 0 if $estatusr;
78 run_git(sub { $l = $_; }, @_);
79 die "git @_ ?" unless defined $l;
83 sub run_git_check_nooutput {
84 my ($what) = shift @_;
85 run_git(sub { die "$what $_\n"; }, @_);
88 sub run_git_test_anyoutput {
90 run_git(sub { $any=1; }, @_);
94 sub git_get_object ($) {
96 our ($gro_pid, $gro_out, $gro_in);
98 $gro_pid = IPC::Open2::open2($gro_out, $gro_in,
99 $git_command, qw(cat-file --batch))
102 #debug("git_get_object $objname");
103 $SIG{'PIPE'} = 'IGN';
104 print $gro_in $objname,"\n" or die $!;
105 $gro_in->flush or die "$objname $!";
106 $SIG{'PIPE'} = 'DFL';
108 chomp $l or die "$objname $l ?";
109 #debug("git_get_object $objname => $l");
110 if ($l =~ m/ missing$/) {
112 } elsif (my ($type,$bytes) = $l =~ m/^\S+ (\w+) (\d+)$/) {
115 (read $gro_out, $data, $bytes) == $bytes or die "$objname $!";
118 (read $gro_out, $nl, 1) == 1 or die "$objname $!";
119 $nl eq "\n" or die "$objname ?";
120 return ($type, $data);
126 sub git_config ($$) {
127 my ($cfgvar, $default) = @_;
129 run_git(\$estatus, sub {
132 qw(config), $cfgvar);
134 die "$cfgvar ($estatus)" if $estatus;
137 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
144 if (!defined $git_dir) {
145 $git_dir = run_git_1line(qw(rev-parse --git-dir));
150 sub chdir_toplevel () {
152 run_git(sub { $toplevel = $_; },
153 qw(rev-parse --show-toplevel));
154 die "not in working tree?\n" unless defined $toplevel;
155 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
158 sub current_branch () {
159 open R, git_dir().'/HEAD' or die "open HEAD $!";
160 my $ref = <R>; defined $ref or die $!;
163 if ($ref !~ s#^ref: ##) {
169 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
170 my $fullname = "$2\@$3/$4/$'";
178 DepSpec => $fullname,
179 Fullname => $fullname,
181 } elsif ($ref =~ m#^refs/heads/#) {
195 sub parse_patch_name ($) {
197 my ($eaddr, $date, $nick) = split /\//, $patch, 3;
198 defined $nick && length $nick or die "$patch ?";
199 my ($email, $domain) = $eaddr =~ m/^(.*)\@([^\@]+)$/
200 or die "$patch eaddr ?";
209 Ref => "refs/topbloke-tips/$patch",
213 sub parse_patch_spec ($) {
216 my $spec = { }; # Email Domain DatePrefix DateNear Nick
218 my ($key,$val,$whats) = @_;
219 die "multiple $whats in patch spec\n" if exists $spec->{$key};
220 $spec->{$key} = $val;
224 if (s#([^/\@]*)\@([^/\@]*)/##) {
225 $set->('Email', $1, "email local parts") if length $1;
226 $set->('Domain', $2, "email domains") if length $1;
227 } elsif (s#([^/]*\~[^/]*)/##) {
230 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
232 close DATE or die "date parsing failed\n";
234 $set->('DateNear', $l, 'nearby dates');
235 } elsif (s#^([0-9][^/]*)/##) {
238 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
239 or die "bad date prefix \`$dspec'\n";
240 $set->('DatePrefix', $dspec, 'date prefixes');
243 } elsif (s#^\.\./##) {
250 if (defined $rel_levels) {
251 my $branch = current_branch();
252 if (!defined $branch->{Nick}) {
253 die "relative patch spec \`$orig',".
254 " but current branch not a topbloke patch\n";
256 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
258 die "relative patch spec \`$orig' has too many ../s\n";
259 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
266 sub setup_config () {
267 my (@files) = (qw(msg deps included flags pflags));
269 foreach my $iteration (qw(0 1)) {
270 foreach my $file (@files) {
271 my $cfgname = "merge.topbloke-$file";
272 my ($current, $current_estatus);
273 run_git(\$current_estatus,
274 sub { $current = $_; },
275 qw(config), "$cfgname.driver");
276 $current = "## failed $current_estatus" if $current_estatus;
277 next if $current =~ m/^topbloke-merge-driver --v$version /o;
278 die "$file $current ?" if $iteration;
279 debug("setting merge driver $file");
280 run_git(qw(config), "$cfgname.name",
281 "topbloke merge driver for $file");
282 run_git(qw(config), "$cfgname.driver",
283 "topbloke-merge-driver --v$version".
284 " $file %O %A %B %L");
286 my ($newattrs, $attrsfile);
287 foreach my $file (@files) {
288 my $path = ".topbloke/$file";
289 my $current = run_git_1line(qw(check-attr merge), $path);
290 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
291 my $want = "topbloke-$file";
292 next if $current eq $want;
293 die "$file $current ?" unless $current eq 'unspecified';
294 die "$file $current ?" if $iteration;
296 $attrsfile = git_dir()."/info/attributes";
297 $newattrs = new IO::File "$attrsfile.tmp", 'w'
298 or die "$attrsfile.tmp: $!";
299 if (!open OA, '<', "$attrsfile") {
300 die "$attrsfile $!" unless $!==&ENOENT;
303 print $newattrs $_ or die $!;
304 print "\n" or die $! unless chomp;
307 die $! unless close OA;
310 print $newattrs "$path\tmerge=$want\n" or die $!;
313 close $newattrs or die $!;
314 rename "$attrsfile.tmp", "$attrsfile" or die $!;
318 sub check_no_unwanted_metadata ($) {
319 # for checking foreign branches aren't contaminated
320 my ($gitbranch) = @_;
321 run_git_check_nooutput('foreign unexpectedly contains',
322 qw(ls-tree --name-only),
327 sub patch_matches_spec ($$) {
328 my ($parsedname, $spec) = @_;
329 foreach my $k (qw(Email Domain Nick)) {
330 debug("patch_matches_spec mismatch $k"), return 0
331 if defined $spec->{$k} &&
332 $parsedname->{$k} ne $spec->{$k};
334 debug("patch_matches_spec mismatch DatePrefix"), return 0
335 if defined $spec->{DatePrefix} &&
336 substr($parsedname->{Date}, 0, length $spec->{DatePrefix})
337 ne $spec->{DatePrefix};
338 debug("patch_matches_spec match"), return 1;
341 sub foreach_patch ($$$$) {
342 my ($spec, $deleted_ok, $want, $body) = @_;
343 # runs $body->($patch, $parsedname, \%flags, \%deps, \%pflags, \%included)
345 # where $deps->{$fullname} etc. are 1 for true or nonexistent for false
346 # and if $want->[$item] is not true, the corresponding item may be undef
347 # and $parsedname is only valid if $spec is not undef
348 # (say $spec { } if you want the name parsed but no restrictions)
350 debug("foreach_patch considering $_");
354 my $patch = substr($',19); #');
355 $want->[0] ||= !$deleted_ok;
356 foreach my $file (qw(flags deps pflags included)) {
358 if ($file eq 'deps') {
359 # do this check after checking for deleted patches,
360 # so we don't parse deleted patches' names
361 # right, check the spec next
363 my $have = parse_patch_name($patch);
364 debug("foreach_patch mismatch"), return
365 unless patch_matches_spec($have, $spec);
377 my ($got, $data) = git_get_object("$objname:.topbloke/$file");
378 die "$patch $file ?" unless defined $data;
380 $data{$_}=1 foreach split /\n/, $data;
382 if ($file eq 'flags') {
383 debug("foreach_patch Deleted"), return
384 if !$deleted_ok && $data{Deleted};
389 debug("foreach_patch YES @out"), return
390 $body->($patch, @out);
392 qw(for-each-ref --format), '%(objectname) %(refname)',
393 qw(refs/topbloke-tips));
396 sub flagsfile_add_flag ($$) {
397 # works on "deps" too
398 my ($flagsfile, $flag) = @_;
399 my $wf = wf_start(".topbloke/$flagsfile");
400 open FI, '<', ".topbloke/$flagsfile" or die $!;
403 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
406 FI->error and die $!;
414 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
415 return [ $fh, $path ];
419 my ($wf, $data) = @_;
420 my ($fh, $path) = @$wf;
421 print $fh $data or die "write $path.tmp: $!\n";
426 my ($fh, $path) = @$wf;
428 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
433 my ($fh, $path) = @$wf;
434 close $fh or die "finish writing $path.tmp: $!\n";
435 rename "$path.tmp", $path or die "install new $path: $!\n";
438 sub wf_contents ($$) {
439 my ($path,$contents) = @_;
440 my $wf = wf_start($path);
446 STDOUT->error and die $!;
447 close STDOUT or die $!;