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