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