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