chiark / gitweb /
Some thoughts
[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, $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);
85 }
86
87 sub stage_meta ($) {
88     my ($file) = @_;
89     run_git(qw(add), ".topbloke/$file");
90 }
91
92 sub meta_and_stage ($$) {
93     my ($file, $contents) = @_;
94     wf_contents(".topbloke/$file", $contents);
95     stage_meta($file);
96 }
97
98 #----- create the base branch
99
100 if (lstat '.topbloke') {
101     -d _ or die;
102 } else {
103     mkdir('.topbloke') or die "create .topbloke: $!\n";
104 }
105
106 my $baseref = "refs/topbloke-bases/$newpatch";
107 create_and_switch($baseref, 'base');
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 $newpatch base");
119
120 #----- create the tip branch
121
122 my $tipref = "refs/topbloke-tips/$newpatch";
123 create_and_switch($tipref, 'tip');
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 $newpatch tip");