+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 check_clean_tree ($) {
+ run_git_check_nooutput("operation requires working tree to be clean",
+ qw(diff --name-only HEAD --));
+ run_git_check_nooutput("operation cannot proceed with staged changes",
+ qw(diff --cached --name-only HEAD --));
+}
+
+#----- configuring a tree -----
+
+sub setup_config () {
+ my (@files) = (qw(msg deps included props pprops));
+ my $version = 1;
+ foreach my $iteration (qw(0 1)) {
+ foreach my $file (@files) {
+ my $cfgname = "merge.topbloke-$file";
+ my ($current, $current_estatus);
+ run_git(\$current_estatus,
+ sub { $current = $_; },
+ qw(config), "$cfgname.driver");
+ $current = "## failed $current_estatus" if $current_estatus;
+ next if $current =~ m/^topbloke-merge-driver --v$version /o;
+ die "$file $current ?" if $iteration;
+ debug("setting merge driver $file");
+ run_git(qw(config), "$cfgname.name",
+ "topbloke merge driver for $file");
+ run_git(qw(config), "$cfgname.driver",
+ "topbloke-merge-driver --v$version".
+ " $file %O %A %B %L");
+ }
+ my ($newattrs, $attrsfile);
+ foreach my $file (@files) {
+ my $path = ".topbloke/$file";
+ my $current = run_git_1line(qw(check-attr merge), $path);
+ $current =~ s#^\Q$path\E: merge: ## or die "$file $current ?";
+ my $want = "topbloke-$file";
+ next if $current eq $want;
+ die "$file $current ?" unless $current eq 'unspecified';
+ die "$file $current ?" if $iteration;
+ if (!$newattrs) {
+ $attrsfile = git_dir()."/info/attributes";
+ $newattrs = new IO::File "$attrsfile.tmp", 'w'
+ or die "$attrsfile.tmp: $!";
+ if (!open OA, '<', "$attrsfile") {
+ die "$attrsfile $!" unless $!==&ENOENT;
+ } else {
+ while (<OA>) {
+ print $newattrs $_ or die $!;
+ print "\n" or die $! unless chomp;
+ }
+ die $! if OA->error;
+ die $! unless close OA;
+ }
+ }
+ print $newattrs "$path\tmerge=$want\n" or die $!;
+ }
+ last if !$newattrs;
+ close $newattrs or die $!;
+ rename "$attrsfile.tmp", "$attrsfile" or die $!;
+ }
+}
+
+#----- branch and patch specs and parsed patch names -----
+
+our $tiprefs = 'refs/topbloke-tips';
+our $baserefs = 'refs/topbloke-bases';
+