Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
freebsd
GitHub Repository: freebsd/freebsd-ports-kde
Path: blob/main/Tools/scripts/splitpatch.pl
16460 views
1
#! /usr/bin/env perl
2
# ----------------------------------------------------------------------------
3
# "THE BEER-WARE LICENSE" (Revision 42)
4
# <[email protected]> wrote this file. As long as you retain this notice you
5
# can do whatever you want with this stuff. If we meet some day, and you think
6
# this stuff is worth it, you can buy me a beer in return. Anton Berezin
7
# ----------------------------------------------------------------------------
8
9
use strict;
10
use warnings;
11
12
# good tests:
13
# /usr/ports/archivers/zoo/files/patch-aa (context diff)
14
# /usr/ports/astro/xplanet/files/patch-aa (unified with paths)
15
16
my ($in,$fl,$abort,$state,$out);
17
18
if (!@ARGV || $ARGV[0] =~ /^-/) {
19
print STDERR "Usage:
20
$0 patchfile ...
21
"
22
}
23
24
while (@ARGV) {
25
$in = shift;
26
$state = \&nofile;
27
if (open IN, "< $in") {
28
$abort = 0;
29
$out = "";
30
$fl = "";
31
while (<IN>) {
32
$state->();
33
last if $abort;
34
}
35
close IN;
36
if ($out && !$abort) {
37
print "Wrote $out\n";
38
}
39
} else {
40
print STDERR "cannot open $in: $!\n";
41
}
42
}
43
44
sub nofile
45
{
46
if (/^\*\*\*\s+/ && !/^\*\*\*\s+\d+,\d+\s+/) {
47
$state = \&cstart;
48
$fl = $_;
49
} elsif (/^---\s+/ && !/^---\s+\d+,\d+\s+/) {
50
$state = \&ustart;
51
$fl = $_;
52
}
53
}
54
55
sub cstart
56
{
57
if (!/^---\s+\d+,\d+\s+/ && /^---\s+(\S+)\s+/) {
58
$state = \&body;
59
$out = $1;
60
$out =~ s|/|__|g;
61
$out = "patch-$out";
62
if (open OUT, "> $out") {
63
print OUT $fl;
64
print OUT $_;
65
} else {
66
print STDERR "Cannot create $out: $!, aborting\n";
67
$abort = 1;
68
}
69
} else {
70
print STDERR "Bad context diff in $in, aborting\n";
71
$abort = 1;
72
}
73
}
74
75
sub ustart
76
{
77
if (/^\+\+\+\s+(\S+)\s+/) {
78
$state = \&body;
79
$out = $1;
80
$out =~ s|/|__|g;
81
$out = "patch-$out";
82
if (open OUT, "> $out") {
83
print OUT $fl;
84
print OUT $_;
85
} else {
86
print STDERR "Cannot create $out: $!, aborting\n";
87
$abort = 1;
88
}
89
} else {
90
print STDERR "Bad unified diff in $in, aborting\n";
91
$abort = 1;
92
}
93
}
94
95
sub body
96
{
97
if (/^\*\*\*\s+/ && !/^\*\*\*\s+\d+,\d+\s+/) {
98
close OUT;
99
print "Wrote $out\n";
100
$state = \&cstart;
101
$fl = $_;
102
} elsif (/^---\s+/ && !/^---\s+\d+,\d+\s+/) {
103
close OUT;
104
print "Wrote $out\n";
105
$state = \&ustart;
106
$fl = $_;
107
} else {
108
print OUT $_;
109
}
110
}
111
112