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.$$";
14 &Getopt::Long::Configure('bundling');
15 &usage if !&GetOptions(
16 'branch|b=s' => \( my $master_branch = 'master' ),
17 'skip-check' => \( my $skip_branch_check ),
18 'shell|s' => \( my $launch_shell ),
19 'gen:s' => \( my $incl_generated_files ),
20 'help|h' => \( my $help_opt ),
24 if (defined $incl_generated_files) {
25 $patches_dir = $incl_generated_files if $incl_generated_files ne '';
26 $incl_generated_files = 1;
29 die "No '$patches_dir' directory was found.\n" unless -d $patches_dir;
30 die "No '.git' directory present in the current dir.\n" unless -d '.git';
32 my($status, $is_clean, $starting_branch) = &check_git_status;
33 if (!$skip_branch_check && !$is_clean) {
34 die "The checkout is not clean:\n", $status;
38 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
44 @extra_files = split(' ', $_);
50 if ($incl_generated_files) {
51 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
52 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
53 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/master/" and exit 1;
55 our $last_touch = time;
59 # Start by finding all patches so that we can load all possible parents.
60 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
68 my @patches = sort keys %patches;
70 my(%parent, %description);
71 foreach my $patch (@patches) {
72 my $branch = "patch/$patch";
74 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
79 next unless s/^[ +]//;
80 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
81 my $parent = $parent{$patch} = $1;
82 if (!$patches{$parent}) {
83 die "Parent of $patch is not a local branch: $parent\n";
89 $description{$patch} = $desc;
93 # Limit the list of patches to actually process based on @ARGV.
99 die "Local branch not available for patch: $_\n";
106 foreach my $patch (@patches) {
107 next if $completed{$patch}++;
108 last unless update_patch($patch);
111 if ($incl_generated_files) {
112 system "rm -rf $tmp_dir";
115 sleep 1 while $last_touch >= time;
116 system "git checkout $starting_branch" and exit 1;
125 my $parent = $parent{$patch};
126 if (defined $parent) {
127 unless ($completed{$parent}++) {
128 update_patch($parent);
130 $parent = "patch/$parent";
132 $parent = $master_branch;
135 print "======== $patch ========\n";
137 sleep 1 while $incl_generated_files && $last_touch >= time;
138 system "git checkout patch/$patch" and return 0;
140 my $ok = system("git merge $parent") == 0;
141 if (!$ok || $launch_shell) {
142 print qq|"git merge $parent" incomplete -- please fix.\n| if !$ok;
143 $ENV{PS1} = "[$parent] patch/$patch: ";
145 if (system($ENV{SHELL}) != 0) {
146 print "Abort? [n/y] ";
151 ($status, $is_clean) = &check_git_status;
157 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
158 print OUT $description{$patch}, "\n";
160 if ($incl_generated_files) {
161 system "./config.status Makefile && make gen && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
165 open(PIPE, '-|', 'git', 'diff', $parent) or die $!;
166 DIFF: while (<PIPE>) {
167 while (m{^diff --git a/PATCH}) {
169 last if m{^diff --git a/};
171 last DIFF if !defined $_;
178 if ($incl_generated_files) {
180 if ($parent eq $master_branch) {
181 $parent_dir = 'master';
183 ($parent_dir) = $parent =~ m{([^/]+)$};
185 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
187 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
188 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
189 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
204 open(IN, '-|', 'git status') or die $!;
205 my $status = join('', <IN>);
207 my $is_clean = $status =~ /\nnothing to commit \(working directory clean\)/;
208 my($starting_branch) = $status =~ /^# On branch (.+)\n/;
209 ($status, $is_clean, $starting_branch);
215 Usage: patch-update [OPTIONS]
217 --gen[=DIR] Include generated files. Optional dest DIR overrides "patches".
218 --skip-check Skip the check that ensures starting with a clean branch.