2 # usage: tb-create <patch-spec>
10 Getopt::Long::Configure(qw(bundling));
12 die "bad usage\n" unless @ARGV==1;
14 our $spec = parse_patch_spec($ARGV[0]);
15 our $current = current_branch();
17 die "strange branch ref $current->{Ref} is of kind $current->{Kind},\n".
18 " making new patch with this as dep is not supported\n"
19 unless ($current->{Kind} eq 'foreign' ||
20 $current->{Kind} eq 'tip');
23 my ($key, $newval, $what) = @_;
24 return if defined $spec->{$key};
25 $spec->{$key} = $newval;
28 if (!defined $spec->{Email} || !defined $spec->{Domain}) {
29 my $eaddr = run_git_1line(qw(config user.email));
30 $eaddr =~ m/^(.*)\@/ or die "$eaddr ?";
31 fillin('Email',$1,'email domain');
32 fillin('Domain',$','email domain'); #');
35 if (!defined $spec->{Date}) {
36 $spec->{Date} = `LC_TIME=C date -u +%Y-%m-%dT%H%M%SZ`;
37 chomp $spec->{Date} or die $!;
40 defined $spec->{Nick} or die "no patch nickname specified\n";
42 length($spec->{Date})==18 or die "partial date specified, not supported\n";
46 run_git_check_nooutput("cannot create new patch with staged file(s)",
47 qw(diff --cached --name-only HEAD --));
49 run_git_check_nooutput("cannot create new patch with".
50 " modified metadata file(s)",
51 qw(diff --name-only HEAD -- .topbloke));
53 # For the metadata files in .topbloke, we hope that the user
54 # doesn't modify them. If they do then they get to keep all the pieces.
56 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
58 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
60 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
61 $author =~ s/ \d+ [-+]\d+$// or die $!;
63 my $subjprefix = git_config('topbloke.subjectprefix', '');
65 printf "creating %s\n", $newpatch;
69 #----- subroutines for setup
71 sub create_and_switch ($$) {
72 my ($branchref, $what) = @_;
73 enable_reflog($branchref);
74 run_git(qw(update-ref -m), "tb-create $newpatch $what", $branchref, 'HEAD');
75 run_git(qw(symbolic-ref HEAD), $branchref);
80 run_git(qw(add), ".topbloke/$file");
83 sub meta_and_stage ($$) {
84 my ($file, $contents) = @_;
85 wf_contents(".topbloke/$file", $contents);
89 sub meta_rm_stage ($) {
91 run_git(qw(rm --ignore-unmatch -q --), ".topbloke/$file");
94 #----- create the base branch
96 if (lstat '.topbloke') {
99 mkdir('.topbloke') or die "create .topbloke: $!\n";
104 if ($current->{Kind} eq 'foreign') {
105 check_no_metadata('HEAD');
107 foreach_unknown_metadata('HEAD',
108 sub { push @meta_to_rm, $_ unless m/^\+/; });
111 my $baseref = "$baserefs/$newpatch";
112 my $currentcommit = run_git_1line(qw(rev-parse), "HEAD");
113 create_and_switch($baseref, 'base');
115 meta_rm_stage('msg');
116 meta_and_stage('patch', "$newpatch\n");
117 meta_rm_stage('base');
118 meta_and_stage('deps', "$current->{DepSpec}\n");
119 meta_rm_stage('deleted');
120 meta_rm_stage($_) foreach @meta_to_rm;
122 if ($current->{Kind} eq 'foreign') {
123 meta_and_stage('+included', "");
124 meta_and_stage('+ends', "");
126 # we inherit correct contents for +included
127 if ($current->{Kind} eq 'tip') {
128 metafile_process('+ends', undef, sub {
129 die if m/^\Q$current->{Fullname}\E /;
131 wf($_->[0], "$current->{Fullname} $currentcommit\n");
137 run_git(qw(commit -q -m), "tb-create $newpatch base");
138 my $basecommit = run_git_1line(qw(rev-parse), "$baseref~0");
140 #----- create the tip branch
142 my $tipref = "$tiprefs/$newpatch";
143 create_and_switch($tipref, 'tip');
145 my $nm = wf_start('.topbloke/msg');
146 wf($nm, "From: $author\n");
147 foreach my $h (qw(To CC BCC)) {
149 run_git(\$estatus, sub { wf($nm, "$h: $_") or die $!; },
150 qw(config), "topbloke.".lc $h);
151 die "$h $estatus" unless $estatus==0 || $estatus==256;
153 wf($nm, <<END) or die $!;
154 Subject: [${subjprefix}PATCH] $spec->{Nick}
158 Signed-off-by: $author
163 meta_and_stage('base', "$basecommit\n");
164 meta_rm_stage('deps');
165 depsfile_add_dep('+included',$newpatch);
166 stage_meta('+included');
168 run_git(qw(commit -q -m), "tb-create $newpatch tip");