- Make sure the Makefile is up-to-date before running "make gen".
[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 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;
20 die "No '.git' directory present in the current dir.\n" unless -d '.git';
21
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 }
33 close IN;
34
35 system "git checkout master" and exit 1;
36 if ($incl_generated_files) {
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';
39     system "./config.status Makefile && make gen && rsync -a @extra_files a/" and exit 1;
40 }
41 my $last_touch = time;
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     }
50     open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
51 } else {
52     open(PIPE, '-|', 'git', 'branch', '-a') or die $!;
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
63 my(%parent, %description);
64 foreach my $patch (@patches) {
65     my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch";
66     my $desc = '';
67     open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$branch", '--', "PATCH.$patch") or die $!;
68     while (<PIPE>) {
69         last if /^@@ /;
70     }
71     while (<PIPE>) {
72         next unless s/^[ +]//;
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
91 sleep 1 if $last_touch == time;
92 system "git checkout master";
93
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
111     print "======== $patch ========\n";
112
113     sleep 1 if $incl_generated_files && $last_touch == time;
114     if ($local_patch{$patch}) {
115         system "git checkout patch/$patch" and exit 1;
116     } else {
117         system "git checkout --track -b patch/$patch origin/patch/$patch" and exit 1;
118     }
119
120     open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
121     print OUT $description{$patch}, "\n";
122
123     if (system("git rebase -m $parent") != 0) {
124         print qq|"git rebase -m $parent" incomplete -- please fix.\n|;
125         $ENV{PS1} = "[$parent] patch/$patch: ";
126         system $ENV{SHELL} and exit 1;
127     }
128
129     if ($incl_generated_files) {
130         system "./config.status Makefile && make gen && rsync -a @extra_files b/" and exit 1;
131     }
132     $last_touch = time;
133
134     open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
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         }
142         next if /^index /;
143         print OUT $_;
144     }
145     close PIPE;
146
147     if ($incl_generated_files) {
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 }