chiark / gitweb /
nickname is optional in patch specs
[topbloke.git] / tb-create.pl
1 #!/usr/bin/perl
2 # usage: tb-create <patch-spec>
3
4 use warnings;
5 use strict;
6
7 use Getopt::Long;
8 use Topbloke;
9
10 Getopt::Long::Configure(qw(bundling));
11
12 die "bad usage\n" unless @ARGV==1;
13
14 our $spec = parse_patch_spec($ARGV[0]);
15 our $current = current_branch();
16
17 die "cannot make patch starting at base of another;".
18     " check out a real branch or patch\n" if $current->{Kind} eq 'base';
19
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');
24
25 sub fillin ($$$) {
26     my ($key, $newval, $what) = @_;
27     return if defined $spec->{$key};
28     $spec->{$key} = $newval;
29 }
30
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'); #');
36 }
37
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 $!;
41 }
42
43 defined $spec->{Nick} or die "no patch nickname specified\n";
44
45 length($spec->{Date})==18 or die "partial date specified, not supported\n";
46
47 chdir_toplevel();
48
49 check_no_unwanted_metadata('HEAD')
50     if $current->{Kind} ne 'tip';
51
52 run_git_check_nooutput("cannot create new patch with staged file(s)",
53                        qw(diff --cached --name-only HEAD --));
54
55 run_git_check_nooutput("cannot create new patch with".
56                        " modified metadata file(s)",
57                        qw(diff --name-only HEAD -- .topbloke));
58
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.
61 #
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.
64
65 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
66
67 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
68
69 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
70 $author =~ s/ \d+ [-+]\d+$// or die $!;
71
72 my $subjprefix = git_config('topbloke.subjectprefix', '');
73
74 printf "creating %s\n", $newpatch;
75
76 setup_config();
77
78 #----- subroutines for setup
79
80 sub create_and_switch ($) {
81     my ($branchref) = @_;
82     run_git(qw(update-ref -m), "tb-create base", $branchref, 'HEAD');
83     run_git(qw(symbolic-ref HEAD), $branchref);
84 }
85
86 sub stage_meta ($) {
87     my ($file) = @_;
88     run_git(qw(add), ".topbloke/$file");
89 }
90
91 sub meta_and_stage ($$) {
92     my ($file, $contents) = @_;
93     wf_contents(".topbloke/$file", $contents);
94     stage_meta($file);
95 }
96
97 #----- create the base branch
98
99 if (lstat '.topbloke') {
100     -d _ or die;
101 } else {
102     mkdir('.topbloke') or die "create .topbloke: $!\n";
103 }
104
105 my $baseref = "refs/topbloke-bases/$newpatch";
106 create_and_switch($baseref);
107
108 run_git(qw(update-ref -m), "tb-create base $newpatch", $baseref, 'HEAD');
109 run_git(qw(symbolic-ref HEAD), $baseref);
110
111 meta_and_stage('msg', "# not applicable\n");
112 meta_and_stage('deps', "# not applicable\n");
113 meta_and_stage('flags', '');
114
115 if ($current->{Kind} eq 'foreign') {
116     meta_and_stage('included', $current->{DepSpec}."\n");
117     meta_and_stage('pflags', '');
118 }
119
120 run_git(qw(commit -q -m), "tb-create base $newpatch");
121
122 #----- create the tip branch
123
124 my $tipref = "refs/topbloke-tips/$newpatch";
125 create_and_switch($tipref);
126
127 my $nm = wf_start('.topbloke/msg');
128 wf($nm, "From: $author\n");
129 foreach my $h (qw(To CC BCC)) {
130     my $estatus;
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;
134 }
135 wf($nm, <<END) or die $!;
136 Subject: [${subjprefix}PATCH] $spec->{Nick}
137
138 <patch description>
139
140 Signed-off-by: $author
141 END
142 wf_done($nm);
143 stage_meta('msg');
144
145 meta_and_stage('deps', "$current->{DepSpec}\n");
146 # we inherit empty flags from the base branch
147
148 flagsfile_add_flag('included',$newpatch);
149 stage_meta('included');
150
151 run_git(qw(commit -q -m), "tb-create tip $spec->{Nick}\n$newpatch\n");