Enhanced the release scripts to be able to handle a branch release.
[rsync/rsync.git] / packaging / patch-update
... / ...
CommitLineData
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;
9use Getopt::Long;
10
11my $patches_dir = 'patches';
12my $tmp_dir = "patches.$$";
13
14&Getopt::Long::Configure('bundling');
15&usage if !&GetOptions(
16 'branch|b=s' => \( my $master_branch = 'master' ),
17 'skip-check' => \( my $skip_branch_check ),
18 'shell|s' => \( my $launch_shell ),
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 '';
26 $incl_generated_files = 1;
27}
28
29die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
30die "No '.git' directory present in the current dir.\n" unless -d '.git';
31
32my($status, $is_clean, $starting_branch) = &check_git_status;
33if (!$skip_branch_check && !$is_clean) {
34 die "The checkout is not clean:\n", $status;
35}
36
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}
48close IN;
49
50if ($incl_generated_files) {
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;
54}
55our $last_touch = time;
56
57my %patches;
58
59# Start by finding all patches so that we can load all possible parents.
60open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
61while (<PIPE>) {
62 if (m# patch/(.*)#) {
63 $patches{$1} = 1;
64 }
65}
66close PIPE;
67
68my @patches = sort keys %patches;
69
70my(%parent, %description);
71foreach my $patch (@patches) {
72 my $branch = "patch/$patch";
73 my $desc = '';
74 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
75 while (<PIPE>) {
76 last if /^@@ /;
77 }
78 while (<PIPE>) {
79 next unless s/^[ +]//;
80 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
81 my $parent = $parent{$patch} = $1;
82 if (!$patches{$parent}) {
83 die "Parent of $patch is not a local branch: $parent\n";
84 }
85 }
86 $desc .= $_;
87 }
88 close PIPE;
89 $description{$patch} = $desc;
90}
91
92if (@ARGV) {
93 # Limit the list of patches to actually process based on @ARGV.
94 @patches = ( );
95 foreach (@ARGV) {
96 s{^patch(es)?/} {};
97 s{\.diff$} {};
98 if (!$patches{$_}) {
99 die "Local branch not available for patch: $_\n";
100 }
101 push(@patches, $_);
102 }
103}
104
105my %completed;
106foreach my $patch (@patches) {
107 next if $completed{$patch}++;
108 last unless update_patch($patch);
109}
110
111if ($incl_generated_files) {
112 system "rm -rf $tmp_dir";
113}
114
115sleep 1 while $last_touch >= time;
116system "git checkout $starting_branch" and exit 1;
117
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 {
132 $parent = $master_branch;
133 }
134
135 print "======== $patch ========\n";
136
137 sleep 1 while $incl_generated_files && $last_touch >= time;
138 system "git checkout patch/$patch" and return 0;
139
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;
143 $ENV{PS1} = "[$parent] patch/$patch: ";
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 }
155 }
156
157 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
158 print OUT $description{$patch}, "\n";
159
160 if ($incl_generated_files) {
161 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
162 }
163 $last_touch = time;
164
165 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
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 }
173 next if /^index /;
174 print OUT $_;
175 }
176 close PIPE;
177
178 if ($incl_generated_files) {
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 $!;
186 while (<PIPE>) {
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;
190 print OUT $_;
191 }
192 close PIPE;
193 }
194
195 close OUT;
196
197 1;
198}
199
200exit;
201
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
212sub usage
213{
214 die <<EOT;
215Usage: patch-update [OPTIONS]
216
217--gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
218--skip-check Skip the check that ensures starting with a clean branch.
219EOT
220}