11 use File::Path qw(make_path remove_tree);
16 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
21 run_git run_git_1line run_git_check_nooutput
22 run_git_test_anyoutput git_get_object
23 git_config git_dir chdir_toplevel enable_reflog
24 current_branch parse_patch_spec parse_patch_name
25 setup_config check_no_unwanted_metadata
29 wf_start wf wf_abort wf_done wf_contents
35 our $git_command = 'git';
39 print STDERR "DEBUG: $msg\n" or die $!;
43 # takes optional prefix arguments:
44 # coderef hook to call for each line read,
45 # with $_ containing chomped line; if not supplied,
47 # scalarref place to store exit status; if not supplied,
48 # nonzero exit status is fatal
49 my ($estatusr,$linecallr);
52 if (ref $ref eq 'SCALAR') {
54 } elsif (ref $ref eq 'CODE') {
57 die ref($ref)." @_ ?";
60 open GIT, "-|", $git_command, @_ or die $!;
63 chomp or die "$git_command @_ gave $_ ?";
69 die "$git_command @_ $!" if $!;
71 die "$git_command @_ ($?)" unless $estatusr;
74 $$estatusr = 0 if $estatusr;
80 run_git(sub { $l = $_; }, @_);
81 die "git @_ ?" unless defined $l;
85 sub run_git_check_nooutput {
86 my ($what) = shift @_;
87 run_git(sub { die "$what $_\n"; }, @_);
90 sub run_git_test_anyoutput {
92 run_git(sub { $any=1; }, @_);
96 sub git_get_object ($) {
98 our ($gro_pid, $gro_out, $gro_in);
100 $gro_pid = open2($gro_out, $gro_in, $git_command, qw(cat-file --batch))
103 #debug("git_get_object $objname");
104 $SIG{'PIPE'} = 'IGN';
105 print $gro_in $objname,"\n" or die $!;
106 $gro_in->flush or die "$objname $!";
107 $SIG{'PIPE'} = 'DFL';
109 chomp $l or die "$objname $l ?";
110 #debug("git_get_object $objname => $l");
111 if ($l =~ m/ missing$/) {
113 } elsif (my ($type,$bytes) = $l =~ m/^\S+ (\w+) (\d+)$/) {
116 (read $gro_out, $data, $bytes) == $bytes or die "$objname $!";
119 (read $gro_out, $nl, 1) == 1 or die "$objname $!";
120 $nl eq "\n" or die "$objname ?";
121 return ($type, $data);
127 sub git_config ($$) {
128 my ($cfgvar, $default) = @_;
130 run_git(\$estatus, sub {
133 qw(config), $cfgvar);
135 die "$cfgvar ($estatus)" if $estatus;
138 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
145 if (!defined $git_dir) {
146 $git_dir = run_git_1line(qw(rev-parse --git-dir));
151 sub chdir_toplevel () {
153 run_git(sub { $toplevel = $_; },
154 qw(rev-parse --show-toplevel));
155 die "not in working tree?\n" unless defined $toplevel;
156 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
159 sub enable_reflog ($) {
160 my ($branchref) = @_;
161 $branchref =~ m#^refs/# or die;
162 my $logsdir = git_dir().'/logs/';
163 my $dirname = $logsdir.dirname($branchref);
164 make_path($dirname) or die "$dirname $!";
165 open REFLOG, '>>', $logsdir.$branchref or die "$logsdir$branchref $!";
166 close REFLOG or die $!;
169 sub current_branch () {
170 open R, git_dir().'/HEAD' or die "open HEAD $!";
171 my $ref = <R>; defined $ref or die $!;
174 if ($ref !~ s#^ref: ##) {
180 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
181 my $fullname = "$2\@$3/$4/$'";
189 DepSpec => $fullname,
190 Fullname => $fullname,
192 } elsif ($ref =~ m#^refs/heads/#) {
206 sub parse_patch_name ($) {
208 my ($eaddr, $date, $nick) = split /\//, $patch, 3;
209 defined $nick && length $nick or die "$patch ?";
210 my ($email, $domain) = $eaddr =~ m/^(.*)\@([^\@]+)$/
211 or die "$patch eaddr ?";
220 Ref => "refs/topbloke-tips/$patch",
224 sub parse_patch_spec ($) {
227 my $spec = { }; # Email Domain DatePrefix DateNear Nick
229 my ($key,$val,$whats) = @_;
230 die "multiple $whats in patch spec\n" if exists $spec->{$key};
231 $spec->{$key} = $val;
235 if (s#([^/\@]*)\@([^/\@]*)/##) {
236 $set->('Email', $1, "email local parts") if length $1;
237 $set->('Domain', $2, "email domains") if length $1;
238 } elsif (s#([^/]*\~[^/]*)/##) {
241 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
243 close DATE or die "date parsing failed\n";
245 $set->('DateNear', $l, 'nearby dates');
246 } elsif (s#^([0-9][^/]*)/##) {
249 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
250 or die "bad date prefix \`$dspec'\n";
251 $set->('DatePrefix', $dspec, 'date prefixes');
254 } elsif (s#^\.\./##) {
261 if (defined $rel_levels) {
262 my $branch = current_branch();
263 if (!defined $branch->{Nick}) {
264 die "relative patch spec \`$orig',".
265 " but current branch not a topbloke patch\n";
267 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
269 die "relative patch spec \`$orig' has too many ../s\n";
270 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
277 sub setup_config () {
278 my (@files) = (qw(msg deps included flags pflags));
280 foreach my $iteration (qw(0 1)) {
281 foreach my $file (@files) {
282 my $cfgname = "merge.topbloke-$file";
283 my ($current, $current_estatus);
284 run_git(\$current_estatus,
285 sub { $current = $_; },
286 qw(config), "$cfgname.driver");
287 $current = "## failed $current_estatus" if $current_estatus;
288 next if $current =~ m/^topbloke-merge-driver --v$version /o;
289 die "$file $current ?" if $iteration;
290 debug("setting merge driver $file");
291 run_git(qw(config), "$cfgname.name",
292 "topbloke merge driver for $file");
293 run_git(qw(config), "$cfgname.driver",
294 "topbloke-merge-driver --v$version".
295 " $file %O %A %B %L");
297 my ($newattrs, $attrsfile);
298 foreach my $file (@files) {
299 my $path = ".topbloke/$file";
300 my $current = run_git_1line(qw(check-attr merge), $path);
301 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
302 my $want = "topbloke-$file";
303 next if $current eq $want;
304 die "$file $current ?" unless $current eq 'unspecified';
305 die "$file $current ?" if $iteration;
307 $attrsfile = git_dir()."/info/attributes";
308 $newattrs = new IO::File "$attrsfile.tmp", 'w'
309 or die "$attrsfile.tmp: $!";
310 if (!open OA, '<', "$attrsfile") {
311 die "$attrsfile $!" unless $!==&ENOENT;
314 print $newattrs $_ or die $!;
315 print "\n" or die $! unless chomp;
318 die $! unless close OA;
321 print $newattrs "$path\tmerge=$want\n" or die $!;
324 close $newattrs or die $!;
325 rename "$attrsfile.tmp", "$attrsfile" or die $!;
329 sub check_no_unwanted_metadata ($) {
330 # for checking foreign branches aren't contaminated
331 my ($gitbranch) = @_;
332 run_git_check_nooutput('foreign unexpectedly contains',
333 qw(ls-tree --name-only),
338 sub patch_matches_spec ($$) {
339 my ($parsedname, $spec) = @_;
340 foreach my $k (qw(Email Domain Nick)) {
341 debug("patch_matches_spec mismatch $k"), return 0
342 if defined $spec->{$k} &&
343 $parsedname->{$k} ne $spec->{$k};
345 debug("patch_matches_spec mismatch DatePrefix"), return 0
346 if defined $spec->{DatePrefix} &&
347 substr($parsedname->{Date}, 0, length $spec->{DatePrefix})
348 ne $spec->{DatePrefix};
349 debug("patch_matches_spec match"), return 1;
352 sub foreach_patch ($$$$) {
353 my ($spec, $deleted_ok, $want, $body) = @_;
354 # runs $body->($patch, $parsedname, \%flags, \%deps, \%pflags, \%included)
356 # where $deps->{$fullname} etc. are 1 for true or nonexistent for false
357 # and if $want->[$item] is not true, the corresponding item may be undef
358 # and $parsedname is only valid if $spec is not undef
359 # (say $spec { } if you want the name parsed but no restrictions)
361 debug("foreach_patch considering $_");
365 my $patch = substr($',19); #');
366 $want->[0] ||= !$deleted_ok;
367 foreach my $file (qw(flags deps pflags included)) {
369 if ($file eq 'deps') {
370 # do this check after checking for deleted patches,
371 # so we don't parse deleted patches' names
372 # right, check the spec next
374 my $have = parse_patch_name($patch);
375 debug("foreach_patch mismatch"), return
376 unless patch_matches_spec($have, $spec);
388 my ($got, $data) = git_get_object("$objname:.topbloke/$file");
389 die "$patch $file ?" unless defined $data;
391 $data{$_}=1 foreach split /\n/, $data;
393 if ($file eq 'flags') {
394 debug("foreach_patch Deleted"), return
395 if !$deleted_ok && $data{Deleted};
400 debug("foreach_patch YES @out"), return
401 $body->($patch, @out);
403 qw(for-each-ref --format), '%(objectname) %(refname)',
404 qw(refs/topbloke-tips));
407 sub flagsfile_add_flag ($$) {
408 # works on "deps" too
409 my ($flagsfile, $flag) = @_;
410 my $wf = wf_start(".topbloke/$flagsfile");
411 open FI, '<', ".topbloke/$flagsfile" or die $!;
414 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
417 FI->error and die $!;
425 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
426 return [ $fh, $path ];
430 my ($wf, $data) = @_;
431 my ($fh, $path) = @$wf;
432 print $fh $data or die "write $path.tmp: $!\n";
437 my ($fh, $path) = @$wf;
439 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
444 my ($fh, $path) = @$wf;
445 close $fh or die "finish writing $path.tmp: $!\n";
446 rename "$path.tmp", $path or die "install new $path: $!\n";
449 sub wf_contents ($$) {
450 my ($path,$contents) = @_;
451 my $wf = wf_start($path);
457 STDOUT->error and die $!;
458 close STDOUT or die $!;