11 use File::Path qw(make_path remove_tree);
16 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
20 @EXPORT = qw(debug $tiprefs $baserefs
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
28 propsfile_add_prop depsfile_add_dep
29 wf_start wf wf_abort wf_done wf_contents
35 our $git_command = 'git';
39 print STDERR "DEBUG: $msg\n" or die $!;
42 #----- general interaction with git -----
45 # takes optional prefix arguments:
46 # coderef hook to call for each line read,
47 # with $_ containing chomped line; if not supplied,
49 # scalarref place to store exit status; if not supplied,
50 # nonzero exit status is fatal
51 my ($estatusr,$linecallr);
54 if (ref $ref eq 'SCALAR') {
56 } elsif (ref $ref eq 'CODE') {
59 die ref($ref)." @_ ?";
62 open GIT, "-|", $git_command, @_ or die $!;
65 chomp or die "$git_command @_ gave $_ ?";
71 die "$git_command @_ $!" if $!;
73 die "$git_command @_ ($?)" unless $estatusr;
76 $$estatusr = 0 if $estatusr;
82 run_git(sub { $l = $_; }, @_);
83 die "git @_ ?" unless defined $l;
87 sub run_git_check_nooutput {
88 my ($what) = shift @_;
89 run_git(sub { die "$what $_\n"; }, @_);
92 sub run_git_test_anyoutput {
94 run_git(sub { $any=1; }, @_);
98 sub git_get_object ($) {
100 our ($gro_pid, $gro_out, $gro_in);
102 $gro_pid = open2($gro_out, $gro_in, $git_command, qw(cat-file --batch))
105 #debug("git_get_object $objname");
106 $SIG{'PIPE'} = 'IGN';
107 print $gro_in $objname,"\n" or die $!;
108 $gro_in->flush or die "$objname $!";
109 $SIG{'PIPE'} = 'DFL';
111 chomp $l or die "$objname $l ?";
112 #debug("git_get_object $objname => $l");
113 if ($l =~ m/ missing$/) {
115 } elsif (my ($type,$bytes) = $l =~ m/^\S+ (\w+) (\d+)$/) {
118 (read $gro_out, $data, $bytes) == $bytes or die "$objname $!";
121 (read $gro_out, $nl, 1) == 1 or die "$objname $!";
122 $nl eq "\n" or die "$objname ?";
123 return ($type, $data);
129 sub git_config ($$) {
130 my ($cfgvar, $default) = @_;
132 run_git(\$estatus, sub {
135 qw(config), $cfgvar);
137 die "$cfgvar ($estatus)" if $estatus;
140 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
147 if (!defined $git_dir) {
148 $git_dir = run_git_1line(qw(rev-parse --git-dir));
153 #----- specific interactions with git -----
155 sub chdir_toplevel () {
157 run_git(sub { $toplevel = $_; },
158 qw(rev-parse --show-toplevel));
159 die "not in working tree?\n" unless defined $toplevel;
160 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
163 sub enable_reflog ($) {
164 my ($branchref) = @_;
165 $branchref =~ m#^refs/# or die;
166 my $logsdir = git_dir().'/logs/';
167 my $dirname = $logsdir.dirname($branchref);
168 make_path($dirname) or die "$dirname $!";
169 open REFLOG, '>>', $logsdir.$branchref or die "$logsdir$branchref $!";
170 close REFLOG or die $!;
173 sub check_no_unwanted_metadata ($) {
174 # for checking foreign branches aren't contaminated
175 my ($gitbranch) = @_;
176 run_git_check_nooutput('foreign unexpectedly contains',
177 qw(ls-tree --name-only),
182 sub check_clean_tree ($) {
183 run_git_check_nooutput("operation requires working tree to be clean",
184 qw(diff --name-only HEAD --));
185 run_git_check_nooutput("operation cannot proceed with staged changes",
186 qw(diff --cached --name-only HEAD --));
189 #----- configuring a tree -----
191 sub setup_config () {
192 my (@files) = (qw(msg deps included props pprops));
194 foreach my $iteration (qw(0 1)) {
195 foreach my $file (@files) {
196 my $cfgname = "merge.topbloke-$file";
197 my ($current, $current_estatus);
198 run_git(\$current_estatus,
199 sub { $current = $_; },
200 qw(config), "$cfgname.driver");
201 $current = "## failed $current_estatus" if $current_estatus;
202 next if $current =~ m/^topbloke-merge-driver --v$version /o;
203 die "$file $current ?" if $iteration;
204 debug("setting merge driver $file");
205 run_git(qw(config), "$cfgname.name",
206 "topbloke merge driver for $file");
207 run_git(qw(config), "$cfgname.driver",
208 "topbloke-merge-driver --v$version".
209 " $file %O %A %B %L");
211 my ($newattrs, $attrsfile);
212 foreach my $file (@files) {
213 my $path = ".topbloke/$file";
214 my $current = run_git_1line(qw(check-attr merge), $path);
215 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
216 my $want = "topbloke-$file";
217 next if $current eq $want;
218 die "$file $current ?" unless $current eq 'unspecified';
219 die "$file $current ?" if $iteration;
221 $attrsfile = git_dir()."/info/attributes";
222 $newattrs = new IO::File "$attrsfile.tmp", 'w'
223 or die "$attrsfile.tmp: $!";
224 if (!open OA, '<', "$attrsfile") {
225 die "$attrsfile $!" unless $!==&ENOENT;
228 print $newattrs $_ or die $!;
229 print "\n" or die $! unless chomp;
232 die $! unless close OA;
235 print $newattrs "$path\tmerge=$want\n" or die $!;
238 close $newattrs or die $!;
239 rename "$attrsfile.tmp", "$attrsfile" or die $!;
243 #----- branch and patch specs and parsed patch names -----
245 our $tiprefs = 'refs/topbloke-tips';
246 our $baserefs = 'refs/topbloke-bases';
248 sub current_branch () {
249 open R, git_dir().'/HEAD' or die "open HEAD $!";
250 my $ref = <R>; defined $ref or die $!;
253 if ($ref !~ s#^ref: ##) {
259 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
260 my $fullname = "$2\@$3/$4/$'";
268 DepSpec => $fullname,
269 Fullname => $fullname,
271 } elsif ($ref =~ m#^refs/heads/#) {
275 DepSpec => ".f $ref",
285 sub parse_patch_name ($) {
287 my ($eaddr, $date, $nick) = split /\//, $patch, 3;
288 defined $nick && length $nick or die "$patch ?";
289 my ($email, $domain) = $eaddr =~ m/^(.*)\@([^\@]+)$/
290 or die "$patch eaddr ?";
299 Ref => "refs/topbloke-tips/$patch",
303 sub parse_patch_spec ($) {
306 my $spec = { }; # Email Domain DatePrefix DateNear Nick
308 my ($key,$val,$whats) = @_;
309 die "multiple $whats in patch spec\n" if exists $spec->{$key};
310 $spec->{$key} = $val;
314 if (s#([^/\@]*)\@([^/\@]*)/##) {
315 $set->('Email', $1, "email local parts") if length $1;
316 $set->('Domain', $2, "email domains") if length $1;
317 } elsif (s#([^/]*\~[^/]*)/##) {
320 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
322 close DATE or die "date parsing failed\n";
324 $set->('DateNear', $l, 'nearby dates');
325 } elsif (s#^([0-9][^/]*)/##) {
328 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
329 or die "bad date prefix \`$dspec'\n";
330 $set->('DatePrefix', $dspec, 'date prefixes');
333 } elsif (s#^\.\./##) {
340 if (defined $rel_levels) {
341 my $branch = current_branch();
342 if (!defined $branch->{Nick}) {
343 die "relative patch spec \`$orig',".
344 " but current branch not a topbloke patch\n";
346 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
348 die "relative patch spec \`$orig' has too many ../s\n";
349 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
356 sub patch_matches_spec ($$) {
357 my ($parsedname, $spec) = @_;
358 foreach my $k (qw(Email Domain Nick)) {
359 debug("patch_matches_spec mismatch $k"), return 0
360 if defined $spec->{$k} &&
361 $parsedname->{$k} ne $spec->{$k};
363 debug("patch_matches_spec mismatch DatePrefix"), return 0
364 if defined $spec->{DatePrefix} &&
365 substr($parsedname->{Date}, 0, length $spec->{DatePrefix})
366 ne $spec->{DatePrefix};
367 debug("patch_matches_spec match"), return 1;
370 #----- reading topbloke metadata -----
372 sub foreach_patch ($$$$) {
373 my ($spec, $deleted_ok, $want, $body) = @_;
374 # runs $body->($patch, $parsedname, \%props, \%deps, \%pprops, \%included)
376 # where $deps->{$fullname} etc. are 1 for true or nonexistent for false
377 # and if $want->[$item] is not true, the corresponding item may be undef
378 # and $parsedname is only valid if $spec is not undef
379 # (say $spec { } if you want the name parsed but no restrictions)
381 $want[0] ||= !$deleted_ok;
383 debug("foreach_patch considering $_");
387 my $patch = substr($',19); #');
389 foreach my $file (qw(props deps pprops included)) {
391 if ($file eq 'deps') {
392 # do this check after checking for deleted patches,
393 # so we don't parse deleted patches' names
394 # right, check the spec next
396 my $have = parse_patch_name($patch);
397 debug("foreach_patch mismatch"), return
398 unless patch_matches_spec($have, $spec);
405 if (!$want[$wantix++]) {
410 my ($got, $data) = git_get_object("$objname:.topbloke/$file");
411 die "$patch $file ?" unless defined $data;
413 if ($file !~ m/props/) {
414 $data{$_}=1 foreach split /\n/, $data;
416 foreach (split /\n/, $data) {
422 if ($file eq 'props') {
423 debug("foreach_patch Deleted"), return
424 if !$deleted_ok && $data{Deleted};
429 debug("foreach_patch YES ".(join '', map { 0+defined } @out)), return
430 $body->($patch, @out);
432 qw(for-each-ref --format), '%(objectname) %(refname)',
433 qw(refs/topbloke-tips));
436 #----- updating topbloke metadata -----
438 sub propsfile_set_prop ($$$) {
439 # set $value to undef to delete; returns old value
440 my ($propsfile, $prop, $value) = @_;
441 my $wf = wf_start(".topbloke/$propsfile");
443 open FI, '<', ".topbloke/$propsfile" or die $!;
448 die "prop $prop repeated in $propsfile ?!" if defined $oldvalue;
454 FI->error and die $!;
456 wf($wf, "$prop $value\n") if defined $value;
461 sub depssfile_add_dep ($$) {
462 my ($depsfile, $depspec) = @_;
463 my $wf = wf_start(".topbloke/$depsfile");
464 open FI, '<', ".topbloke/$depsfile" or die $!;
467 die "dep $depspec already set in $depsfile ?!" if $_ eq $depspec;
470 FI->error and die $!;
472 wf($wf, "$depspec\n");
476 #----- general utilities -----
480 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
481 return [ $fh, $path ];
485 my ($wf, $data) = @_;
486 my ($fh, $path) = @$wf;
487 print $fh $data or die "write $path.tmp: $!\n";
492 my ($fh, $path) = @$wf;
494 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
499 my ($fh, $path) = @$wf;
500 close $fh or die "finish writing $path.tmp: $!\n";
501 rename "$path.tmp", $path or die "install new $path: $!\n";
504 sub wf_contents ($$) {
505 my ($path,$contents) = @_;
506 my $wf = wf_start($path);
512 STDOUT->error and die $!;
513 close STDOUT or die $!;