chiark / gitweb /
fix a few bugs in THEORY
[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 "strange branch ref $current->{Ref} is of kind $current->{Kind},\n".
18     " making new patch with this as dep is not supported\n"
19     unless ($current->{Kind} eq 'foreign' ||
20             $current->{Kind} eq 'tip');
21
22 sub fillin ($$$) {
23     my ($key, $newval, $what) = @_;
24     return if defined $spec->{$key};
25     $spec->{$key} = $newval;
26 }
27
28 if (!defined $spec->{Email} || !defined $spec->{Domain}) {
29     my $eaddr = run_git_1line(qw(config user.email));
30     $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
31     fillin('Email',$1,'email domain');
32     fillin('Domain',$','email domain'); #');
33 }
34
35 if (!defined $spec->{Date}) {
36     $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
37     chomp $spec->{Date} or die $!;
38 }
39
40 defined $spec->{Nick} or die "no patch nickname specified\n";
41
42 length($spec->{Date})==18 or die "partial date specified, not supported\n";
43
44 chdir_toplevel();
45
46 run_git_check_nooutput("cannot create new patch with staged file(s)",
47                        qw(diff --cached --name-only HEAD --));
48
49 run_git_check_nooutput("cannot create new patch with".
50                        " modified metadata file(s)",
51                        qw(diff --name-only HEAD -- .topbloke));
52
53 # For the metadata files in .topbloke, we hope that the user
54 # doesn't modify them.  If they do then they get to keep all the pieces.
55 #
56 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
57
58 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
59
60 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
61 $author =~ s/ \d+ [-+]\d+$// or die $!;
62
63 my $subjprefix = git_config('topbloke.subjectprefix', '');
64
65 printf "creating %s\n", $newpatch;
66
67 setup_config();
68
69 #----- subroutines for setup
70
71 sub create_and_switch ($$) {
72     my ($branchref, $what) = @_;
73     enable_reflog($branchref);
74     run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
75     run_git(qw(symbolic-ref HEAD), $branchref);
76 }
77
78 sub stage_meta ($) {
79     my ($file) = @_;
80     run_git(qw(add), ".topbloke/$file");
81 }
82
83 sub meta_and_stage ($$) {
84     my ($file, $contents) = @_;
85     wf_contents(".topbloke/$file", $contents);
86     stage_meta($file);
87 }
88
89 sub meta_rm_stage ($) {
90     my ($file) = @_;
91     run_git(qw(rm --ignore-unmatch -q --), ".topbloke/$file");
92 }
93
94 #----- create the base branch
95
96 if (lstat '.topbloke') {
97     -d _ or die;
98 } else {
99     mkdir('.topbloke') or die "create .topbloke: $!\n";
100 }
101
102 my @meta_to_rm;
103
104 if ($current->{Kind} eq 'foreign') {
105     check_no_metadata('HEAD');
106 } else {
107     foreach_unknown_metadata('HEAD', 
108                              sub { push @meta_to_rm, $_ unless m/^\+/; });
109 }
110
111 my $baseref = "$baserefs/$newpatch";
112 my $currentcommit = run_git_1line(qw(rev-parse), "HEAD");
113 create_and_switch($baseref, 'base');
114
115 meta_rm_stage('msg');
116 meta_and_stage('patch', "$newpatch\n");
117 meta_rm_stage('base');
118 meta_and_stage('deps', "$current->{DepSpec}\n");
119 meta_rm_stage('deleted');
120 meta_rm_stage($_) foreach @meta_to_rm;
121
122 if ($current->{Kind} eq 'foreign') {
123     meta_and_stage('+included', "");
124     meta_and_stage('+ends', "");
125 } else {
126     # we inherit correct contents for +included
127     if ($current->{Kind} eq 'tip') {
128         metafile_process('+ends', undef, sub {
129             die if m/^\Q$current->{Fullname}\E /;
130         }, sub {
131             wf($_->[0], "$current->{Fullname} $currentcommit\n");
132         }, undef);
133         stage_meta('+ends');
134     }
135 }
136
137 run_git(qw(commit -q -m), "tb-create $newpatch base");
138 my $basecommit = run_git_1line(qw(rev-parse), "$baseref~0");
139
140 #----- create the tip branch
141
142 my $tipref = "$tiprefs/$newpatch";
143 create_and_switch($tipref, 'tip');
144
145 my $nm = wf_start('.topbloke/msg');
146 wf($nm, "From: $author\n");
147 foreach my $h (qw(To CC BCC)) {
148     my $estatus;
149     run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
150             qw(config), "topbloke.".lc $h);
151     die "$h $estatus" unless $estatus==0 || $estatus==256;
152 }
153 wf($nm, <<END) or die $!;
154 Subject: [${subjprefix}PATCH] $spec->{Nick}
155
156 <patch description>
157
158 Signed-off-by: $author
159 END
160 wf_done($nm);
161 stage_meta('msg');
162
163 meta_and_stage('base', "$basecommit\n");
164 meta_rm_stage('deps');
165 depsfile_add_dep('+included',$newpatch);
166 stage_meta('+included');
167
168 run_git(qw(commit -q -m), "tb-create $newpatch tip");