2 # This script is used to turn one or more of the "patch/*" branches
3 # into one or more diffs in the "patches" directory. Pass the option
4 # --gen if you want generated files in the diffs. Pass the name of
5 # one or more diffs if you want to just update a subset of all the
11 my $patches_dir = 'patches';
12 my $tmp_dir = "patches.$$";
14 &Getopt::Long::Configure('bundling');
15 &usage if !&GetOptions(
16 'skip-check' => \( my $skip_branch_check ),
17 'gen:s' => \( my $incl_generated_files ),
18 'help|h' => \( my $help_opt ),
22 if (defined $incl_generated_files) {
23 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
24 $incl_generated_files = 1;
27 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
28 die "No '.git' directory present in the current dir.\n" unless -d '.git';
30 open(IN, '-|', 'git status') or die $!;
31 my $status = join('', <IN>);
33 unless ($skip_branch_check) {
34 die "The checkout is not clean:\n", $status unless $status =~ /\nnothing to commit \(working directory clean\)/;
36 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
39 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
45 @extra_files = split(' ', $_);
51 if ($incl_generated_files) {
52 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
53 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
54 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/master/" and exit 1;
56 my $last_touch = time;
58 my(%patches, %local_patch);
60 # Start by finding all patches so that we can load all possible parents.
61 open(PIPE, '-|', 'git', 'branch', '-a') or die $!;
63 if (m# origin/patch/(.*)#) {
65 } elsif (m# patch/(.*)#) {
66 $patches{$1} = $local_patch{$1} = 1;
71 my @patches = sort keys %patches;
73 my(%parent, %description);
74 foreach my $patch (@patches) {
75 my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch";
77 open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$branch", '--', "PATCH.$patch") or die $!;
82 next unless s/^[ +]//;
83 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
88 $description{$patch} = $desc;
92 # Limit the list of patches to actually process based on @ARGV.
95 s{^(patches|patch|origin/patch)/} {};
102 foreach my $patch (@patches) {
103 next if $completed{$patch}++;
104 update_patch($patch);
107 if ($incl_generated_files) {
108 system "rm -rf $tmp_dir";
111 sleep 1 if $last_touch == time;
112 system "git checkout $starting_branch" and exit 1;
121 my $parent = $parent{$patch};
122 if (defined $parent) {
123 unless ($completed{$parent}++) {
124 update_patch($parent);
126 $parent = "patch/$parent";
131 print "======== $patch ========\n";
133 sleep 1 if $incl_generated_files && $last_touch == time;
134 if ($local_patch{$patch}) {
135 system "git checkout patch/$patch" and exit 1;
137 system "git checkout --track -b patch/$patch origin/patch/$patch" and exit 1;
140 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
141 print OUT $description{$patch}, "\n";
143 if (system("git merge $parent") != 0) {
144 print qq|"git merge $parent" incomplete -- please fix.\n|;
145 $ENV{PS1} = "[$parent] patch/$patch: ";
146 system $ENV{SHELL} and exit 1;
149 if ($incl_generated_files) {
150 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
154 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
155 DIFF: while (<PIPE>) {
156 while (m{^diff --git a/PATCH}) {
158 last if m{^diff --git a/};
160 last DIFF if !defined $_;
167 if ($incl_generated_files) {
169 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent", "$tmp_dir/$patch") or die $!;
171 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
172 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
173 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
187 Usage: patch-update [OPTIONS]
189 --gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
190 --skip-check Skip the check that ensures starting with a clean branch.