chiark / gitweb /
16954d4c09bacea7210817b5808c76917137fdc1
[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 length($spec->{Date})==18 or die "partial date specified, not supported\n";
44
45 chdir_toplevel();
46
47 check_no_unwanted_metadata('HEAD')
48     if $current->{Kind} ne 'tip';
49
50 run_git_check_nooutput("cannot create new patch with staged file(s)",
51                        qw(diff --cached --name-only HEAD --));
52
53 run_git_check_nooutput("cannot create new patch with".
54                        " modified metadata file(s)",
55                        qw(diff --name-only HEAD -- .topbloke));
56
57 # For the metadata files in .topbloke, we hope that the user
58 # doesn't modify them.  If they do then they get to keep all the pieces.
59 #
60 # For .topbloke/msg, if it's modified by the user (ie, if working
61 # version differs from HEAD) we keep that and stage it.
62
63 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
64
65 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
66
67 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
68 $author =~ s/ \d+ [-+]\d+$// or die $!;
69
70 my $subjprefix = git_config('topbloke.subjectprefix', '');
71
72 printf "creating %s\n", $newpatch;
73
74 setup_config();
75
76 #----- subroutines for setup
77
78 sub create_and_switch ($) {
79     my ($branchref) = @_;
80     run_git(qw(update-ref -m), "tb-create base", $branchref, 'HEAD');
81     run_git(qw(symbolic-ref HEAD), $branchref);
82 }
83
84 sub stage_meta ($) {
85     my ($file) = @_;
86     run_git(qw(add), ".topbloke/$file");
87 }
88
89 sub meta_and_stage ($$) {
90     my ($file, $contents) = @_;
91     wf_contents(".topbloke/$file", $contents);
92     stage_meta($file);
93 }
94
95 #----- create the base branch
96
97 if (lstat '.topbloke') {
98     -d _ or die;
99 } else {
100     mkdir('.topbloke') or die "create .topbloke: $!\n";
101 }
102
103 my $baseref = "refs/topbloke-bases/$newpatch";
104 create_and_switch($baseref);
105
106 run_git(qw(update-ref -m), "tb-create base $newpatch", $baseref, 'HEAD');
107 run_git(qw(symbolic-ref HEAD), $baseref);
108
109 meta_and_stage('msg', "# not applicable\n");
110 meta_and_stage('deps', "# not applicable\n");
111 meta_and_stage('flags', '');
112
113 if ($current->{Kind} eq 'foreign') {
114     meta_and_stage('included', $current->{DepSpec}."\n");
115     meta_and_stage('pflags', '');
116 }
117
118 run_git(qw(commit -q -m), "tb-create base $newpatch");
119
120 #----- create the tip branch
121
122 my $tipref = "refs/topbloke-tips/$newpatch";
123 create_and_switch($tipref);
124
125 my $nm = wf_start('.topbloke/msg');
126 wf($nm, "From: $author\n");
127 foreach my $h (qw(To CC BCC)) {
128     my $estatus;
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;
132 }
133 wf($nm, <<END) or die $!;
134 Subject: [${subjprefix}PATCH] $spec->{Nick}
135
136 <patch description>
137
138 Signed-off-by: $author
139 END
140 wf_done($nm);
141 stage_meta('msg');
142
143 meta_and_stage('deps', "$current->{DepSpec}\n");
144 # we inherit empty flags from the base branch
145
146 flagsfile_add_flag('included',$newpatch);
147 stage_meta('included');
148
149 run_git(qw(commit -q -m), "tb-create tip $spec->{Nick}\n$newpatch\n");