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
11 my $patches_dir = 'patches';
12 my $tmp_dir = "patches.$$";
13 my $make_gen_cmd = 'make -f prepare-source.mak conf && ./config.status && make gen';
15 &Getopt::Long::Configure('bundling');
16 &usage if !&GetOptions(
17 'branch|b=s' => \( my $master_branch = 'master' ),
18 'skip-check' => \( my $skip_branch_check ),
19 'shell|s' => \( my $launch_shell ),
20 'gen:s' => \( my $incl_generated_files ),
21 'help|h' => \( my $help_opt ),
25 if (defined $incl_generated_files) {
26 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
27 $incl_generated_files = 1;
30 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
31 die "No '.git' directory present in the current dir.\n" unless -d '.git';
33 my($status, $is_clean, $starting_branch) = &check_git_status;
34 if (!$skip_branch_check && !$is_clean) {
35 die "The checkout is not clean:\n", $status;
39 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
45 @extra_files = split(' ', $_);
51 if ($incl_generated_files) {
52 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
53 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
54 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1;
56 our $last_touch = time;
60 # Start by finding all patches so that we can load all possible parents.
61 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
69 my @patches = sort keys %patches;
71 my(%parent, %description);
72 foreach my $patch (@patches) {
73 my $branch = "patch/$patch";
75 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
80 next unless s/^[ +]//;
81 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
82 my $parent = $parent{$patch} = $1;
83 if (!$patches{$parent}) {
84 die "Parent of $patch is not a local branch: $parent\n";
90 $description{$patch} = $desc;
94 # Limit the list of patches to actually process based on @ARGV.
100 die "Local branch not available for patch: $_\n";
107 foreach my $patch (@patches) {
108 next if $completed{$patch}++;
109 last unless update_patch($patch);
112 if ($incl_generated_files) {
113 system "rm -rf $tmp_dir";
116 sleep 1 while $last_touch >= time;
117 system "git checkout $starting_branch" and exit 1;
126 my $parent = $parent{$patch};
127 if (defined $parent) {
128 unless ($completed{$parent}++) {
129 update_patch($parent);
131 $parent = "patch/$parent";
133 $parent = $master_branch;
136 print "======== $patch ========\n";
138 sleep 1 while $incl_generated_files && $last_touch >= time;
139 system "git checkout patch/$patch" and return 0;
141 my $ok = system("git merge $parent") == 0;
142 if (!$ok || $launch_shell) {
143 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
144 $ENV{PS1} = "[$parent] patch/$patch: ";
146 if (system($ENV{SHELL}) != 0) {
147 print "Abort? [n/y] ";
152 ($status, $is_clean) = &check_git_status;
158 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
159 print OUT $description{$patch}, "\n";
161 if ($incl_generated_files) {
162 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
166 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
167 DIFF: while (<PIPE>) {
168 while (m{^diff --git a/PATCH}) {
170 last if m{^diff --git a/};
172 last DIFF if !defined $_;
179 if ($incl_generated_files) {
181 if ($parent eq $master_branch) {
182 $parent_dir = 'master';
184 ($parent_dir) = $parent =~ m{([^/]+)$};
186 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
188 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
189 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
190 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
205 open(IN, '-|', 'git status') or die $!;
206 my $status = join('', <IN>);
208 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
209 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
210 ($status, $is_clean, $starting_branch);
216 Usage: patch-update [OPTIONS]
218 --gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
219 --skip-check Skip the check that ensures starting with a clean branch.