Enhanced the release scripts to be able to handle a branch release.
[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(
56fc9f70 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
56fc9f70 57my %patches;
970ce063
WD
58
59# Start by finding all patches so that we can load all possible parents.
56fc9f70 60open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
d26c7dfd 61while (<PIPE>) {
56fc9f70 62 if (m# patch/(.*)#) {
a5e0bf35 63 $patches{$1} = 1;
d26c7dfd
WD
64 }
65}
66close PIPE;
67
a5e0bf35
WD
68my @patches = sort keys %patches;
69
97f04215 70my(%parent, %description);
d26c7dfd 71foreach my $patch (@patches) {
56fc9f70 72 my $branch = "patch/$patch";
97f04215 73 my $desc = '';
56fc9f70 74 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
97f04215 75 while (<PIPE>) {
5288be3a
WD
76 last if /^@@ /;
77 }
78 while (<PIPE>) {
6a2456c5 79 next unless s/^[ +]//;
97f04215 80 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
56fc9f70
WD
81 my $parent = $parent{$patch} = $1;
82 if (!$patches{$parent}) {
83 die "Parent of $patch is not a local branch: $parent\n";
84 }
97f04215
WD
85 }
86 $desc .= $_;
87 }
56fc9f70 88 close PIPE;
97f04215
WD
89 $description{$patch} = $desc;
90}
91
970ce063
WD
92if (@ARGV) {
93 # Limit the list of patches to actually process based on @ARGV.
94 @patches = ( );
95 foreach (@ARGV) {
56fc9f70 96 s{^patch(es)?/} {};
970ce063 97 s{\.diff$} {};
56fc9f70
WD
98 if (!$patches{$_}) {
99 die "Local branch not available for patch: $_\n";
100 }
970ce063
WD
101 push(@patches, $_);
102 }
103}
104
97f04215
WD
105my %completed;
106foreach my $patch (@patches) {
107 next if $completed{$patch}++;
c202b4fa 108 last unless update_patch($patch);
97f04215
WD
109}
110
111if ($incl_generated_files) {
f2b7b64d 112 system "rm -rf $tmp_dir";
97f04215
WD
113}
114
8a5ae84e 115sleep 1 while $last_touch >= time;
16e24c20 116system "git checkout $starting_branch" and exit 1;
aa6865d7 117
97f04215
WD
118exit;
119
120
121sub update_patch
122{
123 my($patch) = @_;
124
125 my $parent = $parent{$patch};
126 if (defined $parent) {
127 unless ($completed{$parent}++) {
128 update_patch($parent);
129 }
130 $parent = "patch/$parent";
131 } else {
56fc9f70 132 $parent = $master_branch;
97f04215
WD
133 }
134
d26c7dfd 135 print "======== $patch ========\n";
97f04215 136
8a5ae84e 137 sleep 1 while $incl_generated_files && $last_touch >= time;
56fc9f70 138 system "git checkout patch/$patch" and return 0;
18fa9129 139
c202b4fa
WD
140 my $ok = system("git merge $parent") == 0;
141 if (!$ok || $launch_shell) {
142 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
8d321144 143 $ENV{PS1} = "[$parent] patch/$patch: ";
c202b4fa
WD
144 while (1) {
145 if (system($ENV{SHELL}) != 0) {
146 print "Abort? [n/y] ";
147 $_ = <STDIN>;
148 next unless /^y/i;
149 return 0;
150 }
151 ($status, $is_clean) = &check_git_status;
152 last if $is_clean;
153 print $status;
154 }
8d321144 155 }
d26c7dfd 156
c202b4fa
WD
157 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
158 print OUT $description{$patch}, "\n";
159
49ebb358 160 if ($incl_generated_files) {
8a5ae84e 161 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
d26c7dfd 162 }
49ebb358
WD
163 $last_touch = time;
164
6a2456c5 165 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
49ebb358
WD
166 DIFF: while (<PIPE>) {
167 while (m{^diff --git a/PATCH}) {
168 while (<PIPE>) {
169 last if m{^diff --git a/};
170 }
171 last DIFF if !defined $_;
172 }
6a2456c5 173 next if /^index /;
49ebb358 174 print OUT $_;
d26c7dfd 175 }
d26c7dfd
WD
176 close PIPE;
177
178 if ($incl_generated_files) {
56fc9f70
WD
179 my $parent_dir;
180 if ($parent eq $master_branch) {
181 $parent_dir = 'master';
182 } else {
183 ($parent_dir) = $parent =~ m{([^/]+)$};
184 }
185 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
d26c7dfd 186 while (<PIPE>) {
f2b7b64d
WD
187 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
188 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
189 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
d26c7dfd
WD
190 print OUT $_;
191 }
192 close PIPE;
193 }
194
195 close OUT;
c202b4fa
WD
196
197 1;
d26c7dfd 198}
f2b7b64d
WD
199
200exit;
201
c202b4fa
WD
202sub check_git_status
203{
204 open(IN, '-|', 'git status') or die $!;
205 my $status = join('', <IN>);
206 close IN;
207 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
208 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
209 ($status, $is_clean, $starting_branch);
210}
211
f2b7b64d
WD
212sub usage
213{
214 die <<EOT;
215Usage: patch-update [OPTIONS]
216
dd1f0da8 217--gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
16e24c20 218--skip-check Skip the check that ensures starting with a clean branch.
f2b7b64d
WD
219EOT
220}