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