Tweaked rsync.spec to use "rsync" in place of "%{name}" in a few
[rsync.git] / support / patch-update
blob4892d6681e4816924afa44a595bfedb06c49fc3e
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.
8 use strict;
9 use Getopt::Long;
11 my $patches_dir = 'patches';
12 my $tmp_dir = "patches.$$";
14 &Getopt::Long::Configure('bundling');
15 &usage if !&GetOptions(
16 'skip-check' => \( my $skip_branch_check ),
17 'shell|s' => \( my $launch_shell ),
18 'gen:s' => \( my $incl_generated_files ),
19 'help|h' => \( my $help_opt ),
21 &usage if $help_opt;
23 if (defined $incl_generated_files) {
24 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
25 $incl_generated_files = 1;
28 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
29 die "No '.git' directory present in the current dir.\n" unless -d '.git';
31 my($status, $is_clean, $starting_branch) = &check_git_status;
32 if (!$skip_branch_check && !$is_clean) {
33 die "The checkout is not clean:\n", $status;
36 my @extra_files;
37 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
38 while (<IN>) {
39 if (s/^GENFILES=//) {
40 while (s/\\$//) {
41 $_ .= <IN>;
43 @extra_files = split(' ', $_);
44 last;
47 close IN;
49 if ($incl_generated_files) {
50 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
51 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
52 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/master/" and exit 1;
54 my $last_touch = time;
56 my(%patches, %local_patch);
58 # Start by finding all patches so that we can load all possible parents.
59 open(PIPE, '-|', 'git', 'branch', '-a') or die $!;
60 while (<PIPE>) {
61 if (m# origin/patch/(.*)#) {
62 $patches{$1} = 1;
63 } elsif (m# patch/(.*)#) {
64 $patches{$1} = $local_patch{$1} = 1;
67 close PIPE;
69 my @patches = sort keys %patches;
71 my(%parent, %description);
72 foreach my $patch (@patches) {
73 my $branch = ($local_patch{$patch} ? '' : 'origin/') . "patch/$patch";
74 my $desc = '';
75 open(PIPE, '-|', 'git', 'diff', '-U1000', "master...$branch", '--', "PATCH.$patch") or die $!;
76 while (<PIPE>) {
77 last if /^@@ /;
79 while (<PIPE>) {
80 next unless s/^[ +]//;
81 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
82 $parent{$patch} = $1;
84 $desc .= $_;
86 $description{$patch} = $desc;
89 if (@ARGV) {
90 # Limit the list of patches to actually process based on @ARGV.
91 @patches = ( );
92 foreach (@ARGV) {
93 s{^(patches|patch|origin/patch)/} {};
94 s{\.diff$} {};
95 push(@patches, $_);
99 my %completed;
100 foreach my $patch (@patches) {
101 next if $completed{$patch}++;
102 last unless update_patch($patch);
105 if ($incl_generated_files) {
106 system "rm -rf $tmp_dir";
109 sleep 1 if $last_touch == time;
110 system "git checkout $starting_branch" and exit 1;
112 exit;
115 sub update_patch
117 my($patch) = @_;
119 my $parent = $parent{$patch};
120 if (defined $parent) {
121 unless ($completed{$parent}++) {
122 update_patch($parent);
124 $parent = "patch/$parent";
125 } else {
126 $parent = 'master';
129 print "======== $patch ========\n";
131 sleep 1 if $incl_generated_files && $last_touch == time;
132 if ($local_patch{$patch}) {
133 system "git checkout patch/$patch" and return 0;
134 } else {
135 system "git checkout --track -b patch/$patch origin/patch/$patch" and return 0;
138 my $ok = system("git merge $parent") == 0;
139 if (!$ok || $launch_shell) {
140 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
141 $ENV{PS1} = "[$parent] patch/$patch: ";
142 while (1) {
143 if (system($ENV{SHELL}) != 0) {
144 print "Abort? [n/y] ";
145 $_ = <STDIN>;
146 next unless /^y/i;
147 return 0;
149 ($status, $is_clean) = &check_git_status;
150 last if $is_clean;
151 print $status;
155 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
156 print OUT $description{$patch}, "\n";
158 if ($incl_generated_files) {
159 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/";
161 $last_touch = time;
163 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
164 DIFF: while (<PIPE>) {
165 while (m{^diff --git a/PATCH}) {
166 while (<PIPE>) {
167 last if m{^diff --git a/};
169 last DIFF if !defined $_;
171 next if /^index /;
172 print OUT $_;
174 close PIPE;
176 if ($incl_generated_files) {
177 $parent =~ s#.*/##;
178 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent", "$tmp_dir/$patch") or die $!;
179 while (<PIPE>) {
180 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
181 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
182 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
183 print OUT $_;
185 close PIPE;
188 close OUT;
193 exit;
195 sub check_git_status
197 open(IN, '-|', 'git status') or die $!;
198 my $status = join('', <IN>);
199 close IN;
200 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
201 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
202 ($status, $is_clean, $starting_branch);
205 sub usage
207 die <<EOT;
208 Usage: patch-update [OPTIONS]
210 --gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
211 --skip-check Skip the check that ensures starting with a clean branch.