-if (!run_git_test_anyoutput(qw(diff --name-only HEAD -- .topbloke/msg)) {
- open NM, '>', ".topbloke/msg.tmp" or die $!;
- my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
- $author =~ s/ \d+ [-+]\d+$//;
- print NM "From: $author\n" or die $!;
- foreach my $h (qw(To CC BCC)) {
- my $estatus;
- run_git(\$estatus, sub { print NM "$h: $_" or die $!; },
- qw(config), "topbloke.".lc $h);
- die "$h $estatus" unless $estatus==0 || $estatus==256;
- }
- $subjprefix = git_config('topbloke.subjectprefix', '');
- print NM <END or die $!;
-Subject: [${subprefix}PATCH] $spec->{Nick}
+#----- subroutines for setup
+
+sub create_and_switch ($$) {
+ my ($branchref, $what) = @_;
+ enable_reflog($branchref);
+ run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
+ run_git(qw(symbolic-ref HEAD), $branchref);
+}
+
+sub stage_meta ($) {
+ my ($file) = @_;
+ run_git(qw(add), ".topbloke/$file");
+}
+
+sub meta_and_stage ($$) {
+ my ($file, $contents) = @_;
+ wf_contents(".topbloke/$file", $contents);
+ stage_meta($file);
+}
+
+#----- create the base branch
+
+if (lstat '.topbloke') {
+ -d _ or die;
+} else {
+ mkdir('.topbloke') or die "create .topbloke: $!\n";
+}
+
+my $baseref = "refs/topbloke-bases/$newpatch";
+create_and_switch($baseref, 'base');
+
+meta_and_stage('msg', "# not applicable\n");
+meta_and_stage('deps', "");
+meta_and_stage('props', "patch $current->{Fullname}\n");
+
+if ($current->{Kind} eq 'foreign') {
+ meta_and_stage('included', $current->{DepSpec}."\n");
+ meta_and_stage('pprops', '');
+}
+
+run_git(qw(commit -q -m), "tb-create $newpatch base");
+
+#----- create the tip branch
+
+my $tipref = "refs/topbloke-tips/$newpatch";
+create_and_switch($tipref, 'tip');
+
+my $nm = wf_start('.topbloke/msg');
+wf($nm, "From: $author\n");
+foreach my $h (qw(To CC BCC)) {
+ my $estatus;
+ run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
+ qw(config), "topbloke.".lc $h);
+ die "$h $estatus" unless $estatus==0 || $estatus==256;
+}
+wf($nm, <<END) or die $!;
+Subject: [${subjprefix}PATCH] $spec->{Nick}