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