Fixed a potential alignment issue in the IRIX ACL code when allocating
[rsync/rsync.git] / packaging / patch-update
CommitLineData
d26c7dfd
WD
1#!/usr/bin/perl -w
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
6# diffs.
7
8use strict;
f2b7b64d 9use Getopt::Long;
d26c7dfd 10
4da9fcd4 11my $patches_dir = 'patches';
f2b7b64d
WD
12my $tmp_dir = "patches.$$";
13
14&Getopt::Long::Configure('bundling');
15&usage if !&GetOptions(
6fd26629 16 'branch|b=s' => \( my $master_branch = 'master' ),
f2b7b64d 17 'skip-check' => \( my $skip_branch_check ),
c202b4fa 18 'shell|s' => \( my $launch_shell ),
f2b7b64d
WD
19 'gen:s' => \( my $incl_generated_files ),
20 'help|h' => \( my $help_opt ),
21);
22&usage if $help_opt;
23
24if (defined $incl_generated_files) {
25 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
4da9fcd4 26 $incl_generated_files = 1;
4da9fcd4
WD
27}
28
29die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
d26c7dfd
WD
30die "No '.git' directory present in the current dir.\n" unless -d '.git';
31
c202b4fa
WD
32my($status, $is_clean, $starting_branch) = &check_git_status;
33if (!$skip_branch_check && !$is_clean) {
34 die "The checkout is not clean:\n", $status;
f2b7b64d
WD
35}
36
67b9b26f
WD
37my @extra_files;
38open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
39while (<IN>) {
40 if (s/^GENFILES=//) {
41 while (s/\\$//) {
42 $_ .= <IN>;
43 }
44 @extra_files = split(' ', $_);
45 last;
46 }
47}
d26c7dfd
WD
48close IN;
49
d26c7dfd 50if ($incl_generated_files) {
f2b7b64d
WD
51 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
52 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
53 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/master/" and exit 1;
d26c7dfd 54}
8a5ae84e 55our $last_touch = time;
d26c7dfd 56
a5e0bf35 57my(%patches, %local_patch);
970ce063
WD
58
59# Start by finding all patches so that we can load all possible parents.
60open(PIPE, '-|', 'git', 'branch', '-a') or die $!;
d26c7dfd
WD
61while (<PIPE>) {
62 if (m# origin/patch/(.*)#) {
a5e0bf35 63 $patches{$1} = 1;
d26c7dfd 64 } elsif (m# patch/(.*)#) {
a5e0bf35 65 $patches{$1} = $local_patch{$1} = 1;
d26c7dfd
WD
66 }
67}
68close PIPE;
69
a5e0bf35
WD
70my @patches = sort keys %patches;
71
97f04215 72my(%parent, %description);
d26c7dfd 73foreach my $patch (@patches) {
97f04215 74 my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch";
97f04215 75 my $desc = '';
6fd26629 76 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
97f04215 77 while (<PIPE>) {
5288be3a
WD
78 last if /^@@ /;
79 }
80 while (<PIPE>) {
6a2456c5 81 next unless s/^[ +]//;
97f04215
WD
82 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
83 $parent{$patch} = $1;
84 }
85 $desc .= $_;
86 }
87 $description{$patch} = $desc;
88}
89
970ce063
WD
90if (@ARGV) {
91 # Limit the list of patches to actually process based on @ARGV.
92 @patches = ( );
93 foreach (@ARGV) {
94 s{^(patches|patch|origin/patch)/} {};
95 s{\.diff$} {};
96 push(@patches, $_);
97 }
98}
99
97f04215
WD
100my %completed;
101foreach my $patch (@patches) {
102 next if $completed{$patch}++;
c202b4fa 103 last unless update_patch($patch);
97f04215
WD
104}
105
106if ($incl_generated_files) {
f2b7b64d 107 system "rm -rf $tmp_dir";
97f04215
WD
108}
109
8a5ae84e 110sleep 1 while $last_touch >= time;
16e24c20 111system "git checkout $starting_branch" and exit 1;
aa6865d7 112
97f04215
WD
113exit;
114
115
116sub update_patch
117{
118 my($patch) = @_;
119
120 my $parent = $parent{$patch};
121 if (defined $parent) {
122 unless ($completed{$parent}++) {
123 update_patch($parent);
124 }
125 $parent = "patch/$parent";
126 } else {
6fd26629 127 $parent = $master_branch;
97f04215
WD
128 }
129
d26c7dfd 130 print "======== $patch ========\n";
97f04215 131
8a5ae84e 132 sleep 1 while $incl_generated_files && $last_touch >= time;
d26c7dfd 133 if ($local_patch{$patch}) {
c202b4fa 134 system "git checkout patch/$patch" and return 0;
d26c7dfd 135 } else {
c202b4fa 136 system "git checkout --track -b patch/$patch origin/patch/$patch" and return 0;
d26c7dfd 137 }
18fa9129 138
c202b4fa
WD
139 my $ok = system("git merge $parent") == 0;
140 if (!$ok || $launch_shell) {
141 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
8d321144 142 $ENV{PS1} = "[$parent] patch/$patch: ";
c202b4fa
WD
143 while (1) {
144 if (system($ENV{SHELL}) != 0) {
145 print "Abort? [n/y] ";
146 $_ = <STDIN>;
147 next unless /^y/i;
148 return 0;
149 }
150 ($status, $is_clean) = &check_git_status;
151 last if $is_clean;
152 print $status;
153 }
8d321144 154 }
d26c7dfd 155
c202b4fa
WD
156 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
157 print OUT $description{$patch}, "\n";
158
49ebb358 159 if ($incl_generated_files) {
8a5ae84e 160 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
d26c7dfd 161 }
49ebb358
WD
162 $last_touch = time;
163
6a2456c5 164 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
49ebb358
WD
165 DIFF: while (<PIPE>) {
166 while (m{^diff --git a/PATCH}) {
167 while (<PIPE>) {
168 last if m{^diff --git a/};
169 }
170 last DIFF if !defined $_;
171 }
6a2456c5 172 next if /^index /;
49ebb358 173 print OUT $_;
d26c7dfd 174 }
d26c7dfd
WD
175 close PIPE;
176
177 if ($incl_generated_files) {
6fd26629
WD
178 my $parent_dir;
179 if ($parent eq $master_branch) {
180 $parent_dir = 'master';
181 } else {
182 ($parent_dir) = $parent =~ m{([^/]+)$};
183 }
184 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
d26c7dfd 185 while (<PIPE>) {
f2b7b64d
WD
186 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
187 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
188 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
d26c7dfd
WD
189 print OUT $_;
190 }
191 close PIPE;
192 }
193
194 close OUT;
c202b4fa
WD
195
196 1;
d26c7dfd 197}
f2b7b64d
WD
198
199exit;
200
c202b4fa
WD
201sub check_git_status
202{
203 open(IN, '-|', 'git status') or die $!;
204 my $status = join('', <IN>);
205 close IN;
206 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
207 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
208 ($status, $is_clean, $starting_branch);
209}
210
f2b7b64d
WD
211sub usage
212{
213 die <<EOT;
214Usage: patch-update [OPTIONS]
215
dd1f0da8 216--gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
16e24c20 217--skip-check Skip the check that ensures starting with a clean branch.
f2b7b64d
WD
218EOT
219}