2 # usage: tb-create <patch-spec>
10 Getopt::Long::Configure(qw(bundling));
12 die "bad usage\n" unless @ARGV==1;
14 our $spec = parse_patch_spec($ARGV[0]);
15 our $current = current_branch();
17 die "cannot make patch starting at base of another;".
18 " check out a real branch or patch\n" if $current->{Kind} eq 'base';
20 die "strange branch ref $current->{Kind} $current->{Ref},\n".
21 " making new patch with this as dep is unwise\n"
22 unless ($current->{Kind} eq 'foreign' ||
23 $current->{Kind} eq 'tip');
26 my ($key, $newval, $what) = @_;
27 return if defined $spec->{$key};
28 $spec->{$key} = $newval;
31 if (!defined $spec->{Email} || !defined $spec->{Domain}) {
32 my $eaddr = run_git_1line(qw(config user.email));
33 $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
34 fillin('Email',$1,'email domain');
35 fillin('Domain',$','email domain'); #');
38 if (!defined $spec->{Date}) {
39 $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
40 chomp $spec->{Date} or die $!;
43 defined $spec->{Nick} or die "no patch nickname specified\n";
45 length($spec->{Date})==18 or die "partial date specified, not supported\n";
49 check_no_unwanted_metadata('HEAD')
50 if $current->{Kind} ne 'tip';
52 run_git_check_nooutput("cannot create new patch with staged file(s)",
53 qw(diff --cached --name-only HEAD --));
55 run_git_check_nooutput("cannot create new patch with".
56 " modified metadata file(s)",
57 qw(diff --name-only HEAD -- .topbloke));
59 # For the metadata files in .topbloke, we hope that the user
60 # doesn't modify them. If they do then they get to keep all the pieces.
62 # For .topbloke/msg, if it's modified by the user (ie, if working
63 # version differs from HEAD) we keep that and stage it.
65 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
67 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
69 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
70 $author =~ s/ \d+ [-+]\d+$// or die $!;
72 my $subjprefix = git_config('topbloke.subjectprefix', '');
74 printf "creating %s\n", $newpatch;
78 #----- subroutines for setup
80 sub create_and_switch ($$) {
81 my ($branchref, $what) = @_;
82 enable_reflog($branchref);
83 run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
84 run_git(qw(symbolic-ref HEAD), $branchref);
89 run_git(qw(add), ".topbloke/$file");
92 sub meta_and_stage ($$) {
93 my ($file, $contents) = @_;
94 wf_contents(".topbloke/$file", $contents);
98 #----- create the base branch
100 if (lstat '.topbloke') {
103 mkdir('.topbloke') or die "create .topbloke: $!\n";
106 my $baseref = "refs/topbloke-bases/$newpatch";
107 create_and_switch($baseref, 'base');
109 meta_and_stage('msg', "# not applicable\n");
110 meta_and_stage('deps', "");
111 meta_and_stage('props', "patch $current->{Fullname}\n");
113 if ($current->{Kind} eq 'foreign') {
114 meta_and_stage('included', $current->{DepSpec}."\n");
115 meta_and_stage('pprops', '');
118 run_git(qw(commit -q -m), "tb-create $newpatch base");
120 #----- create the tip branch
122 my $tipref = "refs/topbloke-tips/$newpatch";
123 create_and_switch($tipref, 'tip');
125 my $nm = wf_start('.topbloke/msg');
126 wf($nm, "From: $author\n");
127 foreach my $h (qw(To CC BCC)) {
129 run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
130 qw(config), "topbloke.".lc $h);
131 die "$h $estatus" unless $estatus==0 || $estatus==256;
133 wf($nm, <<END) or die $!;
134 Subject: [${subjprefix}PATCH] $spec->{Nick}
138 Signed-off-by: $author
143 meta_and_stage('deps', "$current->{DepSpec}\n");
144 # we inherit correct props and pprops from the base branch
146 depsfile_add_dep('included','tb',$newpatch);
147 stage_meta('included');
149 run_git(qw(commit -q -m), "tb-create $newpatch tip");