summaryrefslogtreecommitdiffstats
path: root/CVSROOT/commit_prep.pl
blob: ea6f8aae50231f19cc96ca0152f190da1cc8751c (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
#!/usr/bin/perl -w
#
# $FreeBSD$
#
#
# Perl filter to handle pre-commit checking of files.  This program
# records the last directory where commits will be taking place for
# use by the log_accumulate script.  For new file, it forcing the
# existence of a RCS "Id" keyword in the first ten lines of the file.
# For existing files, it checks version number in the "Id" line to
# prevent losing changes because an old version of a file was copied
# into the direcory.
#
# Possible future enhancements:
#
#
#    Check for cruft left by unresolved conflicts.  Search for
#    "^<<<<<<<$", "^-------$", and "^>>>>>>>$".
#
#    Look for a copyright and automagically update it to the
#    current year.
#
# Contributed by David Hampton <hampton@cisco.com>
#

require 5.003;	# to be sure.  log_accum needs perl5

############################################################
#
# Configurable options
#
############################################################
#
# Check each file (except dot files) for our RCS header keyword.
# (defined in the constants section.)
#
$check_id = 0;

#
# Record the directory for later use by the log_accumulate stript.
#
$record_directory = 1;

############################################################
#
# Constants
#
############################################################
$LAST_FILE     = "/tmp/#cvs.files.lastdir";
$ENTRIES       = "CVS/Entries";
$HEADER        = 'FreeBSD';	# Our RCS header is '$ FreeBSD $', 
				# (without the spaces.)

$NoId = "
%s - Does not contain a line with the keyword \"\$$HEADER:\".\n";

# Protect string from substitution by RCS.
$NoName = "
%s - The ID line should contain only \$$HEADER\$ for a newly created file.\n";

#$DelPath = "
#%s - The old path and version has been deleted from \$$HEADER\$.\n";

$BadId = "%s - The \$$HEADER\$ is mangled.\n";

$BadName = "
%s - The pathname '%s'
    in the \$$HEADER\$ line does not match the actual filename.\n";

$BadVersion = "
%s - GRRR!!  You spammed your copy of the file
    which was based upon version %s, with a different version based
    upon %s.  Please move your '%s' out of the way,
    perform an update to get the current version, and then
    CAREFULLY merge your changes into that file.\n";

############################################################
#
# Subroutines
#
############################################################

sub write_line {
    local($filename, $line) = @_;
    open(FILE, ">$filename") || die("Cannot open $filename, stopped");
    print(FILE $line, "\n");
    close(FILE);
}

sub check_version {
    local($id, $rname, $version, $bareid, $exclude, $path);
    local($filename, $directory, $hastag, $cvsversion) = @_;

    if (! -f $filename) {
	return(0);	# not present - either removed or let cvs deal with it.
    }
    open(FILE, $filename) || die("Cannot open $filename, stopped");
    # requiring the header within the first 'n' lines isn't useful.
    while (1) {
	$pos = -1;
	last if eof(FILE);
	$line = <FILE>;
	$pos = index($line, "\$$HEADER");
	last if ($pos >= 0);
    }

    if ($pos == -1) {
	$exclude = $cvsroot . "/CVSROOT/exclude";
	$path = $directory . "/" . $filename;
	open(EX, "<$exclude") || die("cannot open $exclude: $!");
	while (<EX>) {
	    local($line);

	    chop;
	    $line = $_;

	    if ($line =~ /^#/) {
		next;
	    }
	    if ($path =~ /$line/) {
		close(EX);
		return(0);
	    }
	}
	close(EX);
    }
    if ($pos == -1) {
	printf($NoId, $filename);
	return(1);
    }
    $bareid = (index($line, "\$$HEADER: \$") >= 0 ||
		index($line, "\$$HEADER\$") >= 0);
    if (!$bareid && $line !~ /\$$HEADER: .* \$/) {
	printf($BadId, $filename);
	return(1);
    }
    # Ignore version mismatches (MFC spamming etc) on branches.
    if ($hastag) {
	return (0);
    }
    ($id, $rname, $version) = split(' ', substr($line, $pos));
    if ($cvsversion{$filename} eq '0') {
	if (!$bareid) {
	    printf($NoName, $filename);
	    return(1);
	}
	return(0);
    }
    if ($bareid) {
	return (0);
#	if ($directory =~ /^ports\//) {
#	    return (0);	# ok for ports
#	}
#	# Don't know whether to allow or trap this.  It means one could
#	# bypass the version spam checks by simply using a bare tag.
#	printf($DelPath, $filename);
#	return(1);
    }
    if ($rname ne "$directory/$filename,v") {
	# If ports and the pathname is just the basename (eg: somebody sent
	# in a port with $Id$ and the committer changed Id -> $HEADER and
	# the version numbers otherwise match.
	if (!($directory =~ /^ports\// && $rname eq "$filename,v")) {
	    printf($BadName, "$directory/$filename,v", $rname);
	    return(1);
	}
    }
    if ($cvsversion{$filename} ne $version) {
	printf($BadVersion, $filename, $cvsversion{$filename},
	       $version, $filename);
	return(1);
    }
    return(0);
}

#############################################################
#
# Main Body
#
############################################################

$id = getpgrp();
#print("ARGV - ", join(":", @ARGV), "\n");
#print("id   - ", id, "\n");

#
# Suck in the Entries file
#
open(ENTRIES, $ENTRIES) || die("Cannot open $ENTRIES.\n");
while (<ENTRIES>) {
    chop;
    next if (/^D/);
    local($filename, $version, $stamp, $opt, $tag) = split('/', substr($_, 1));
    $cvsversion{$filename} = $version;
    $cvstag{$filename} = $tag;
    $stamp = $opt;	#silence -w
}
close(ENTRIES);

$directory = $ARGV[0];
shift @ARGV;

$cvsroot=$ENV{'CVSROOT'} || "/home/ncvs";
$directory =~ s,^$cvsroot[/]+,,;

if ($directory =~ /^src/) {
	$check_id = 1;
}
if ($directory =~ /^ports/) {
	$check_id = 2;
}
if ($directory =~ /^src\/contrib/) {
	$check_id = 3;
}
if ($directory =~ /^src\/crypto/) {
	$check_id = 3;
}
if ($directory =~ /^src\/release/) {
	$check_id = 0;
}
if ($directory =~ /^src\/etc/) {
	$check_id = 0;
}
$login = $ENV{'USER'} || getlogin || (getpwuid($<))[0] || sprintf("uid#%d",$<);
if ($check_id != 0 && $ENV{'CVSFUBAR'}) {
	$check_id = 0;
	print "CVS VERSION CHECK BYPASSED!\n";
	system("ps -xww | mail -s 'version check override used' cvs $login");
}
#
# Now check each file name passed in, except for dot files.  Dot files
# are considered to be administrative files by this script.
#
if ($check_id != 0) {
    $failed = 0;
    foreach $arg (@ARGV) {
	local($hastag) = ($cvstag{$arg} ne '');
	next if (index($arg, ".") == 0);
	next if ($check_id == 2 && $arg ne "Makefile");
	next if ($check_id == 3 && $hastag);
	$failed += &check_version($arg, $directory, $hastag, $cvsversion);
    }
    if ($failed) {
	print "\n";
	unlink("$LAST_FILE.$id");
	exit(1);
    }
}

#
# Record this directory as the last one checked.  This will be used
# by the log_accumulate script to determine when it is processing
# the final directory of a multi-directory commit.
#
if ($record_directory != 0) {
    &write_line("$LAST_FILE.$id", $directory);
}
exit(0);
OpenPOWER on IntegriCloud