Handle new PATCH-$name files, improved $last_touch code,
[rsync/rsync.git] / support / patch-update
CommitLineData
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
8use strict;
9
10die "No 'patches' directory present in the current dir.\n" unless -d 'patches';
11die "No '.git' directory present in the current dir.\n" unless -d '.git';
12
13open(IN, '<', 'prepare-source.mak') or die "Couldn't open prepare-source.mak: $!\n";
14$_ = join('', <IN>);
15close IN;
16
17my @extra_files = m{\n([^\s:]+):.*\n\t\S}g;
49ebb358 18my $incl_generated_files = shift if @ARGV && $ARGV[0] eq '--gen';
d26c7dfd
WD
19
20system "git-checkout master" and exit 1;
d26c7dfd 21if ($incl_generated_files) {
18fa9129
WD
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';
d26c7dfd
WD
24 system "./prepare-source && rsync -a @extra_files a/" and exit 1;
25}
49ebb358 26my $last_touch = time;
d26c7dfd
WD
27
28my(@patches, %local_patch);
29if (@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}
39while (<PIPE>) {
40 if (m# origin/patch/(.*)#) {
41 push(@patches, $1);
42 } elsif (m# patch/(.*)#) {
43 $local_patch{$1} = 1;
44 }
45}
46close PIPE;
47
48foreach my $patch (@patches) {
49 print "======== $patch ========\n";
18fa9129 50 sleep 1 if $incl_generated_files && $last_touch == time;
d26c7dfd
WD
51 if ($local_patch{$patch}) {
52 system "git-checkout patch/$patch" and exit 1;
53 } else {
54 system "git-checkout --track -b patch/$patch origin/patch/$patch" and exit 1;
55 }
18fa9129 56
d26c7dfd 57 my $parent = 'master';
49ebb358 58 open(IN, '<', "PATCH.$patch") or next;
d26c7dfd
WD
59 open(OUT, '>', "patches/$patch.diff") or die $!;
60
61 while (<IN>) {
49ebb358
WD
62 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
63 $parent = "patch/$1";
d26c7dfd
WD
64 }
65 print OUT $_;
66 }
67 close IN;
68 print OUT "\n";
69
8d321144
WD
70 if (system("git-rebase -m $parent") != 0) {
71 print qq|"git-rebase -m $parent" incomplete -- please fix.\n|;
72 $ENV{PS1} = "[$parent] patch/$patch: ";
18fa9129 73 system $ENV{SHELL} and exit 1;
8d321144 74 }
d26c7dfd 75
49ebb358
WD
76 if ($incl_generated_files) {
77 system "./prepare-source && rsync -a @extra_files b/" and exit 1;
d26c7dfd 78 }
49ebb358
WD
79 $last_touch = time;
80
81 open(PIPE, '-|', 'git-diff', $parent) or die $!;
82 DIFF: while (<PIPE>) {
83 while (m{^diff --git a/PATCH}) {
84 while (<PIPE>) {
85 last if m{^diff --git a/};
86 }
87 last DIFF if !defined $_;
88 }
89 print OUT $_;
d26c7dfd 90 }
d26c7dfd
WD
91 close PIPE;
92
93 if ($incl_generated_files) {
d26c7dfd
WD
94 open(PIPE, '-|', 'diff', '-up', 'a', 'b') or die $!;
95 while (<PIPE>) {
96 s/^((?:---|\+\+\+) [^\t]+)\t.*/$1/;
97 print OUT $_;
98 }
99 close PIPE;
100 }
101
102 close OUT;
103}
104
105if ($incl_generated_files) {
106 system "rm -rf a b";
107}
108
109print "-------- master --------\n";
18fa9129 110sleep 1 if $last_touch == time;
d26c7dfd 111system "git-checkout master && ./prepare-source";