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; | |
9 | ||
4da9fcd4 WD |
10 | my $patches_dir = 'patches'; |
11 | my $incl_generated_files; | |
12 | ||
13 | if (@ARGV && $ARGV[0] =~ /^--gen(?:=(\S+))?$/) { | |
14 | $patches_dir = $1 if defined $1; | |
15 | $incl_generated_files = 1; | |
16 | shift; | |
17 | } | |
18 | ||
19 | die "No '$patches_dir' directory was found.\n" unless -d $patches_dir; | |
d26c7dfd WD |
20 | die "No '.git' directory present in the current dir.\n" unless -d '.git'; |
21 | ||
67b9b26f WD |
22 | my @extra_files; |
23 | open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n"; | |
24 | while (<IN>) { | |
25 | if (s/^GENFILES=//) { | |
26 | while (s/\\$//) { | |
27 | $_ .= <IN>; | |
28 | } | |
29 | @extra_files = split(' ', $_); | |
30 | last; | |
31 | } | |
32 | } | |
d26c7dfd WD |
33 | close IN; |
34 | ||
6a2456c5 | 35 | system "git checkout master" and exit 1; |
d26c7dfd | 36 | if ($incl_generated_files) { |
18fa9129 WD |
37 | die "'a' must not exist in the current directory.\n" if -e 'a'; |
38 | die "'b' must not exist in the current directory.\n" if -e 'b'; | |
4da9fcd4 | 39 | system "./config.status Makefile && make gen && rsync -a @extra_files a/" and exit 1; |
d26c7dfd | 40 | } |
49ebb358 | 41 | my $last_touch = time; |
d26c7dfd WD |
42 | |
43 | my(@patches, %local_patch); | |
44 | if (@ARGV) { | |
45 | foreach (@ARGV) { | |
46 | s{^(patches|patch|origin/patch)/} {}; | |
47 | s{\.diff$} {}; | |
48 | push(@patches, $_); | |
49 | } | |
6a2456c5 | 50 | open(PIPE, '-|', 'git', 'branch', '-l') or die $!; |
d26c7dfd | 51 | } else { |
6a2456c5 | 52 | open(PIPE, '-|', 'git', 'branch', '-a') or die $!; |
d26c7dfd WD |
53 | } |
54 | while (<PIPE>) { | |
55 | if (m# origin/patch/(.*)#) { | |
56 | push(@patches, $1); | |
57 | } elsif (m# patch/(.*)#) { | |
58 | $local_patch{$1} = 1; | |
59 | } | |
60 | } | |
61 | close PIPE; | |
62 | ||
97f04215 | 63 | my(%parent, %description); |
d26c7dfd | 64 | foreach my $patch (@patches) { |
97f04215 | 65 | my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch"; |
97f04215 | 66 | my $desc = ''; |
6a2456c5 | 67 | open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$branch", '--', "PATCH.$patch") or die $!; |
97f04215 | 68 | while (<PIPE>) { |
5288be3a WD |
69 | last if /^@@ /; |
70 | } | |
71 | while (<PIPE>) { | |
6a2456c5 | 72 | next unless s/^[ +]//; |
97f04215 WD |
73 | if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) { |
74 | $parent{$patch} = $1; | |
75 | } | |
76 | $desc .= $_; | |
77 | } | |
78 | $description{$patch} = $desc; | |
79 | } | |
80 | ||
81 | my %completed; | |
82 | foreach my $patch (@patches) { | |
83 | next if $completed{$patch}++; | |
84 | update_patch($patch); | |
85 | } | |
86 | ||
87 | if ($incl_generated_files) { | |
88 | system "rm -rf a b"; | |
89 | } | |
90 | ||
aa6865d7 | 91 | sleep 1 if $last_touch == time; |
6a2456c5 | 92 | system "git checkout master"; |
aa6865d7 | 93 | |
97f04215 WD |
94 | exit; |
95 | ||
96 | ||
97 | sub update_patch | |
98 | { | |
99 | my($patch) = @_; | |
100 | ||
101 | my $parent = $parent{$patch}; | |
102 | if (defined $parent) { | |
103 | unless ($completed{$parent}++) { | |
104 | update_patch($parent); | |
105 | } | |
106 | $parent = "patch/$parent"; | |
107 | } else { | |
108 | $parent = 'master'; | |
109 | } | |
110 | ||
d26c7dfd | 111 | print "======== $patch ========\n"; |
97f04215 | 112 | |
18fa9129 | 113 | sleep 1 if $incl_generated_files && $last_touch == time; |
d26c7dfd | 114 | if ($local_patch{$patch}) { |
6a2456c5 | 115 | system "git checkout patch/$patch" and exit 1; |
d26c7dfd | 116 | } else { |
6a2456c5 | 117 | system "git checkout --track -b patch/$patch origin/patch/$patch" and exit 1; |
d26c7dfd | 118 | } |
18fa9129 | 119 | |
4da9fcd4 | 120 | open(OUT, '>', "$patches_dir/$patch.diff") or die $!; |
97f04215 | 121 | print OUT $description{$patch}, "\n"; |
d26c7dfd | 122 | |
6a2456c5 WD |
123 | if (system("git rebase -m $parent") != 0) { |
124 | print qq|"git rebase -m $parent" incomplete -- please fix.\n|; | |
8d321144 | 125 | $ENV{PS1} = "[$parent] patch/$patch: "; |
18fa9129 | 126 | system $ENV{SHELL} and exit 1; |
8d321144 | 127 | } |
d26c7dfd | 128 | |
49ebb358 | 129 | if ($incl_generated_files) { |
4da9fcd4 | 130 | system "./config.status Makefile && make gen && rsync -a @extra_files b/" and exit 1; |
d26c7dfd | 131 | } |
49ebb358 WD |
132 | $last_touch = time; |
133 | ||
6a2456c5 | 134 | open(PIPE, '-|', 'git', 'diff', $parent) or die $!; |
49ebb358 WD |
135 | DIFF: while (<PIPE>) { |
136 | while (m{^diff --git a/PATCH}) { | |
137 | while (<PIPE>) { | |
138 | last if m{^diff --git a/}; | |
139 | } | |
140 | last DIFF if !defined $_; | |
141 | } | |
6a2456c5 | 142 | next if /^index /; |
49ebb358 | 143 | print OUT $_; |
d26c7dfd | 144 | } |
d26c7dfd WD |
145 | close PIPE; |
146 | ||
147 | if ($incl_generated_files) { | |
d26c7dfd WD |
148 | open(PIPE, '-|', 'diff', '-up', 'a', 'b') or die $!; |
149 | while (<PIPE>) { | |
150 | s/^((?:---|\+\+\+) [^\t]+)\t.*/$1/; | |
151 | print OUT $_; | |
152 | } | |
153 | close PIPE; | |
154 | } | |
155 | ||
156 | close OUT; | |
157 | } |