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(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
46 @extra_files = split(' ', $_);
52 if ($incl_generated_files) {
53 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
54 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
55 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1;
57 our $last_touch = time;
61 # Start by finding all patches so that we can load all possible parents.
62 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
70 my @patches = sort keys %patches;
72 my(%parent, %description);
73 foreach my $patch (@patches) {
74 my $branch = "patch/$patch";
76 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
81 next unless s/^[ +]//;
82 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
83 my $parent = $parent{$patch} = $1;
84 if (!$patches{$parent}) {
85 die "Parent of $patch is not a local branch: $parent\n";
91 $description{$patch} = $desc;
95 # Limit the list of patches to actually process based on @ARGV.
101 die "Local branch not available for patch: $_\n";
108 foreach my $patch (@patches) {
109 next if $completed{$patch}++;
110 last unless update_patch($patch);
113 if ($incl_generated_files) {
114 system "rm -rf $tmp_dir";
117 sleep 1 while $last_touch >= time;
118 system "git checkout $starting_branch" and exit 1;
127 my $parent = $parent{$patch};
128 if (defined $parent) {
129 unless ($completed{$parent}++) {
130 update_patch($parent);
132 $parent = "patch/$parent";
134 $parent = $master_branch;
137 print "======== $patch ========\n";
139 sleep 1 while $incl_generated_files && $last_touch >= time;
140 system "git checkout patch/$patch" and return 0;
142 my $ok = system("git merge $parent") == 0;
143 if (!$ok || $launch_shell) {
144 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
145 $ENV{PS1} = "[$parent] patch/$patch: ";
147 if (system($ENV{SHELL}) != 0) {
148 print "Abort? [n/y] ";
153 ($status, $is_clean) = &check_git_status;
159 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
160 print OUT $description{$patch}, "\n";
162 if ($incl_generated_files) {
163 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
167 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
168 DIFF: while (<PIPE>) {
169 while (m{^diff --git a/PATCH}) {
171 last if m{^diff --git a/};
173 last DIFF if !defined $_;
180 if ($incl_generated_files) {
182 if ($parent eq $master_branch) {
183 $parent_dir = 'master';
185 ($parent_dir) = $parent =~ m{([^/]+)$};
187 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
189 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
190 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
191 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
206 open(IN, '-|', 'git status') or die $!;
207 my $status = join('', <IN>);
209 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
210 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
211 ($status, $is_clean, $starting_branch);
217 Usage: patch-update [OPTIONS]
219 --gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
220 --skip-check Skip the check that ensures starting with a clean branch.