summaryrefslogtreecommitdiff
path: root/pkgtools/pkgdiff/files/mkpatches.pl
blob: b48af1fd51bf98301c978dc193e199d754a5336f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#!@PERL5@
#
# $NetBSD: mkpatches.pl,v 1.16 2011/03/04 15:57:07 wiz Exp $
#
# mkpatches: creates a set of patches patch-aa, patch-ab, ...
#   in work/.newpatches by looking for *.orig files in and below
#   WRKDIR and comparing them to the corresponding changed file. All
#   files are then referrenced relative to WRKSRC.
#
#   It should be called from the packages directory,
#   e.g. /usr/pkgsrc/example/test
#
#   It retains the naming and header (RCS Id and comment) from the
#   patches directory.
#
# Copyright (c) 2000, 2011 by Thomas Klausner <wiz@NetBSD.org>
#               2004 by Dieter Baron <dillo@NetBSD.org>
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHORS
# ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
# TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
# PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHORS
# BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

use Getopt::Std;
use Cwd;
use File::Spec;

my $patchdir;
my $old_patchdir;
my $wrkdir;
my $wrksrc;
my %old_filename;
my %old_header;

# create patchdir, or empty it if already existing

sub create_patchdir {
    if (! -d $patchdir) {
	mkdir($patchdir, 0755);
	if (-d $origpatchdir && "$origpatchdir" != "$patchdir") {
	    system("cp", "$origpatchdir/p*", "$patchdir");
	}
    }
}

# read command line arguments

undef($opt_c);
undef($opt_D);
undef($opt_d);
undef($opt_h);
undef($opt_r);
undef($opt_v);

getopts('cDd:hrv');

if ($opt_h) {
		($prog) = ($0 =~ /([^\/]+)$/);
		print STDERR <<EOF;
usage: $prog [-hv] [-c | -r] [-D | -d dir]
    -c   	commit -- clean up old patches backups
    -d dir   	create patches in this directory
    -D   	create patches in \$WRKDIR/.newpatches
    -h   	show this help
    -r   	revert -- remove new patches, put old patches back
    -v   	verbose - list .orig files as processed
EOF
		exit 0;
};

if ($opt_d && $opt_D) {
    print STDERR "-D and -d conflict, choose one\n";
    exit 1;
}

# get some pkgsrc variables

$wrksrc=`@MAKE@ show-var VARNAME=WRKSRC` or 
    die ("can't find WRKSRC -- wrong dir?");
chomp($wrksrc);
$wrkdir=`@MAKE@ show-var VARNAME=WRKDIR` or
    die ("can't find WRKDIR -- wrong dir?");
chomp($wrkdir);
$origpatchdir=`@MAKE@ show-var VARNAME=PATCHDIR` or
    die ("can't find PATCHDIR -- wrong dir?");
chomp($origpatchdir);

if ($opt_D) {
    $patchdir = "$wrkdir/.newpatches";
} elsif ($opt_d) {
    if (-d "/$opt_d") {
	$patchdir = $opt_d;
    } else {
	my $pwd = cwd();
	chomp($pwd);
	$patchdir = "$pwd/$opt_d";
    }
} else {
    $patchdir = $origpatchdir;
}

if ($opt_c) {
    open(HANDLE, "find ${patchdir} -type f -name \\\*.orig |");
    foreach (<HANDLE>) {
	chomp;
	unlink $_;
    }
    exit 0;
}

if ($opt_r) {
    open(HANDLE, "find ${patchdir} -type f -name \\\*.orig |");
    foreach (<HANDLE>) {
	chomp;
	my $orig = $_;
	my $new = $_;
	$new =~ s/.orig$//;
	rename $orig, $new;
	if (! -s $new) {
	    unlink $new;
	}
    }
    exit 0;
}

create_patchdir();


move_away_old_patches();

analyze_old_patches();

chdir $wrksrc or die ("can't cd to WRKSRC ($wrksrc)");

# find files

open(HANDLE, "find ${wrksrc} -type f -name \\\*.orig |");

# create patches

foreach (sort <HANDLE>) {
    my ($path, $complete);
    my ($new, $old);
    chomp();
    $path = $_;
    $complete = $path;
    $complete =~ s/.orig$//;
    $new = File::Spec->abs2rel($complete, $wrksrc);
    $old = File::Spec->abs2rel($path, $wrksrc);
    if (-f $complete) {
	$patchfile = patch_name($new);
	if ($opt_v) {
	    print "$patchfile -> $complete\n";
	}
	$diff=`pkgdiff $old $new 2>&1`;
	if ($?) {
	    print "$old: $diff";
	}
	make_patch($old, $new, $patchfile, $diff);
    } else {
	print ("$new doesn't exist, though $old does\n");
    }
}

sub analyze_old_patches 
{
    my $filename;
    my $origfilename;
    my $patch;
    my $name;
    my $checkname;

    %old_header = ();
    %old_filename = ();

    open(HANDLE, "ls $patchdir/patch-* 2>/dev/null |");

    while ($origfilename = <HANDLE>) {
	chomp $origfilename;
	next if not $origfilename =~ m/.orig$/;
	$filename = $origfilename;
	$filename =~ s/.orig$//;
	$checkname = $origfilename;
	if (! -s $checkname) {
	    $checkname = $filename;
	}
	$patch = `sed '/^\+\+\+/ q' $checkname`;
	if (!($patch =~ m/^\+\+\+ ([^\t\n]*)(\n$|\t)/m)) {
	    warn "cannot extract filename from patch $checkname";
	    next;
	}
	$name = $1;
	$name =~ s/^\.\///; # ignore leading "./", if any.
	$patch =~ s/\n--- .*/\n/s;
	$old_header{$name} = $patch;
	$filename =~ s!.*/!!;
	$old_filename{$name} = $filename;
    }

    close(HANDLE);
}

sub move_away_old_patches
{
    open(HANDLE, "ls $patchdir/patch-* 2>/dev/null |");

    while ($filename = <HANDLE>) {
	chomp $filename;
	next if $filename =~ m/.orig$/;
	if (-f "$filename" and not -f "$filename.orig") {
	    rename "$filename", "$filename.orig";
	}
    }
	
}

sub patch_name # filename
{
    my $name = shift;
    my ($pname, $l);

    if (defined($old_filename{$name})) {
	return $old_filename{$name};
    }

    $name =~ s,_,__,g;
    $name =~ s,/,_,g;
    $name = "patch-$name";

    return $name;
}


sub make_patch # new old patchfile diff
{
    my ($old, $new, $patchfile, $diff) = @_;

    if ("$diff" eq "") {
	print "$old and $new don't differ\n";
	if (-f "$patchdir/$patchfile.orig") {
	    rename "$patchdir/$patchfile.orig", "$patchdir/$patchfile";
	}
    }
    if (not -f "$patchdir/$patchfile.orig") {
	system("touch", "$patchdir/$patchfile.orig");
    }
    if (defined($old_header{$new})) {
	$diff =~ s/^.*\n(--- )/$1/s;
	$diff = $old_header{$new} . $diff;
    }

    open(HANDLE, "> $patchdir/$patchfile");
    print HANDLE $diff;
    close(HANDLE);
}