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