chiark / gitweb /
591a72e0bb397b4264e073c1986b8c842a1a8a1a
[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_tb_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 check_no_unwanted_metadata('HEAD')
45     if $current->{Kind} ne 'tip';
46
47 # For the metadata files in .topbloke, we hope that the user
48 # doesn't modify them.  If they do then they get to keep all the pieces.
49 #
50 # For .topbloke/msg, if it's modified by the user (ie, if working
51 # version differs from HEAD) we keep that, and we stage it unless
52 # the cached version differs from the HEAD.
53
54 my $newbranch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
55
56 $newbranch = run_git_1line(qw(check-ref-format --print), $newbranch);
57
58 printf "creating %s\n", $newbranch;
59
60 setup_config();
61
62 if (!run_git_test_anyoutput(qw(diff --name-only HEAD -- .topbloke/msg)) {
63     open NM, '>', ".topbloke/msg.tmp" or die $!;
64     my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
65     $author =~ s/ \d+ [-+]\d+$//;
66     print NM "From: $author\n" or die $!;
67     foreach my $h (qw(To CC BCC)) {
68         my $estatus;
69         run_git(\$estatus, sub { print NM "$h: $_" or die $!; },
70                 qw(config), "topbloke.".lc $h);
71         die "$h $estatus" unless $estatus==0 || $estatus==256;
72     }
73     $subjprefix = git_config('topbloke.subjectprefix', '');
74     print NM <END or die $!;
75 Subject: [${subprefix}PATCH] $spec->{Nick}
76
77 <patch description>
78
79 Signed-off-by: $author
80 END
81     run_git(qw(add .topbloke/msg));
82     print " created and staged new .topbloke/msg\n";
83 } else {
84     if (!run_git_test_anyoutput(qw(diff --cached --name-only HEAD --
85                                    .topblokemsg))) {
86         print " staged your modified .topbloke/msg\n";
87         run_git(qw(add .topbloke/msg));
88     } else {
89         print " left your (partially staged?) .topbloke/msg\n";
90     }
91 }
92