4 #ident "@(#)cvs/contrib:$Name: $:$Id: commit_prep,v 1.1 2006/05/09 01:06:37 bdemsky Exp $"
6 # first thing: delegate to cvs_acls for access controls.
7 system "$CVSROOT/CVSROOT/cvs_acls" == 0
8 or exit 1; # bad access controls!
11 # Perl filter to handle pre-commit checking of files. This program
12 # records the last directory where commits will be taking place for
13 # use by the log_accum.pl script. For new files, it forces the
14 # existence of a RCS "Id" keyword in the first ten lines of the file.
15 # For existing files, it checks version number in the "Id" line to
16 # prevent losing changes because an old version of a file was copied
19 # Possible future enhancements:
21 # Check for cruft left by unresolved conflicts. Search for
22 # "^<<<<<<<$", "^-------$", and "^>>>>>>>$".
24 # Look for a copyright and automagically update it to the
25 # current year. [[ bad idea! -- woods ]]
28 # Contributed by David Hampton <hampton@cisco.com>
30 # Hacked on lots by Greg A. Woods <woods@web.net>
33 # Configurable options
36 # Constants (remember to protect strings from RCS keyword substitution)
38 $LAST_FILE = "/tmp/#cvs.lastdir"; # must match name in log_accum.pl
39 $DIFF_FILE = "/tmp/#cvs.diff"; # must match name in log_accum.pl
40 $ENTRIES = "CVS/Entries";
42 # Patterns to find $Log keywords in files
44 $LogString1 = "\\\$\\Log: .* \\\$";
45 $LogString2 = "\\\$\\Log\\\$";
46 $NoLog = "%s - contains an RCS \$Log keyword. It must not!\n";
48 # pattern to match an RCS Id keyword line with an existing ID
50 $IDstring = "\"@\\(#\\)[^:]*:.*\\\$\Id: .*\\\$\"";
52 %s - Does not contain a properly formatted line with the keyword \"Id:\".
53 I.e. no lines match \"" . $IDstring . "\".
54 Please see the template files for an example.\n";
56 # pattern to match an RCS Id keyword line for a new file (i.e. un-expanded)
58 $NewId = "\"@(#)[^:]*:.*\\$\Id\\$\""; # emacs-fontify gets confused by the "
61 %s - The ID line should contain only \"@(#)module/path:\$Name\$:\$\Id\$\"
62 for a newly created file.\n";
65 %s - The file name '%s' in the ID line does not match
66 the actual filename.\n";
69 %s - How dare you!!! You replaced your copy of the file '%s',
70 which was based upon version %s, with an %s version based
71 upon %s. Please move your '%s' out of the way, perform an
72 update to get the current version, and them merge your changes
73 into that file, then try the commit again.\n";
76 %s - contains a CR (^M, ascii 13) character, which is not a valid
77 line ending on this platform. Either tag this as a binary
78 file or remove the bogus CR characters, and then try the
85 local($filename, $line) = @_;
86 open(FILE, ">$filename") || die("Cannot open $filename, stopped");
87 print(FILE $line, "\n");
92 local($filename, $line) = @_;
93 open(FILE, ">>$filename") || die("Cannot open $filename, stopped");
94 print(FILE $line, "\n");
99 local($i, $id, $rname, $version);
100 local($filename) = @_;
102 open(FILE, "<$filename") || return(0);
107 for ($i = 0; <FILE>; $i++) {
109 push(@all_lines, $_);
110 if ($_ =~ /$IDstring/) {
113 if ($_ =~ /$NewId/) {
116 if ($_ =~ /\015/ && $cvsflags{$filename} !~ /-kb/) {
117 print STDERR sprintf($BadCR, $filename);
122 if (grep(/$LogString1/, @all_lines) || grep(/$LogString2/, @all_lines)) {
123 print STDERR sprintf($NoLog, $filename);
128 print STDERR sprintf("file = %s, version = %d.\n", $filename, $cvsversion{$filename});
131 if ($cvsversion{$filename} == 0) {
132 if ($newidpos != -1 && $all_lines[$newidpos] !~ /$NewId/) {
133 print STDERR sprintf($NoName, $filename);
140 print STDERR sprintf($NoId, $filename);
144 $line = $all_lines[$idpos];
145 $pos = index($line, "Id: ");
147 print STDERR sprintf("%d in '%s'.\n", $pos, $line);
149 ($id, $rname, $version) = split(' ', substr($line, $pos));
150 if ($rname ne "$filename,v") {
151 print STDERR sprintf($BadName, $filename, substr($rname, 0, length($rname)-2));
154 if ($cvsversion{$filename} < $version) {
155 print STDERR sprintf($BadVersion, $filename, $filename, $cvsversion{$filename},
156 "newer", $version, $filename);
159 if ($cvsversion{$filename} > $version) {
160 print STDERR sprintf($BadVersion, $filename, $filename, $cvsversion{$filename},
161 "older", $version, $filename);
171 $id = getpgrp(); # You *must* use a shell that does setpgrp()!
173 # Check each file (except dot files) for an RCS "Id" keyword.
177 # Record the directory for later use by the log_accumulate script.
179 $record_directory = 0;
181 # Keep track of diffs for later use by the log_accumulate script.
185 # parse command line arguments
192 print STDERR "Debug turned on...\n";
193 } elsif ($arg eq '-c') {
195 } elsif ($arg eq '-r') {
196 $record_directory = 1;
197 } elsif ($arg eq '-u') {
204 $directory = shift @files;
207 print STDERR "dir - ", $directory, "\n";
208 print STDERR "files - ", join(":", @files), "\n";
209 print STDERR "id - ", $id, "\n";
212 # Suck in the CVS/Entries file
214 open(ENTRIES, $ENTRIES) || die("Cannot open $ENTRIES.\n");
216 local($filename, $version, $verdate, $flags)=split('/', substr($_, 1));
217 $cvsversion{$filename} = $version;
218 $cvsflags{$filename} = $flags;
222 # Now check each file name passed in, except for dot files. Dot files
223 # are considered to be administrative files by this script.
225 if ($check_id != 0) {
227 foreach $arg (@files) {
228 if (index($arg, ".") == 0) {
231 $failed += &check_version($arg);
239 # Create a diff for each file name passed in.
240 if ($track_diffs != 0) {
242 foreach $arg (@files) {
243 # open(STATUS, "-|") ||
244 # exec 'cvs', '-nQq', 'diff', '-u', $arg;
245 open(STATUS, "-|") || # redirect stderr, too.
246 exec 'csh', '-c', "cvs -nQq diff -u $arg |& cat";
253 &append_line("$DIFF_FILE.$id", join("\n", @diff_txt));
256 # Record this directory as the last one checked. This will be used
257 # by the log_accumulate script to determine when it is processing
258 # the final directory of a multi-directory commit.
260 if ($record_directory != 0) {
261 &write_line("$LAST_FILE.$id", $directory);