7 &Getopt::Long::Configure('bundling');
8 &usage if !&GetOptions(
9 'branch|b=s' => \( my $master_branch = 'master' ),
10 'skip-check' => \( my $skip_branch_check ),
11 'delete' => \( my $delete_local_branches ),
12 'help|h' => \( my $help_opt ),
17 open PIPE, '-|', 'git branch -l' or die "Unable to fork: $!\n";
20 $local_branch{$1} = 1;
25 if ($delete_local_branches) {
26 foreach my $name (sort keys %local_branch) {
27 my $branch = "patch/$name";
28 system 'git', 'branch', '-D', $branch and exit 1;
33 open IN, '-|', 'git status' or die $!;
34 my $status = join('', <IN>);
36 die "The checkout is not clean:\n", $status unless $status =~ /\nnothing to commit \(working directory clean\)/;
37 die "The checkout is not on the $master_branch branch.\n" unless $status =~ /^# On branch $master_branch\n/;
42 die "File not found: $_\n";
44 die "Filename is not a .diff file: $_\n" unless /\.diff$/;
48 exit unless @patch_list;
50 my(%scanned, %created, %info);
52 foreach my $patch (@patch_list) {
53 my($where, $name) = $patch =~ m{^(.*?)([^/]+)\.diff$};
54 next if $scanned{$name}++;
56 open IN, '<', $patch or die "Unable to open $patch: $!\n";
61 if (m#^based-on: (\S+)#) {
65 last if m#^index .*\.\..* \d#;
66 last if m#^diff --git #;
67 last if m#^--- (old|a)/#;
74 my $parent = $master_branch;
75 my @patches = $info =~ m#patch -p1 <patches/(\S+)\.diff#g;
77 if ($patches[-1] eq $name) {
80 warn "No identity patch line in $patch\n";
83 $parent = pop @patches;
84 if (!$scanned{$parent}) {
85 unless (-f "$where$parent.diff") {
86 die "Unknown parent of $patch: $parent\n";
88 # Add parent to @patch_list so that we will look for the
89 # parent's parent. Any duplicates will just be ignored.
90 push @patch_list, "$where$parent.diff";
94 warn "No patch lines found in $patch\n";
97 $info{$name} = [ $parent, $info, $commit ];
100 foreach my $patch (@patch_list) {
101 create_branch($patch);
104 system 'git', 'checkout', $master_branch and exit 1;
111 my($where, $name) = $patch =~ m{^(.*?)([^/]+)\.diff$};
113 return if $created{$name}++;
115 my $ref = $info{$name};
116 my($parent, $info, $commit) = @$ref;
119 if ($parent eq $master_branch) {
120 $parent_branch = $master_branch;
121 $parent_branch = $commit if defined $commit;
123 create_branch("$where/$parent.diff");
124 $parent_branch = "patch/$parent";
127 my $branch = "patch/$name";
128 print "\n", '=' x 64, "\nProcessing $branch ($parent_branch)\n";
130 if ($local_branch{$name}) {
131 system 'git', 'branch', '-D', $branch and exit 1;
134 system 'git', 'checkout', '-b', $branch, $parent_branch and exit 1;
136 open OUT, '>', "PATCH.$name" or die $!;
139 system 'git', 'add', "PATCH.$name" and exit 1;
141 open IN, '<', $patch or die "Unable to open $patch: $!\n";
145 open PIPE, '|-', 'patch -p1' or die $!;
149 system 'rm -f *.orig */*.orig';
151 while (m#\nnew file mode (\d+)\s+--- /dev/null\s+\Q+++\E b/(.*)#g) {
153 system 'git', 'add', $2;
158 print 'Press Enter to commit, Ctrl-C to abort, or type a wild-name to add a new file: ';
165 while (system 'git', 'commit', '-a', '-m', "Creating branch from $name.diff.") {
166 exit 1 if system '/bin/zsh';
173 Usage branch-from-patch [OPTIONS] patches/DIFF...
176 -b, --branch=BRANCH Create branches relative to BRANCH if no "based-on"
177 header was found in the patch file.
178 --skip-check Skip the check that ensures starting with a clean branch.
179 --delete Delete all the local patch/* branches, not just the ones
180 that are being recreated.
181 -h, --help Output this help message.