241cd16a6a67103f4ac5a22a4ede2ac58802565b
[rsync/rsync.git] / packaging / patch-update
1 #!/usr/bin/perl
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
8 use strict;
9 use warnings;
10 use Getopt::Long;
11
12 my $patches_dir = 'patches';
13 my $tmp_dir = "patches.$$";
14 my $make_gen_cmd = 'make -f prepare-source.mak conf && ./config.status && make gen';
15
16 &Getopt::Long::Configure('bundling');
17 &usage if !&GetOptions(
18     'branch|b=s' => \( my $master_branch = 'master' ),
19     'skip-check' => \( my $skip_branch_check ),
20     'shell|s' => \( my $launch_shell ),
21     'gen:s' => \( my $incl_generated_files ),
22     'help|h' => \( my $help_opt ),
23 );
24 &usage if $help_opt;
25
26 if (defined $incl_generated_files) {
27     $patches_dir = $incl_generated_files if $incl_generated_files ne '';
28     $incl_generated_files = 1;
29 }
30
31 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
32 die "No '.git' directory present in the current dir.\n" unless -d '.git';
33
34 my($status, $is_clean, $starting_branch) = &check_git_status;
35 if (!$skip_branch_check && !$is_clean) {
36     die "The checkout is not clean:\n", $status;
37 }
38
39 my $master_commit;
40 open PIPE, '-|', "git log -1 --no-color $master_branch" or die $!;
41 while (<PIPE>) {
42     if (/^commit (\S+)/) {
43         $master_commit = $1;
44         last;
45     }
46 }
47 close PIPE;
48 die "Unable to determine commit hash for master branch: $master_branch\n" unless defined $master_commit;
49
50 my @extra_files;
51 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
52 while (<IN>) {
53     if (s/^GENFILES=//) {
54         while (s/\\$//) {
55             $_ .= <IN>;
56         }
57         @extra_files = split(' ', $_);
58         last;
59     }
60 }
61 close IN;
62
63 if ($incl_generated_files) {
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";
66     system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1;
67 }
68 our $last_touch = time;
69
70 my %patches;
71
72 # Start by finding all patches so that we can load all possible parents.
73 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
74 while (<PIPE>) {
75     if (m# patch/(.*)#) {
76         $patches{$1} = 1;
77     }
78 }
79 close PIPE;
80
81 my @patches = sort keys %patches;
82
83 my(%parent, %description);
84 foreach my $patch (@patches) {
85     my $branch = "patch/$patch";
86     my $desc = '';
87     open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
88     while (<PIPE>) {
89         last if /^@@ /;
90     }
91     while (<PIPE>) {
92         next unless s/^[ +]//;
93         if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
94             my $parent = $parent{$patch} = $1;
95             if (!$patches{$parent}) {
96                 die "Parent of $patch is not a local branch: $parent\n";
97             }
98         }
99         $desc .= $_;
100     }
101     close PIPE;
102     $description{$patch} = $desc;
103 }
104
105 if (@ARGV) {
106     # Limit the list of patches to actually process based on @ARGV.
107     @patches = ( );
108     foreach (@ARGV) {
109         s{^patch(es)?/} {};
110         s{\.diff$} {};
111         if (!$patches{$_}) {
112             die "Local branch not available for patch: $_\n";
113         }
114         push(@patches, $_);
115     }
116 }
117
118 my %completed;
119 foreach my $patch (@patches) {
120     next if $completed{$patch}++;
121     last unless update_patch($patch);
122 }
123
124 if ($incl_generated_files) {
125     system "rm -rf $tmp_dir";
126 }
127
128 sleep 1 while $last_touch >= time;
129 system "git checkout $starting_branch" and exit 1;
130
131 exit;
132
133
134 sub update_patch
135 {
136     my($patch) = @_;
137
138     my $parent = $parent{$patch};
139     my $based_on;
140     if (defined $parent) {
141         unless ($completed{$parent}++) {
142             update_patch($parent);
143         }
144         $based_on = $parent = "patch/$parent";
145     } else {
146         $parent = $master_branch;
147         $based_on = $master_commit;
148     }
149
150     print "======== $patch ========\n";
151
152     sleep 1 while $incl_generated_files && $last_touch >= time;
153     system "git checkout patch/$patch" and return 0;
154
155     my $ok = system("git merge $based_on") == 0;
156     if (!$ok || $launch_shell) {
157         print qq|"git merge $based_on" incomplete -- please fix.\n| if !$ok;
158         $ENV{PS1} = "[$parent] patch/$patch: ";
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         }
170     }
171
172     open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
173     print OUT $description{$patch}, "\nbased-on: $based_on\n";
174
175     if ($incl_generated_files) {
176         system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
177     }
178     $last_touch = time;
179
180     open(PIPE, '-|', 'git', 'diff', $based_on) or die $!;
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         }
188         next if /^index /;
189         print OUT $_;
190     }
191     close PIPE;
192
193     if ($incl_generated_files) {
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 $!;
201         while (<PIPE>) {
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;
205             print OUT $_;
206         }
207         close PIPE;
208     }
209
210     close OUT;
211
212     1;
213 }
214
215 exit;
216
217 sub 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
227 sub usage
228 {
229     die <<EOT;
230 Usage: patch-update [OPTIONS]
231
232 --gen[=DIR]   Include generated files.  Optional dest DIR overrides "patches".
233 --skip-check  Skip the check that ensures starting with a clean branch.
234 EOT
235 }