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 "cannot make patch starting at base of another;".
18 " check out a real branch or patch\n" if $current->{Kind} eq 'base';
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');
26 my ($key, $newval, $what) = @_;
27 return if defined $spec->{$key};
28 $spec->{$key} = $newval;
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'); #');
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 $!;
43 length($spec->{Date})==18 or die "partial date specified, not supported\n";
47 check_no_unwanted_metadata('HEAD')
48 if $current->{Kind} ne 'tip';
50 run_git_check_nooutput("cannot create new patch with staged file(s)",
51 qw(diff --cached --name-only HEAD --));
53 run_git_check_nooutput("cannot create new patch with".
54 " modified metadata file(s)",
55 qw(diff --name-only HEAD -- .topbloke));
57 # For the metadata files in .topbloke, we hope that the user
58 # doesn't modify them. If they do then they get to keep all the pieces.
60 # For .topbloke/msg, if it's modified by the user (ie, if working
61 # version differs from HEAD) we keep that and stage it.
63 my $newpatch = "$spec->{Email}\@$spec->{Domain}/$spec->{Date}/$spec->{Nick}";
65 $newpatch = run_git_1line(qw(check-ref-format --print), $newpatch);
67 my $author = run_git_1line(qw(var GIT_AUTHOR_IDENT));
68 $author =~ s/ \d+ [-+]\d+$// or die $!;
70 my $subjprefix = git_config('topbloke.subjectprefix', '');
72 printf "creating %s\n", $newpatch;
76 #----- subroutines for setup
78 sub create_and_switch ($) {
80 run_git(qw(update-ref -m), "tb-create base", $branchref, 'HEAD');
81 run_git(qw(symbolic-ref HEAD), $branchref);
86 run_git(qw(add), ".topbloke/$file");
89 sub meta_and_stage ($$) {
90 my ($file, $contents) = @_;
91 wf_contents(".topbloke/$file", $contents);
95 #----- create the base branch
97 if (lstat '.topbloke') {
100 mkdir('.topbloke') or die "create .topbloke: $!\n";
103 my $baseref = "refs/topbloke-bases/$newpatch";
104 create_and_switch($baseref);
106 run_git(qw(update-ref -m), "tb-create base $newpatch", $baseref, 'HEAD');
107 run_git(qw(symbolic-ref HEAD), $baseref);
109 meta_and_stage('msg', "# not applicable\n");
110 meta_and_stage('deps', "# not applicable\n");
111 meta_and_stage('flags', '');
113 if ($current->{Kind} eq 'foreign') {
114 meta_and_stage('included', $current->{DepSpec}."\n");
115 meta_and_stage('pflags', '');
118 run_git(qw(commit -q -m), "tb-create base $newpatch");
120 #----- create the tip branch
122 my $tipref = "refs/topbloke-tips/$newpatch";
123 create_and_switch($tipref);
125 my $nm = wf_start('.topbloke/msg');
126 wf($nm, "From: $author\n");
127 foreach my $h (qw(To CC BCC)) {
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;
133 wf($nm, <<END) or die $!;
134 Subject: [${subjprefix}PATCH] $spec->{Nick}
138 Signed-off-by: $author
143 meta_and_stage('deps', "$current->{DepSpec}\n");
144 # we inherit empty flags from the base branch
146 flagsfile_add_flag('included',$newpatch);
147 stage_meta('included');
149 run_git(qw(commit -q -m), "tb-create tip $spec->{Nick}\n$newpatch\n");