From: Wayne Davison Date: Sat, 23 May 2009 20:40:34 +0000 (-0700) Subject: Adding a new script that creates a local patch/* branch X-Git-Url: https://mattmccutchen.net/rsync/rsync.git/commitdiff_plain/06886d36cfdcfb94f0d67f8964d22ee7c7872018 Adding a new script that creates a local patch/* branch for each file given on the command-line. --- diff --git a/packaging/branch-from-patch b/packaging/branch-from-patch new file mode 100755 index 00000000..bf854b17 --- /dev/null +++ b/packaging/branch-from-patch @@ -0,0 +1,183 @@ +#!/usr/bin/perl + +use strict; +use warnings; +use Getopt::Long; + +&Getopt::Long::Configure('bundling'); +&usage if !&GetOptions( + 'branch|b=s' => \( my $master_branch = 'master' ), + 'skip-check' => \( my $skip_branch_check ), + 'delete' => \( my $delete_local_branches ), + 'help|h' => \( my $help_opt ), +); +&usage if $help_opt; + +my %local_branch; +open PIPE, '-|', 'git branch -l' or die "Unable to fork: $!\n"; +while () { + if (m# patch/(.*)#) { + $local_branch{$1} = 1; + } +} +close PIPE; + +if ($delete_local_branches) { + foreach my $name (sort keys %local_branch) { + my $branch = "patch/$name"; + system 'git', 'branch', '-D', $branch and exit 1; + } + %local_branch = ( ); +} + +open IN, '-|', 'git status' or die $!; +my $status = join('', ); +close IN; +die "The checkout is not clean:\n", $status unless $status =~ /\nnothing to commit \(working directory clean\)/; +die "The checkout is not on the $master_branch branch.\n" unless $status =~ /^# On branch $master_branch\n/; + +my @patch_list; +foreach (@ARGV) { + if (!-f $_) { + die "File not found: $_\n"; + } + die "Filename is not a .diff file: $_\n" unless /\.diff$/; + push @patch_list, $_; +} + +exit unless @patch_list; + +my(%scanned, %created, %info); + +foreach my $patch (@patch_list) { + my($where, $name) = $patch =~ m{^(.*?)([^/]+)\.diff$}; + next if $scanned{$name}++; + + open IN, '<', $patch or die "Unable to open $patch: $!\n"; + + my $info = ''; + my $commit; + while () { + if (m#^based-on: (\S+)#) { + $commit = $1; + last; + } + last if m#^index .*\.\..* \d#; + last if m#^diff --git #; + last if m#^--- (old|a)/#; + $info .= $_; + } + close IN; + + $info =~ s/\s+\Z/\n/; + + my $parent = $master_branch; + my @patches = $info =~ m#patch -p1 ', "PATCH.$name" or die $!; + print OUT $info; + close OUT; + system 'git', 'add', "PATCH.$name" and exit 1; + + open IN, '<', $patch or die "Unable to open $patch: $!\n"; + $_ = join('', ); + close IN; + + open PIPE, '|-', 'patch -p1' or die $!; + print PIPE $_; + close PIPE; + + system 'rm -f *.orig */*.orig'; + + while (m#\nnew file mode (\d+)\s+--- /dev/null\s+\Q+++\E b/(.*)#g) { + chmod oct($1), $2; + system 'git', 'add', $2; + } + + while (1) { + system 'git status'; + print 'Press Enter to commit, Ctrl-C to abort, or type a wild-name to add a new file: '; + $_ = ; + last if /^$/; + chomp; + system "git add $_"; + } + + while (system 'git', 'commit', '-a', '-m', "Creating branch from $name.diff.") { + exit 1 if system '/bin/zsh'; + } +} + +sub usage +{ + die <