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);
31 print STDERR "DEBUG: $msg\n" or die $!;
35 # takes optional prefix arguments:
36 # coderef hook to call for each line read,
37 # with $_ containing chomped line; if not supplied,
39 # scalarref place to store exit status; if not supplied,
40 # nonzero exit status is fatal
41 my ($estatusr,$linecallr);
44 if (ref $ref eq 'SCALAR') {
46 } elsif (ref $ref eq 'CODE') {
49 die ref($ref)." @_ ?";
52 open GIT, "-|", 'git', @_ or die $!;
61 die "git @_ $!" if $!;
63 die "git @_ ($?)" unless $estatusr;
66 $$estatusr = 0 if $estatusr;
72 run_git(sub { $l = $_; }, @_);
73 die "git @_ ?" unless defined $l;
77 sub run_git_check_nooutput {
78 my ($what) = shift @_;
79 run_git(sub { die "$what $_\n"; }, @_);
82 sub run_git_test_anyoutput {
84 run_git(sub { $any=1; }, @_);
89 my ($cfgvar, $default) = @_;
91 run_git(\$estatus, sub {
96 die "$cfgvar ($estatus)" if $estatus;
99 die "$cfgvar ($estatus)" unless $estatus==0 || $estatus==256;
106 if (!defined $git_dir) {
107 $git_dir = run_git_1line(qw(rev-parse --git-dir));
112 sub chdir_toplevel () {
114 run_git(sub { $toplevel = $_; },
115 qw(rev-parse --show-toplevel));
116 die "not in working tree?\n" unless defined $toplevel;
117 chdir $toplevel or die "chdir toplevel $toplevel: $!\n";
120 sub current_branch () {
121 open R, git_dir().'/HEAD' or die "open HEAD $!";
122 my $ref = <R>; defined $ref or die $!;
125 if ($ref !~ s#^ref: ##) {
131 if ($ref =~ m#^refs/topbloke-(tip|base)s/([^/\@]*)\@([^/\@]*)/([^/]*)/#) {
139 DepSpec => "$2\@$3/$4/$'",
141 } elsif ($ref =~ m#^refs/heads/#) {
155 sub parse_patch_spec ($) {
158 my $spec = { }; # Email Domain DatePrefix DateNear Nick
160 my ($key,$val,$whats) = @_;
161 die "multiple $whats in patch spec\n" if exists $spec->{$key};
162 $spec->{$key} = $val;
166 if (s#([^/\@]*)\@([^/\@]*)/##) {
167 $set->('Email', $1, "email local parts") if length $1;
168 $set->('Domain', $2, "email domains") if length $1;
169 } elsif (s#([^/]*\~[^/]*)/##) {
172 open DATE, "-|", 'date','+%s','-d',$dspec or die $!;
174 close DATE or die "date parsing failed\n";
176 $set->('DateNear', $l, 'nearby dates');
177 } elsif (s#^([0-9][^/]*)/##) {
180 m/^\d{4}(?:-\d\d(?:-\d\d(?:T(?:\d\d(?:\d\d(?:\d\d(?:Z)?)?)?)?)?)?)?$/
181 or die "bad date prefix \`$dspec'\n";
182 $set->('DatePrefix', $dspec, 'date prefixes');
185 } elsif (s#^\.\./##) {
192 if (defined $rel_levels) {
193 my $branch = current_branch();
194 if (!defined $branch->{Nick}) {
195 die "relative patch spec \`$orig',".
196 " but current branch not a topbloke patch\n";
198 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
200 die "relative patch spec \`$orig' has too many ../s\n";
201 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
208 sub setup_config () {
209 my (@files) = (qw(msg deps included flags pflags));
211 foreach my $iteration (qw(0 1)) {
212 foreach my $file (@files) {
213 my $cfgname = "merge.topbloke-$file";
214 my ($current, $current_estatus);
215 run_git(\$current_estatus,
216 sub { $current = $_; },
217 qw(config), "$cfgname.driver");
218 $current = "## failed $current_estatus" if $current_estatus;
219 next if $current =~ m/^topbloke-merge-driver --v$version /o;
220 die "$file $current ?" if $iteration;
221 debug("setting merge driver $file");
222 run_git(qw(config), "$cfgname.name",
223 "topbloke merge driver for $file");
224 run_git(qw(config), "$cfgname.driver",
225 "topbloke-merge-driver --v$version".
226 " $file %O %A %B %L");
228 my ($newattrs, $attrsfile);
229 foreach my $file (@files) {
230 my $path = ".topbloke/$file";
231 my $current = run_git_1line(qw(check-attr merge), $path);
232 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
233 my $want = "topbloke-$file";
234 next if $current eq $want;
235 die "$file $current ?" unless $current eq 'unspecified';
236 die "$file $current ?" if $iteration;
238 $attrsfile = git_dir()."/info/attributes";
239 $newattrs = new IO::File "$attrsfile.tmp", 'w'
240 or die "$attrsfile.tmp: $!";
241 if (!open OA, '<', "$attrsfile") {
242 die "$attrsfile $!" unless $!==&ENOENT;
245 print $newattrs $_ or die $!;
246 print "\n" or die $! unless chomp;
249 die $! unless close OA;
252 print $newattrs "$path\tmerge=$want\n" or die $!;
255 close $newattrs or die $!;
256 rename "$attrsfile.tmp", "$attrsfile" or die $!;
260 sub check_no_unwanted_metadata ($) {
261 # for checking foreign branches aren't contaminated
262 my ($gitbranch) = @_;
263 run_git_check_nooutput('foreign unexpectedly contains',
264 qw(ls-tree --name-only),
269 sub flagsfile_add_flag ($$) {
270 # works on "deps" too
271 my ($flagsfile, $flag) = @_;
272 my $wf = wf_start(".topbloke/$flagsfile");
273 open FI, '<', ".topbloke/$flagsfile" or die $!;
276 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
279 FI->error and die $!;
287 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
288 return [ $fh, $path ];
292 my ($wf, $data) = @_;
293 my ($fh, $path) = @$wf;
294 print $fh $data or die "write $path.tmp: $!\n";
299 my ($fh, $path) = @$wf;
301 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
306 my ($fh, $path) = @$wf;
307 close $fh or die "finish writing $path.tmp: $!\n";
308 rename "$path.tmp", $path or die "install new $path: $!\n";
311 sub wf_contents ($$) {
312 my ($path,$contents) = @_;
313 my $wf = wf_start($path);