chiark / gitweb /
b01010b8333172c0af35bcf60b0ce0d910a3e83c
[topbloke.git] / tb-create.pl
1 #!/usr/bin/perl
2 # usage: tb-create <patch-spec>
3
4 xxx needs updating for new metadata and new theory
5
6 use warnings;
7 use strict;
8
9 use Getopt::Long;
10 use Topbloke;
11
12 fixme needs update to new metadata;
13
14 Getopt::Long::Configure(qw(bundling));
15
16 die "bad usage\n" unless @ARGV==1;
17
18 our $spec = parse_patch_spec($ARGV[0]);
19 our $current = current_branch();
20
21 die "cannot make patch starting at base of another;".
22     " check out a real branch or patch\n" if $current->{Kind} eq 'base';
23
24 die "strange branch ref $current->{Kind} $current->{Ref},\n".
25     " making new patch with this as dep is unwise\n"
26     unless ($current->{Kind} eq 'foreign' ||
27             $current->{Kind} eq 'tip');
28
29 sub fillin ($$$) {
30     my ($key, $newval, $what) = @_;
31     return if defined $spec->{$key};
32     $spec->{$key} = $newval;
33 }
34
35 if (!defined $spec->{Email} || !defined $spec->{Domain}) {
36     my $eaddr = run_git_1line(qw(config user.email));
37     $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
38     fillin('Email',$1,'email domain');
39     fillin('Domain',$','email domain'); #');
40 }
41
42 if (!defined $spec->{Date}) {
43     $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
44     chomp $spec->{Date} or die $!;
45 }
46
47 defined $spec->{Nick} or die "no patch nickname specified\n";
48
49 length($spec->{Date})==18 or die "partial date specified, not supported\n";
50
51 chdir_toplevel();
52
53 check_no_unwanted_metadata('HEAD')
54     if $current->{Kind} ne 'tip';
55
56 run_git_check_nooutput("cannot create new patch with staged file(s)",
57                        qw(diff --cached --name-only HEAD --));
58
59 run_git_check_nooutput("cannot create new patch with".
60                        " modified metadata file(s)",
61                        qw(diff --name-only HEAD -- .topbloke));
62
63 # For the metadata files in .topbloke, we hope that the user
64 # doesn't modify them.  If they do then they get to keep all the pieces.
65 #
66 # For .topbloke/msg, if it's modified by the user (ie, if working
67 # version differs from HEAD) we keep that and stage it.
68
69 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
70
71 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
72
73 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
74 $author =~ s/ \d+ [-+]\d+$// or die $!;
75
76 my $subjprefix = git_config('topbloke.subjectprefix', '');
77
78 printf "creating %s\n", $newpatch;
79
80 setup_config();
81
82 #----- subroutines for setup
83
84 sub create_and_switch ($$) {
85     my ($branchref, $what) = @_;
86     enable_reflog($branchref);
87     run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
88     run_git(qw(symbolic-ref HEAD), $branchref);
89 }
90
91 sub stage_meta ($) {
92     my ($file) = @_;
93     run_git(qw(add), ".topbloke/$file");
94 }
95
96 sub meta_and_stage ($$) {
97     my ($file, $contents) = @_;
98     wf_contents(".topbloke/$file", $contents);
99     stage_meta($file);
100 }
101
102 #----- create the base branch
103
104 if (lstat '.topbloke') {
105     -d _ or die;
106 } else {
107     mkdir('.topbloke') or die "create .topbloke: $!\n";
108 }
109
110 my $baseref = "refs/topbloke-bases/$newpatch";
111 create_and_switch($baseref, 'base');
112
113 meta_and_stage('msg', "# not applicable\n");
114 meta_and_stage('deps', "");
115 meta_and_stage('props', "patch $current->{Fullname}\n");
116
117 if ($current->{Kind} eq 'foreign') {
118     meta_and_stage('included', $current->{DepSpec}."\n");
119     meta_and_stage('pprops', '');
120 }
121
122 run_git(qw(commit -q -m), "tb-create $newpatch base");
123
124 #----- create the tip branch
125
126 my $tipref = "refs/topbloke-tips/$newpatch";
127 create_and_switch($tipref, 'tip');
128
129 my $nm = wf_start('.topbloke/msg');
130 wf($nm, "From: $author\n");
131 foreach my $h (qw(To CC BCC)) {
132     my $estatus;
133     run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
134             qw(config), "topbloke.".lc $h);
135     die "$h $estatus" unless $estatus==0 || $estatus==256;
136 }
137 wf($nm, <<END) or die $!;
138 Subject: [${subjprefix}PATCH] $spec->{Nick}
139
140 <patch description>
141
142 Signed-off-by: $author
143 END
144 wf_done($nm);
145 stage_meta('msg');
146
147 meta_and_stage('deps', "$current->{DepSpec}\n");
148 # we inherit correct props and pprops from the base branch
149
150 depsfile_add_dep('included','tb',$newpatch);
151 stage_meta('included');
152
153 run_git(qw(commit -q -m), "tb-create $newpatch tip");