13 our ($VERSION, @ISA, @EXPORT, @EXPORT_OK, %EXPORT_TAGS);
18 run_git run_git_1line run_git_check_nooutput
19 run_git_test_anyoutput
20 git_config git_dir chdir_toplevel
21 current_branch parse_patch_spec
22 setup_config check_no_unwanted_metadata
24 wf_start wf wf_abort wf_done wf_contents);
29 our $git_command = 'git';
33 print STDERR "DEBUG: $msg\n" or die $!;
37 # takes optional prefix arguments:
38 # coderef hook to call for each line read,
39 # with $_ containing chomped line; if not supplied,
41 # scalarref place to store exit status; if not supplied,
42 # nonzero exit status is fatal
43 my ($estatusr,$linecallr);
46 if (ref $ref eq 'SCALAR') {
48 } elsif (ref $ref eq 'CODE') {
51 die ref($ref)." @_ ?";
54 open GIT, "-|", $git_command, @_ or die $!;
57 chomp or die "$git_command @_ gave $_ ?";
63 die "$git_command @_ $!" if $!;
65 die "$git_command @_ ($?)" unless $estatusr;
68 $$estatusr = 0 if $estatusr;
74 run_git(sub { $l = $_; }, @_);
75 die "git @_ ?" unless defined $l;
79 sub run_git_check_nooutput {
80 my ($what) = shift @_;
81 run_git(sub { die "$what $_\n"; }, @_);
84 sub run_git_test_anyoutput {
86 run_git(sub { $any=1; }, @_);
91 my ($cfgvar, $default) = @_;
93 run_git(\$estatus, sub {
98 die "$cfgvar ($estatus)" if $estatus;
101 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
108 if (!defined $git_dir) {
109 $git_dir = run_git_1line(qw(rev-parse --git-dir));
114 sub chdir_toplevel () {
116 run_git(sub { $toplevel = $_; },
117 qw(rev-parse --show-toplevel));
118 die "not in working tree?\n" unless defined $toplevel;
119 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
122 sub current_branch () {
123 open R, git_dir().'/HEAD' or die "open HEAD $!";
124 my $ref = <R>; defined $ref or die $!;
127 if ($ref !~ s#^ref: ##) {
133 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
141 DepSpec => "$2\@$3/$4/$'",
143 } elsif ($ref =~ m#^refs/heads/#) {
157 sub parse_patch_spec ($) {
160 my $spec = { }; # Email Domain DatePrefix DateNear Nick
162 my ($key,$val,$whats) = @_;
163 die "multiple $whats in patch spec\n" if exists $spec->{$key};
164 $spec->{$key} = $val;
168 if (s#([^/\@]*)\@([^/\@]*)/##) {
169 $set->('Email', $1, "email local parts") if length $1;
170 $set->('Domain', $2, "email domains") if length $1;
171 } elsif (s#([^/]*\~[^/]*)/##) {
174 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
176 close DATE or die "date parsing failed\n";
178 $set->('DateNear', $l, 'nearby dates');
179 } elsif (s#^([0-9][^/]*)/##) {
182 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
183 or die "bad date prefix \`$dspec'\n";
184 $set->('DatePrefix', $dspec, 'date prefixes');
187 } elsif (s#^\.\./##) {
194 if (defined $rel_levels) {
195 my $branch = current_branch();
196 if (!defined $branch->{Nick}) {
197 die "relative patch spec \`$orig',".
198 " but current branch not a topbloke patch\n";
200 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
202 die "relative patch spec \`$orig' has too many ../s\n";
203 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
210 sub setup_config () {
211 my (@files) = (qw(msg deps included flags pflags));
213 foreach my $iteration (qw(0 1)) {
214 foreach my $file (@files) {
215 my $cfgname = "merge.topbloke-$file";
216 my ($current, $current_estatus);
217 run_git(\$current_estatus,
218 sub { $current = $_; },
219 qw(config), "$cfgname.driver");
220 $current = "## failed $current_estatus" if $current_estatus;
221 next if $current =~ m/^topbloke-merge-driver --v$version /o;
222 die "$file $current ?" if $iteration;
223 debug("setting merge driver $file");
224 run_git(qw(config), "$cfgname.name",
225 "topbloke merge driver for $file");
226 run_git(qw(config), "$cfgname.driver",
227 "topbloke-merge-driver --v$version".
228 " $file %O %A %B %L");
230 my ($newattrs, $attrsfile);
231 foreach my $file (@files) {
232 my $path = ".topbloke/$file";
233 my $current = run_git_1line(qw(check-attr merge), $path);
234 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
235 my $want = "topbloke-$file";
236 next if $current eq $want;
237 die "$file $current ?" unless $current eq 'unspecified';
238 die "$file $current ?" if $iteration;
240 $attrsfile = git_dir()."/info/attributes";
241 $newattrs = new IO::File "$attrsfile.tmp", 'w'
242 or die "$attrsfile.tmp: $!";
243 if (!open OA, '<', "$attrsfile") {
244 die "$attrsfile $!" unless $!==&ENOENT;
247 print $newattrs $_ or die $!;
248 print "\n" or die $! unless chomp;
251 die $! unless close OA;
254 print $newattrs "$path\tmerge=$want\n" or die $!;
257 close $newattrs or die $!;
258 rename "$attrsfile.tmp", "$attrsfile" or die $!;
262 sub check_no_unwanted_metadata ($) {
263 # for checking foreign branches aren't contaminated
264 my ($gitbranch) = @_;
265 run_git_check_nooutput('foreign unexpectedly contains',
266 qw(ls-tree --name-only),
271 sub flagsfile_add_flag ($$) {
272 # works on "deps" too
273 my ($flagsfile, $flag) = @_;
274 my $wf = wf_start(".topbloke/$flagsfile");
275 open FI, '<', ".topbloke/$flagsfile" or die $!;
278 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
281 FI->error and die $!;
289 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
290 return [ $fh, $path ];
294 my ($wf, $data) = @_;
295 my ($fh, $path) = @$wf;
296 print $fh $data or die "write $path.tmp: $!\n";
301 my ($fh, $path) = @$wf;
303 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
308 my ($fh, $path) = @$wf;
309 close $fh or die "finish writing $path.tmp: $!\n";
310 rename "$path.tmp", $path or die "install new $path: $!\n";
313 sub wf_contents ($$) {
314 my ($path,$contents) = @_;
315 my $wf = wf_start($path);