Fixed finding of parent's description when @ARGV doesn't mention it.
[rsync/rsync.git] / support / 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     'skip-check' => \( my $skip_branch_check ),
17     'gen:s' => \( my $incl_generated_files ),
18     'help|h' => \( my $help_opt ),
19 );
20 &usage if $help_opt;
21
22 if (defined $incl_generated_files) {
23     $patches_dir = $incl_generated_files if $incl_generated_files ne '';
24     $incl_generated_files = 1;
25 }
26
27 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
28 die "No '.git' directory present in the current dir.\n" unless -d '.git';
29
30 unless ($skip_branch_check) {
31     open(IN, '-|', 'git status') or die $!;
32     my $status = join('', <IN>);
33     close IN;
34     die "The checkout is not clean:\n", $status unless $status =~ /\nnothing to commit \(working directory clean\)/;
35     die "The checkout is not on the master branch.\n" unless $status =~ /^# On branch master\n/;
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 my $last_touch = time;
57
58 my(@patches, %local_patch);
59
60 # Start by finding all patches so that we can load all possible parents.
61 open(PIPE, '-|', 'git', 'branch', '-a') or die $!;
62 while (<PIPE>) {
63     if (m# origin/patch/(.*)#) {
64         push(@patches, $1);
65     } elsif (m# patch/(.*)#) {
66         $local_patch{$1} = 1;
67     }
68 }
69 close PIPE;
70
71 my(%parent, %description);
72 foreach my $patch (@patches) {
73     my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch";
74     my $desc = '';
75     open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$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             $parent{$patch} = $1;
83         }
84         $desc .= $_;
85     }
86     $description{$patch} = $desc;
87 }
88
89 if (@ARGV) {
90     # Limit the list of patches to actually process based on @ARGV.
91     @patches = ( );
92     foreach (@ARGV) {
93         s{^(patches|patch|origin/patch)/} {};
94         s{\.diff$} {};
95         push(@patches, $_);
96     }
97 }
98
99 my %completed;
100 foreach my $patch (@patches) {
101     next if $completed{$patch}++;
102     update_patch($patch);
103 }
104
105 if ($incl_generated_files) {
106     system "rm -rf $tmp_dir";
107 }
108
109 sleep 1 if $last_touch == time;
110 system "git checkout master" and exit 1;
111
112 exit;
113
114
115 sub update_patch
116 {
117     my($patch) = @_;
118
119     my $parent = $parent{$patch};
120     if (defined $parent) {
121         unless ($completed{$parent}++) {
122             update_patch($parent);
123         }
124         $parent = "patch/$parent";
125     } else {
126         $parent = 'master';
127     }
128
129     print "======== $patch ========\n";
130
131     sleep 1 if $incl_generated_files && $last_touch == time;
132     if ($local_patch{$patch}) {
133         system "git checkout patch/$patch" and exit 1;
134     } else {
135         system "git checkout --track -b patch/$patch origin/patch/$patch" and exit 1;
136     }
137
138     open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
139     print OUT $description{$patch}, "\n";
140
141     if (system("git rebase -m $parent") != 0) {
142         print qq|"git rebase -m $parent" incomplete -- please fix.\n|;
143         $ENV{PS1} = "[$parent] patch/$patch: ";
144         system $ENV{SHELL} and exit 1;
145     }
146
147     if ($incl_generated_files) {
148         system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
149     }
150     $last_touch = time;
151
152     open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
153     DIFF: while (<PIPE>) {
154         while (m{^diff --git a/PATCH}) {
155             while (<PIPE>) {
156                 last if m{^diff --git a/};
157             }
158             last DIFF if !defined $_;
159         }
160         next if /^index /;
161         print OUT $_;
162     }
163     close PIPE;
164
165     if ($incl_generated_files) {
166         $parent =~ s#.*/##;
167         open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent", "$tmp_dir/$patch") or die $!;
168         while (<PIPE>) {
169             s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
170             s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
171             s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
172             print OUT $_;
173         }
174         close PIPE;
175     }
176
177     close OUT;
178 }
179
180 exit;
181
182 sub usage
183 {
184     die <<EOT;
185 Usage: patch-update [OPTIONS]
186
187 --gen[=DIR]   Include generated files.  Optional dest DIR overrides "patches".
188 --skip-check  Skip the check that ensures starting with a clean master branch.
189 EOT
190 }