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
12 my $patches_dir = 'patches';
13 my $tmp_dir = "patches.$$";
14 my $make_gen_cmd = 'make -f prepare-source.mak conf && ./config.status && make gen';
16 &Getopt::Long::Configure('bundling');
17 &usage if !&GetOptions(
18 'branch|b=s' => \( my $master_branch = 'master' ),
19 'skip-check' => \( my $skip_branch_check ),
20 'shell|s' => \( my $launch_shell ),
21 'gen:s' => \( my $incl_generated_files ),
22 'help|h' => \( my $help_opt ),
26 if (defined $incl_generated_files) {
27 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
28 $incl_generated_files = 1;
31 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
32 die "No '.git' directory present in the current dir.\n" unless -d '.git';
34 my($status, $is_clean, $starting_branch) = &check_git_status;
35 if (!$skip_branch_check && !$is_clean) {
36 die "The checkout is not clean:\n", $status;
40 open PIPE, '-|', "git log -1 --no-color $master_branch" or die $!;
42 if (/^commit (\S+)/) {
48 die "Unable to determine commit hash for master branch: $master_branch\n" unless defined $master_commit;
51 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
57 @extra_files = split(' ', $_);
63 if ($incl_generated_files) {
64 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
65 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
66 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1;
68 our $last_touch = time;
72 # Start by finding all patches so that we can load all possible parents.
73 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
81 my @patches = sort keys %patches;
83 my(%parent, %description);
84 foreach my $patch (@patches) {
85 my $branch = "patch/$patch";
87 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
92 next unless s/^[ +]//;
93 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
94 my $parent = $parent{$patch} = $1;
95 if (!$patches{$parent}) {
96 die "Parent of $patch is not a local branch: $parent\n";
102 $description{$patch} = $desc;
106 # Limit the list of patches to actually process based on @ARGV.
112 die "Local branch not available for patch: $_\n";
119 foreach my $patch (@patches) {
120 next if $completed{$patch}++;
121 last unless update_patch($patch);
124 if ($incl_generated_files) {
125 system "rm -rf $tmp_dir";
128 sleep 1 while $last_touch >= time;
129 system "git checkout $starting_branch" and exit 1;
138 my $parent = $parent{$patch};
140 if (defined $parent) {
141 unless ($completed{$parent}++) {
142 update_patch($parent);
144 $based_on = $parent = "patch/$parent";
146 $parent = $master_branch;
147 $based_on = $master_commit;
150 print "======== $patch ========\n";
152 sleep 1 while $incl_generated_files && $last_touch >= time;
153 system "git checkout patch/$patch" and return 0;
155 my $ok = system("git merge $based_on") == 0;
156 if (!$ok || $launch_shell) {
157 print qq|"git merge $based_on" incomplete -- please fix.\n| if !$ok;
158 $ENV{PS1} = "[$parent] patch/$patch: ";
160 if (system($ENV{SHELL}) != 0) {
161 print "Abort? [n/y] ";
166 ($status, $is_clean) = &check_git_status;
172 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
173 print OUT $description{$patch}, "\nbased-on: $based_on\n";
175 if ($incl_generated_files) {
176 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
180 open(PIPE, '-|', 'git', 'diff', $based_on) or die $!;
181 DIFF: while (<PIPE>) {
182 while (m{^diff --git a/PATCH}) {
184 last if m{^diff --git a/};
186 last DIFF if !defined $_;
193 if ($incl_generated_files) {
195 if ($parent eq $master_branch) {
196 $parent_dir = 'master';
198 ($parent_dir) = $parent =~ m{([^/]+)$};
200 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
202 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
203 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
204 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
219 open(IN, '-|', 'git status') or die $!;
220 my $status = join('', <IN>);
222 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
223 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
224 ($status, $is_clean, $starting_branch);
230 Usage: patch-update [OPTIONS] [patches/DIFF...]
233 -b, --branch=BRANCH The master branch to merge into the patch/* branches.
234 --gen[=DIR] Include generated files. Optional destination DIR
235 arg overrides the default of using the "patches" dir.
236 --skip-check Skip the check that ensures starting with a clean branch.
237 -s, --shell Launch a shell for every patch/* branch updated, not
238 just when a conflict occurs.
239 -h, --help Output this help message.