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