| 1 | #!/usr/bin/perl -w |
| 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 |
| 6 | # diffs. |
| 7 | |
| 8 | use strict; |
| 9 | |
| 10 | die "No 'patches' directory present in the current dir.\n" unless -d 'patches'; |
| 11 | die "No '.git' directory present in the current dir.\n" unless -d '.git'; |
| 12 | |
| 13 | open(IN, '<', 'prepare-source.mak') or die "Couldn't open prepare-source.mak: $!\n"; |
| 14 | $_ = join('', <IN>); |
| 15 | close IN; |
| 16 | |
| 17 | my @extra_files = m{\n([^\s:]+):.*\n\t\S}g; |
| 18 | my $incl_generated_files; |
| 19 | |
| 20 | $incl_generated_files = shift if @ARGV && $ARGV[0] eq '--gen'; |
| 21 | |
| 22 | system "git-checkout master" and exit 1; |
| 23 | my $last_touch = time; |
| 24 | |
| 25 | if ($incl_generated_files) { |
| 26 | die "'a' must not exist in the current directory.\n" if -e 'a'; |
| 27 | die "'b' must not exist in the current directory.\n" if -e 'b'; |
| 28 | system "./prepare-source && rsync -a @extra_files a/" and exit 1; |
| 29 | } |
| 30 | |
| 31 | my(@patches, %local_patch); |
| 32 | if (@ARGV) { |
| 33 | foreach (@ARGV) { |
| 34 | s{^(patches|patch|origin/patch)/} {}; |
| 35 | s{\.diff$} {}; |
| 36 | push(@patches, $_); |
| 37 | } |
| 38 | open(PIPE, '-|', 'git-branch', '-l') or die $!; |
| 39 | } else { |
| 40 | open(PIPE, '-|', 'git-branch', '-a') or die $!; |
| 41 | } |
| 42 | while (<PIPE>) { |
| 43 | if (m# origin/patch/(.*)#) { |
| 44 | push(@patches, $1); |
| 45 | } elsif (m# patch/(.*)#) { |
| 46 | $local_patch{$1} = 1; |
| 47 | } |
| 48 | } |
| 49 | close PIPE; |
| 50 | |
| 51 | foreach my $patch (@patches) { |
| 52 | print "======== $patch ========\n"; |
| 53 | sleep 1 if $incl_generated_files && $last_touch == time; |
| 54 | if ($local_patch{$patch}) { |
| 55 | system "git-checkout patch/$patch" and exit 1; |
| 56 | } else { |
| 57 | system "git-checkout --track -b patch/$patch origin/patch/$patch" and exit 1; |
| 58 | } |
| 59 | $last_touch = time; |
| 60 | |
| 61 | my $parent = 'master'; |
| 62 | open(IN, '<', 'PATCH') or next; |
| 63 | open(OUT, '>', "patches/$patch.diff") or die $!; |
| 64 | |
| 65 | while (<IN>) { |
| 66 | if (m#patch -p1 <patch/(\S+)\.diff# && $1 ne $patch) { |
| 67 | $parent = $1; |
| 68 | } |
| 69 | print OUT $_; |
| 70 | } |
| 71 | close IN; |
| 72 | print OUT "\n"; |
| 73 | |
| 74 | if (system("git-rebase -m $parent") != 0) { |
| 75 | print qq|"git-rebase -m $parent" incomplete -- please fix.\n|; |
| 76 | $ENV{PS1} = "[$parent] patch/$patch: "; |
| 77 | system $ENV{SHELL} and exit 1; |
| 78 | } |
| 79 | |
| 80 | open(PIPE, '-|', 'git-diff', 'master') or die $!; |
| 81 | while (<PIPE>) { |
| 82 | last if m{^diff --git a/PATCH b/PATCH$}; |
| 83 | print OUT $_; |
| 84 | } |
| 85 | while (<PIPE>) { |
| 86 | last if m{^diff --git a/}; |
| 87 | } |
| 88 | print OUT $_, <PIPE>; |
| 89 | close PIPE; |
| 90 | |
| 91 | if ($incl_generated_files) { |
| 92 | system "./prepare-source && rsync -a @extra_files b/" and exit 1; |
| 93 | open(PIPE, '-|', 'diff', '-up', 'a', 'b') or die $!; |
| 94 | while (<PIPE>) { |
| 95 | s/^((?:---|\+\+\+) [^\t]+)\t.*/$1/; |
| 96 | print OUT $_; |
| 97 | } |
| 98 | close PIPE; |
| 99 | } |
| 100 | |
| 101 | close OUT; |
| 102 | } |
| 103 | |
| 104 | if ($incl_generated_files) { |
| 105 | system "rm -rf a b"; |
| 106 | } |
| 107 | |
| 108 | print "-------- master --------\n"; |
| 109 | sleep 1 if $last_touch == time; |
| 110 | system "git-checkout master && ./prepare-source"; |