2 # This script is used to turn one or more of the "patch/BASE/*" 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 require 'packaging/git-status.pl';
35 check_git_state($master_branch, !$skip_branch_check, 1);
38 open PIPE, '-|', "git log -1 --no-color $master_branch" or die $!;
40 if (/^commit (\S+)/) {
46 die "Unable to determine commit hash for master branch: $master_branch\n" unless defined $master_commit;
49 open(IN, '<', 'Makefile.in') or die "Couldn't open Makefile.in: $!\n";
55 @extra_files = split(' ', $_);
61 if ($incl_generated_files) {
62 die "'$tmp_dir' must not exist in the current directory.\n" if -e $tmp_dir;
63 mkdir($tmp_dir, 0700) or die "Unable to mkdir($tmp_dir): $!\n";
64 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/master/" and exit 1;
66 our $last_touch = time;
70 # Start by finding all patches so that we can load all possible parents.
71 open(PIPE, '-|', 'git', 'branch', '-l') or die $!;
73 if (m# patch/\Q$master_branch\E/(.*)#o) {
79 my @patches = sort keys %patches;
81 my(%parent, %description);
82 foreach my $patch (@patches) {
83 my $branch = "patch/$master_branch/$patch";
85 open(PIPE, '-|', 'git', 'diff', '-U1000', "$master_branch...$branch", '--', "PATCH.$patch") or die $!;
90 next unless s/^[ +]//;
91 if (m#patch -p1 <patches/(\S+)\.diff# && $1 ne $patch) {
92 my $parent = $parent{$patch} = $1;
93 if (!$patches{$parent}) {
94 die "Parent of $patch is not a local branch: $parent\n";
100 $description{$patch} = $desc;
104 # Limit the list of patches to actually process based on @ARGV.
110 die "Local branch not available for patch: $_\n";
117 foreach my $patch (@patches) {
118 next if $completed{$patch}++;
119 last unless update_patch($patch);
122 if ($incl_generated_files) {
123 system "rm -rf $tmp_dir";
126 sleep 1 while $last_touch >= time;
127 system "git checkout $master_branch" and exit 1;
136 my $parent = $parent{$patch};
138 if (defined $parent) {
139 unless ($completed{$parent}++) {
140 update_patch($parent);
142 $based_on = $parent = "patch/$master_branch/$parent";
144 $parent = $master_branch;
145 $based_on = $master_commit;
148 print "======== $patch ========\n";
150 sleep 1 while $incl_generated_files && $last_touch >= time;
151 system "git checkout patch/$master_branch/$patch" and return 0;
153 my $ok = system("git merge $based_on") == 0;
154 if (!$ok || $launch_shell) {
155 my($parent_dir) = $parent =~ m{([^/]+)$};
156 print qq|"git merge $based_on" incomplete -- please fix.\n| if !$ok;
157 $ENV{PS1} = "[$parent_dir] $patch: ";
159 if (system($ENV{SHELL}) != 0) {
160 print "Abort? [n/y] ";
165 my($cur_branch, $is_clean, $status) = check_git_status(0);
171 open(OUT, '>', "$patches_dir/$patch.diff") or die $!;
172 print OUT $description{$patch}, "\nbased-on: $based_on\n";
174 if ($incl_generated_files) {
175 system "$make_gen_cmd && rsync -a @extra_files $tmp_dir/$patch/" and exit 1;
179 open(PIPE, '-|', 'git', 'diff', $based_on) or die $!;
180 DIFF: while (<PIPE>) {
181 while (m{^diff --git a/PATCH}) {
183 last if m{^diff --git a/};
185 last DIFF if !defined $_;
192 if ($incl_generated_files) {
194 if ($parent eq $master_branch) {
195 $parent_dir = 'master';
197 ($parent_dir) = $parent =~ m{([^/]+)$};
199 open(PIPE, '-|', 'diff', '-up', "$tmp_dir/$parent_dir", "$tmp_dir/$patch") or die $!;
201 s#^(diff -up) $tmp_dir/[^/]+/(.*?) $tmp_dir/[^/]+/(.*)#$1 a/$2 b/$3#o;
202 s#^\Q---\E $tmp_dir/[^/]+/([^\t]+)\t.*#--- a/$1#o;
203 s#^\Q+++\E $tmp_dir/[^/]+/([^\t]+)\t.*#+++ b/$1#o;
219 Usage: patch-update [OPTIONS] [patches/DIFF...]
222 -b, --branch=BRANCH The master branch to merge into the patch/BASE/* branches.
223 --gen[=DIR] Include generated files. Optional destination DIR
224 arg overrides the default of using the "patches" dir.
225 --skip-check Skip the check that ensures starting with a clean branch.
226 -s, --shell Launch a shell for every patch/BASE/* branch updated, not
227 just when a conflict occurs.
228 -h, --help Output this help message.