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