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