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