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 | ||
f2b7b64d WD |
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 | ||
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 WD |
57 | |
58 | my(@patches, %local_patch); | |
59 | if (@ARGV) { | |
60 | foreach (@ARGV) { | |
61 | s{^(patches|patch|origin/patch)/} {}; | |
62 | s{\.diff$} {}; | |
63 | push(@patches, $_); | |
64 | } | |
6a2456c5 | 65 | open(PIPE, '-|', 'git', 'branch', '-l') or die $!; |
d26c7dfd | 66 | } else { |
6a2456c5 | 67 | open(PIPE, '-|', 'git', 'branch', '-a') or die $!; |
d26c7dfd WD |
68 | } |
69 | while (<PIPE>) { | |
70 | if (m# origin/patch/(.*)#) { | |
71 | push(@patches, $1); | |
72 | } elsif (m# patch/(.*)#) { | |
73 | $local_patch{$1} = 1; | |
74 | } | |
75 | } | |
76 | close PIPE; | |
77 | ||
97f04215 | 78 | my(%parent, %description); |
d26c7dfd | 79 | foreach my $patch (@patches) { |
97f04215 | 80 | my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch"; |
97f04215 | 81 | my $desc = ''; |
6a2456c5 | 82 | open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$branch", '--', "PATCH.$patch") or die $!; |
97f04215 | 83 | while (<PIPE>) { |
5288be3a WD |
84 | last if /^@@ /; |
85 | } | |
86 | while (<PIPE>) { | |
6a2456c5 | 87 | next unless s/^[ +]//; |
97f04215 WD |
88 | if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) { |
89 | $parent{$patch} = $1; | |
90 | } | |
91 | $desc .= $_; | |
92 | } | |
93 | $description{$patch} = $desc; | |
94 | } | |
95 | ||
96 | my %completed; | |
97 | foreach my $patch (@patches) { | |
98 | next if $completed{$patch}++; | |
99 | update_patch($patch); | |
100 | } | |
101 | ||
102 | if ($incl_generated_files) { | |
f2b7b64d | 103 | system "rm -rf $tmp_dir"; |
97f04215 WD |
104 | } |
105 | ||
aa6865d7 | 106 | sleep 1 if $last_touch == time; |
f2b7b64d | 107 | system "git checkout master" and die $!; |
aa6865d7 | 108 | |
97f04215 WD |
109 | exit; |
110 | ||
111 | ||
112 | sub update_patch | |
113 | { | |
114 | my($patch) = @_; | |
115 | ||
116 | my $parent = $parent{$patch}; | |
117 | if (defined $parent) { | |
118 | unless ($completed{$parent}++) { | |
119 | update_patch($parent); | |
120 | } | |
121 | $parent = "patch/$parent"; | |
122 | } else { | |
123 | $parent = 'master'; | |
124 | } | |
125 | ||
d26c7dfd | 126 | print "======== $patch ========\n"; |
97f04215 | 127 | |
18fa9129 | 128 | sleep 1 if $incl_generated_files && $last_touch == time; |
d26c7dfd | 129 | if ($local_patch{$patch}) { |
6a2456c5 | 130 | system "git checkout patch/$patch" and exit 1; |
d26c7dfd | 131 | } else { |
6a2456c5 | 132 | system "git checkout --track -b patch/$patch origin/patch/$patch" and exit 1; |
d26c7dfd | 133 | } |
18fa9129 | 134 | |
4da9fcd4 | 135 | open(OUT, '>', "$patches_dir/$patch.diff") or die $!; |
97f04215 | 136 | print OUT $description{$patch}, "\n"; |
d26c7dfd | 137 | |
6a2456c5 WD |
138 | if (system("git rebase -m $parent") != 0) { |
139 | print qq|"git rebase -m $parent" incomplete -- please fix.\n|; | |
8d321144 | 140 | $ENV{PS1} = "[$parent] patch/$patch: "; |
18fa9129 | 141 | system $ENV{SHELL} and exit 1; |
8d321144 | 142 | } |
d26c7dfd | 143 | |
49ebb358 | 144 | if ($incl_generated_files) { |
f2b7b64d | 145 | system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1; |
d26c7dfd | 146 | } |
49ebb358 WD |
147 | $last_touch = time; |
148 | ||
6a2456c5 | 149 | open(PIPE, '-|', 'git', 'diff', $parent) or die $!; |
49ebb358 WD |
150 | DIFF: while (<PIPE>) { |
151 | while (m{^diff --git a/PATCH}) { | |
152 | while (<PIPE>) { | |
153 | last if m{^diff --git a/}; | |
154 | } | |
155 | last DIFF if !defined $_; | |
156 | } | |
6a2456c5 | 157 | next if /^index /; |
49ebb358 | 158 | print OUT $_; |
d26c7dfd | 159 | } |
d26c7dfd WD |
160 | close PIPE; |
161 | ||
162 | if ($incl_generated_files) { | |
f2b7b64d WD |
163 | $parent =~ s#.*/##; |
164 | open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent", "$tmp_dir/$patch") or die $!; | |
d26c7dfd | 165 | while (<PIPE>) { |
f2b7b64d WD |
166 | s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o; |
167 | s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o; | |
168 | s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o; | |
d26c7dfd WD |
169 | print OUT $_; |
170 | } | |
171 | close PIPE; | |
172 | } | |
173 | ||
174 | close OUT; | |
175 | } | |
f2b7b64d WD |
176 | |
177 | exit; | |
178 | ||
179 | sub usage | |
180 | { | |
181 | die <<EOT; | |
182 | Usage: patch-update [OPTIONS] | |
183 | ||
184 | --gen[=DIR] Inlcude generated files. | |
185 | --skip-check Skip the master-branch, clean-checkout check. | |
186 | EOT | |
187 | } |