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