2 # usage: tb-create <patch-spec>
10 fixme needs update to new metadata;
12 Getopt::Long::Configure(qw(bundling));
14 die "bad usage\n" unless @ARGV==1;
16 our $spec = parse_patch_spec($ARGV[0]);
17 our $current = current_branch();
19 die "cannot make patch starting at base of another;".
20 " check out a real branch or patch\n" if $current->{Kind} eq 'base';
22 die "strange branch ref $current->{Kind} $current->{Ref},\n".
23 " making new patch with this as dep is unwise\n"
24 unless ($current->{Kind} eq 'foreign' ||
25 $current->{Kind} eq 'tip');
28 my ($key, $newval, $what) = @_;
29 return if defined $spec->{$key};
30 $spec->{$key} = $newval;
33 if (!defined $spec->{Email} || !defined $spec->{Domain}) {
34 my $eaddr = run_git_1line(qw(config user.email));
35 $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
36 fillin('Email',$1,'email domain');
37 fillin('Domain',$','email domain'); #');
40 if (!defined $spec->{Date}) {
41 $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
42 chomp $spec->{Date} or die $!;
45 defined $spec->{Nick} or die "no patch nickname specified\n";
47 length($spec->{Date})==18 or die "partial date specified, not supported\n";
51 check_no_unwanted_metadata('HEAD')
52 if $current->{Kind} ne 'tip';
54 run_git_check_nooutput("cannot create new patch with staged file(s)",
55 qw(diff --cached --name-only HEAD --));
57 run_git_check_nooutput("cannot create new patch with".
58 " modified metadata file(s)",
59 qw(diff --name-only HEAD -- .topbloke));
61 # For the metadata files in .topbloke, we hope that the user
62 # doesn't modify them. If they do then they get to keep all the pieces.
64 # For .topbloke/msg, if it's modified by the user (ie, if working
65 # version differs from HEAD) we keep that and stage it.
67 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
69 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
71 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
72 $author =~ s/ \d+ [-+]\d+$// or die $!;
74 my $subjprefix = git_config('topbloke.subjectprefix', '');
76 printf "creating %s\n", $newpatch;
80 #----- subroutines for setup
82 sub create_and_switch ($$) {
83 my ($branchref, $what) = @_;
84 enable_reflog($branchref);
85 run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
86 run_git(qw(symbolic-ref HEAD), $branchref);
91 run_git(qw(add), ".topbloke/$file");
94 sub meta_and_stage ($$) {
95 my ($file, $contents) = @_;
96 wf_contents(".topbloke/$file", $contents);
100 #----- create the base branch
102 if (lstat '.topbloke') {
105 mkdir('.topbloke') or die "create .topbloke: $!\n";
108 my $baseref = "refs/topbloke-bases/$newpatch";
109 create_and_switch($baseref, 'base');
111 meta_and_stage('msg', "# not applicable\n");
112 meta_and_stage('deps', "");
113 meta_and_stage('props', "patch $current->{Fullname}\n");
115 if ($current->{Kind} eq 'foreign') {
116 meta_and_stage('included', $current->{DepSpec}."\n");
117 meta_and_stage('pprops', '');
120 run_git(qw(commit -q -m), "tb-create $newpatch base");
122 #----- create the tip branch
124 my $tipref = "refs/topbloke-tips/$newpatch";
125 create_and_switch($tipref, 'tip');
127 my $nm = wf_start('.topbloke/msg');
128 wf($nm, "From: $author\n");
129 foreach my $h (qw(To CC BCC)) {
131 run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
132 qw(config), "topbloke.".lc $h);
133 die "$h $estatus" unless $estatus==0 || $estatus==256;
135 wf($nm, <<END) or die $!;
136 Subject: [${subjprefix}PATCH] $spec->{Nick}
140 Signed-off-by: $author
145 meta_and_stage('deps', "$current->{DepSpec}\n");
146 # we inherit correct props and pprops from the base branch
148 depsfile_add_dep('included','tb',$newpatch);
149 stage_meta('included');
151 run_git(qw(commit -q -m), "tb-create $newpatch tip");