+sub check_no_unwanted_metadata ($) {
+ # for checking foreign branches aren't contaminated
+ my ($gitbranch) = @_;
+ run_git_check_nooutput('foreign unexpectedly contains',
+ qw(ls-tree --name-only),
+ "$gitbranch:",
+ qw(.topbloke));
+}
+
+sub flagsfile_add_flag ($$) {
+ # works on "deps" too
+ my ($flagsfile, $flag) = @_;
+ my $wf = wf_start(".topbloke/$flagsfile");
+ open FI, '<', ".topbloke/$flagsfile" or die $!;
+ while (<FI>) {
+ chomp or die;
+ die "flag $flag already set in $flagsfile ?!" if $_ eq $flag;
+ wf($wf, "$_\n");
+ }
+ FI->error and die $!;
+ close FI or die $!;
+ wf($wf, "$flag\n");
+ wf_done($wf);
+}
+
+sub wf_start ($) {
+ my ($path) = @_;
+ my $fh = new IO::File "$path.tmp", '>' or die "create $path.tmp: $!\n";
+ return [ $fh, $path ];
+}
+
+sub wf ($$) {
+ my ($wf, $data) = @_;
+ my ($fh, $path) = @$wf;
+ print $fh $data or die "write $path.tmp: $!\n";
+}
+
+sub wf_abort ($) {
+ my ($wf) = @_;
+ my ($fh, $path) = @$wf;
+ close $fh;
+ unlink "$path.tmp" or die "remove $path.tmp: $!\n";
+}
+
+sub wf_done ($) {
+ my ($wf) = @_;
+ my ($fh, $path) = @$wf;
+ close $fh or die "finish writing $path.tmp: $!\n";
+ rename "$path.tmp", $path or die "install new $path: $!\n";
+}
+
+sub wf_contents ($$) {
+ my ($path,$contents) = @_;
+ my $wf = wf_start($path);
+ wf($wf, $contents);
+ wf_done($wf);
+}
+