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_branch_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_branch_spec ($) {
158 my $spec = { }; # Email Domain DatePrefix DateNear Nick
160 my ($key,$val,$whats) = @_;
161 die "multiple $whats in branch 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 branch spec \`$orig',".
196 " but current branch not a topbloke branch\n";
198 my ($ceaddr,$cdate,@l) = split /\//, $branch->{Nick};
200 die "relative branch spec \`$orig' has too many ../s\n";
201 $_ = (join '/', @l[0..$#l-$rel_levels]).'/'.$_;
207 sub setup_config () {
208 my (@files) = (qw(msg deps included flags pflags));
210 foreach my $iteration (qw(0 1)) {
211 foreach my $file (@files) {
212 my $cfgname = "merge.topbloke-$file";
213 my ($current, $current_estatus);
214 run_git(\$current_estatus,
215 sub { $current = $_; },
216 qw(config), "$cfgname.driver");
217 $current = "## failed $current_estatus" if $current_estatus;
218 next if $current =~ m/^topbloke-merge-driver --v$version /o;
219 die "$file $current ?" if $iteration;
220 debug("setting merge driver $file");
221 run_git(qw(config), "$cfgname.name",
222 "topbloke merge driver for $file");
223 run_git(qw(config), "$cfgname.driver",
224 "topbloke-merge-driver --v$version".
225 " $file %O %A %B %L");
227 my ($newattrs, $attrsfile);
228 foreach my $file (@files) {
229 my $path = ".topbloke/$file";
230 my $current = run_git_1line(qw(check-attr merge), $path);
231 $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
232 my $want = "topbloke-$file";
233 next if $current eq $want;
234 die "$file $current ?" unless $current eq 'unspecified';
235 die "$file $current ?" if $iteration;
237 $attrsfile = git_dir()."/info/attributes";
238 $newattrs = new IO::File "$attrsfile.tmp", 'w'
239 or die "$attrsfile.tmp: $!";
240 if (!open OA, '<', "$attrsfile") {
241 die "$attrsfile $!" unless $!==&ENOENT;
244 print $newattrs $_ or die $!;
245 print "\n" or die $! unless chomp;
248 die $! unless close OA;
251 print $newattrs "$path\tmerge=$want\n" or die $!;
254 close $newattrs or die $!;
255 rename "$attrsfile.tmp", "$attrsfile" or die $!;
259 sub check_no_unwanted_metadata ($) {
260 # for checking foreign branches aren't contaminated
261 my ($gitbranch) = @_;
262 run_git_check_nooutput('foreign unexpectedly contains',
263 qw(ls-tree --name-only),
268 sub flagsfile_add_flag ($$) {
269 # works on "deps" too
270 my ($flagsfile, $flag) = @_;
271 my $wf = wf_start(".topbloke/$flagsfile");
272 open FI, '<', ".topbloke/$flagsfile" or die $!;
275 die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
278 FI->error and die $!;
286 my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
287 return [ $fh, $path ];
291 my ($wf, $data) = @_;
292 my ($fh, $path) = @$wf;
293 print $fh $data or die "write $path.tmp: $!\n";
298 my ($fh, $path) = @$wf;
300 unlink "$path.tmp" or die "remove $path.tmp: $!\n";
305 my ($fh, $path) = @$wf;
306 close $fh or die "finish writing $path.tmp: $!\n";
307 rename "$path.tmp", $path or die "install new $path: $!\n";
310 sub wf_contents ($$) {
311 my ($path,$contents) = @_;
312 my $wf = wf_start($path);