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