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