Commit | Line | Data |
---|---|---|
01e293f1 | 1 | #!/usr/bin/perl |
d26c7dfd WD |
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; | |
01e293f1 | 9 | use warnings; |
f2b7b64d | 10 | use Getopt::Long; |
d26c7dfd | 11 | |
4da9fcd4 | 12 | my $patches_dir = 'patches'; |
f2b7b64d | 13 | my $tmp_dir = "patches.$$"; |
5250c3bc | 14 | my $make_gen_cmd = 'make -f prepare-source.mak conf && ./config.status && make gen'; |
f2b7b64d WD |
15 | |
16 | &Getopt::Long::Configure('bundling'); | |
17 | &usage if !&GetOptions( | |
56fc9f70 | 18 | 'branch|b=s' => \( my $master_branch = 'master' ), |
f2b7b64d | 19 | 'skip-check' => \( my $skip_branch_check ), |
c202b4fa | 20 | 'shell|s' => \( my $launch_shell ), |
f2b7b64d WD |
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 ''; | |
4da9fcd4 | 28 | $incl_generated_files = 1; |
4da9fcd4 WD |
29 | } |
30 | ||
31 | die "No '$patches_dir' directory was found.\n" unless -d $patches_dir; | |
d26c7dfd WD |
32 | die "No '.git' directory present in the current dir.\n" unless -d '.git'; |
33 | ||
c202b4fa WD |
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; | |
f2b7b64d WD |
37 | } |
38 | ||
67b9b26f WD |
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 | } | |
d26c7dfd WD |
50 | close IN; |
51 | ||
d26c7dfd | 52 | if ($incl_generated_files) { |
f2b7b64d WD |
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"; | |
5250c3bc | 55 | system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1; |
d26c7dfd | 56 | } |
8a5ae84e | 57 | our $last_touch = time; |
d26c7dfd | 58 | |
56fc9f70 | 59 | my %patches; |
970ce063 WD |
60 | |
61 | # Start by finding all patches so that we can load all possible parents. | |
56fc9f70 | 62 | open(PIPE, '-|', 'git', 'branch', '-l') or die $!; |
d26c7dfd | 63 | while (<PIPE>) { |
56fc9f70 | 64 | if (m# patch/(.*)#) { |
a5e0bf35 | 65 | $patches{$1} = 1; |
d26c7dfd WD |
66 | } |
67 | } | |
68 | close PIPE; | |
69 | ||
a5e0bf35 WD |
70 | my @patches = sort keys %patches; |
71 | ||
97f04215 | 72 | my(%parent, %description); |
d26c7dfd | 73 | foreach my $patch (@patches) { |
56fc9f70 | 74 | my $branch = "patch/$patch"; |
97f04215 | 75 | my $desc = ''; |
56fc9f70 | 76 | open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!; |
97f04215 | 77 | while (<PIPE>) { |
5288be3a WD |
78 | last if /^@@ /; |
79 | } | |
80 | while (<PIPE>) { | |
6a2456c5 | 81 | next unless s/^[ +]//; |
97f04215 | 82 | if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) { |
56fc9f70 WD |
83 | my $parent = $parent{$patch} = $1; |
84 | if (!$patches{$parent}) { | |
85 | die "Parent of $patch is not a local branch: $parent\n"; | |
86 | } | |
97f04215 WD |
87 | } |
88 | $desc .= $_; | |
89 | } | |
56fc9f70 | 90 | close PIPE; |
97f04215 WD |
91 | $description{$patch} = $desc; |
92 | } | |
93 | ||
970ce063 WD |
94 | if (@ARGV) { |
95 | # Limit the list of patches to actually process based on @ARGV. | |
96 | @patches = ( ); | |
97 | foreach (@ARGV) { | |
56fc9f70 | 98 | s{^patch(es)?/} {}; |
970ce063 | 99 | s{\.diff$} {}; |
56fc9f70 WD |
100 | if (!$patches{$_}) { |
101 | die "Local branch not available for patch: $_\n"; | |
102 | } | |
970ce063 WD |
103 | push(@patches, $_); |
104 | } | |
105 | } | |
106 | ||
97f04215 WD |
107 | my %completed; |
108 | foreach my $patch (@patches) { | |
109 | next if $completed{$patch}++; | |
c202b4fa | 110 | last unless update_patch($patch); |
97f04215 WD |
111 | } |
112 | ||
113 | if ($incl_generated_files) { | |
f2b7b64d | 114 | system "rm -rf $tmp_dir"; |
97f04215 WD |
115 | } |
116 | ||
8a5ae84e | 117 | sleep 1 while $last_touch >= time; |
16e24c20 | 118 | system "git checkout $starting_branch" and exit 1; |
aa6865d7 | 119 | |
97f04215 WD |
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 { | |
56fc9f70 | 134 | $parent = $master_branch; |
97f04215 WD |
135 | } |
136 | ||
d26c7dfd | 137 | print "======== $patch ========\n"; |
97f04215 | 138 | |
8a5ae84e | 139 | sleep 1 while $incl_generated_files && $last_touch >= time; |
56fc9f70 | 140 | system "git checkout patch/$patch" and return 0; |
18fa9129 | 141 | |
c202b4fa WD |
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; | |
8d321144 | 145 | $ENV{PS1} = "[$parent] patch/$patch: "; |
c202b4fa WD |
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 | } | |
8d321144 | 157 | } |
d26c7dfd | 158 | |
c202b4fa WD |
159 | open(OUT, '>', "$patches_dir/$patch.diff") or die $!; |
160 | print OUT $description{$patch}, "\n"; | |
161 | ||
49ebb358 | 162 | if ($incl_generated_files) { |
5250c3bc | 163 | system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1; |
d26c7dfd | 164 | } |
49ebb358 WD |
165 | $last_touch = time; |
166 | ||
6a2456c5 | 167 | open(PIPE, '-|', 'git', 'diff', $parent) or die $!; |
49ebb358 WD |
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 | } | |
6a2456c5 | 175 | next if /^index /; |
49ebb358 | 176 | print OUT $_; |
d26c7dfd | 177 | } |
d26c7dfd WD |
178 | close PIPE; |
179 | ||
180 | if ($incl_generated_files) { | |
56fc9f70 WD |
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 $!; | |
d26c7dfd | 188 | while (<PIPE>) { |
f2b7b64d WD |
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; | |
d26c7dfd WD |
192 | print OUT $_; |
193 | } | |
194 | close PIPE; | |
195 | } | |
196 | ||
197 | close OUT; | |
c202b4fa WD |
198 | |
199 | 1; | |
d26c7dfd | 200 | } |
f2b7b64d WD |
201 | |
202 | exit; | |
203 | ||
c202b4fa WD |
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 | ||
f2b7b64d WD |
214 | sub usage |
215 | { | |
216 | die <<EOT; | |
217 | Usage: patch-update [OPTIONS] | |
218 | ||
dd1f0da8 | 219 | --gen[=DIR] Include generated files. Optional dest DIR overrides "patches". |
16e24c20 | 220 | --skip-check Skip the check that ensures starting with a clean branch. |
f2b7b64d WD |
221 | EOT |
222 | } |