2 package IkiWiki::Plugin::cvs;
4 # Copyright (c) 2008 Amitai Schlair
7 # This code is derived from software contributed to ikiwiki
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
13 # 1. Redistributions of source code must retain the above copyright
14 # notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 # notice, this list of conditions and the following disclaimer in the
17 # documentation and/or other materials provided with the distribution.
19 # THIS SOFTWARE IS PROVIDED BY IKIWIKI AND CONTRIBUTORS ``AS IS''
20 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
23 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
39 hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
40 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
41 hook(type => "getsetup", id => "cvs", call => \&getsetup);
42 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
43 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
44 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
45 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
46 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
47 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
48 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
49 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
50 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
51 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
55 my $check_args=<<"EOF";
58 for (j = 1; j < argc; j++)
59 if (strstr(argv[j], "New directory") != NULL)
67 if (! defined $config{cvspath}) {
68 $config{cvspath}="ikiwiki";
70 if (exists $config{cvspath}) {
71 # code depends on the path not having extraneous slashes
72 $config{cvspath}=~tr#/#/#s;
73 $config{cvspath}=~s/\/$//;
74 $config{cvspath}=~s/^\///;
76 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
77 push @{$config{wrappers}}, {
78 wrapper => $config{cvs_wrapper},
79 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
87 safe => 0, # rcs plugin
92 example => "/cvs/wikirepo",
93 description => "cvs repository location",
100 description => "path inside repository where the wiki is located",
101 safe => 0, # paranoia
106 example => "/cvs/wikirepo/CVSROOT/post-commit",
107 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry",
114 description => "mode for cvs_wrapper (can safely be made suid)",
120 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
121 description => "cvsweb url to show file history ([[file]] substituted)",
127 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
128 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
138 local $CWD = $config{srcdir};
140 my $info=`cvs status $file`;
141 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
147 unshift @cmd, 'cvs', '-Q';
149 local $CWD = $config{srcdir};
151 open(my $savedout, ">&STDOUT");
152 open(STDOUT, ">", "/dev/null");
153 my $ret = system(@cmd);
154 open(STDOUT, ">&", $savedout);
156 return ($ret == 0) ? 1 : 0;
159 sub cvs_is_controlling {
161 $dir=$config{srcdir} unless defined($dir);
162 return (-d "$dir/CVS") ? 1 : 0;
166 return unless cvs_is_controlling;
167 cvs_runcvs('update', '-dP');
170 sub rcs_prepedit ($) {
171 # Prepares to edit a file under revision control. Returns a token
172 # that must be passed into rcs_commit when the file is ready
174 # The file is relative to the srcdir.
177 return unless cvs_is_controlling;
179 # For cvs, return the revision of the file when
181 my $rev=cvs_info("Repository revision", "$file");
182 return defined $rev ? $rev : "";
185 sub rcs_commit ($$$;$$) {
186 # Tries to commit the page; returns undef on _success_ and
187 # a version of the page with the rcs's conflict markers on failure.
188 # The file is relative to the srcdir.
195 return unless cvs_is_controlling;
198 $message="web commit by $user".(length $message ? ": $message" : "");
200 elsif (defined $ipaddr) {
201 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
204 # Check to see if the page has been changed by someone
205 # else since rcs_prepedit was called.
206 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
207 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
208 if (defined $rev && defined $oldrev && $rev != $oldrev) {
209 # Merge their changes into the file that we've
211 cvs_runcvs('update', $file) ||
212 warn("cvs merge from $oldrev to $rev failed\n");
215 if (! cvs_runcvs('commit', '-m',
216 IkiWiki::possibly_foolish_untaint $message)) {
217 my $conflict=readfile("$config{srcdir}/$file");
218 cvs_runcvs('update', '-C', $file) ||
219 warn("cvs revert failed\n");
223 return undef # success
226 sub rcs_commit_staged ($$$) {
227 # Commits all staged changes. Changes can be staged using rcs_add,
228 # rcs_remove, and rcs_rename.
229 my ($message, $user, $ipaddr)=@_;
232 $message="web commit by $user".(length $message ? ": $message" : "");
234 elsif (defined $ipaddr) {
235 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
238 if (! cvs_runcvs('commit', '-m',
239 IkiWiki::possibly_foolish_untaint $message)) {
240 warn "cvs staged commit failed\n";
243 return undef # success
247 # filename is relative to the root of the srcdir
249 my $parent=IkiWiki::dirname($file);
250 my @files_to_add = ($file);
252 eval q{use File::MimeInfo};
255 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
256 push @files_to_add, $parent;
257 $parent = IkiWiki::dirname($parent);
260 while ($file = pop @files_to_add) {
261 if (@files_to_add == 0) {
263 my $filemime = File::MimeInfo::default($file);
264 if (defined($filemime) && $filemime eq 'text/plain') {
265 cvs_runcvs('add', $file) ||
266 warn("cvs add $file failed\n");
268 cvs_runcvs('add', '-kb', $file) ||
269 warn("cvs add binary $file failed\n");
273 cvs_runcvs('add', $file) ||
274 warn("cvs add $file failed\n");
280 # filename is relative to the root of the srcdir
283 return unless cvs_is_controlling;
285 cvs_runcvs('rm', '-f', $file) ||
286 warn("cvs rm $file failed\n");
289 sub rcs_rename ($$) {
290 # filenames relative to the root of the srcdir
293 return unless cvs_is_controlling;
295 local $CWD = $config{srcdir};
297 if (system("mv", "$src", "$dest") != 0) {
298 warn("filesystem rename failed\n");
305 sub rcs_recentchanges($) {
309 return unless cvs_is_controlling;
311 eval q{use Date::Parse};
314 local $CWD = $config{srcdir};
316 # There's no cvsps option to get the last N changesets.
317 # Write full output to a temp file and read backwards.
319 eval q{use File::Temp qw/tempfile/};
321 eval q{use File::ReadBackwards};
324 my (undef, $tmpfile) = tempfile(OPEN=>0);
325 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
327 error "couldn't run cvsps: $!\n";
328 } elsif (($? >> 8) != 0) {
329 error "cvsps exited " . ($? >> 8) . ": $!\n";
332 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
333 || error "couldn't open $tmpfile for read: $!\n";
335 while (my $line = <SPSVC>) {
336 $line =~ /^$/ || error "expected blank line, got $line";
338 my ($rev, $user, $committype, $when);
339 my (@message, @pages);
341 # We're reading backwards.
342 # Forwards, an entry looks like so:
343 # ---------------------
346 # Author: $user (or user CGI runs as, for web commits)
352 # @pages (and revisions)
355 while ($line = <SPSVC>) {
356 last if ($line =~ /^Members:/);
361 my ($page, $revs) = split(/:/, $line);
362 my ($oldrev, $newrev) = split(/->/, $revs);
363 $oldrev =~ s/INITIAL/0/;
364 $newrev =~ s/\(DEAD\)//;
365 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
366 $diffurl=~s/\[\[file\]\]/$page/g;
367 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
368 $diffurl=~s/\[\[r2\]\]/$newrev/g;
370 page => pagename($page),
375 while ($line = <SPSVC>) {
376 last if ($line =~ /^Log:$/);
378 unshift @message, { line => $line };
381 if (defined $message[0] &&
382 $message[0]->{line}=~/$config{web_commit_regexp}/) {
383 $user=defined $2 ? "$2" : "$3";
384 $message[0]->{line}=$4;
389 $line = <SPSVC>; # Tag
390 $line = <SPSVC>; # Branch
393 if ($line =~ /^Author: (.*)$/) {
394 $user = $1 unless defined $user && length $user;
396 error "expected Author, got $line";
400 if ($line =~ /^Date: (.*)$/) {
401 $when = str2time($1, 'UTC');
403 error "expected Date, got $line";
407 if ($line =~ /^PatchSet (.*)$/) {
410 error "expected PatchSet, got $line";
413 $line = <SPSVC>; # ---------------------
418 committype => $committype,
420 message => [@message],
423 last if @ret >= $num;
426 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
432 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
434 local $CWD = $config{srcdir};
436 # diff output is unavoidably preceded by the cvsps PatchSet entry
437 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
438 my $blank_lines_seen = 0;
440 while (my $line = shift @cvsps) {
441 $blank_lines_seen++ if ($line =~ /^$/);
442 last if $blank_lines_seen == 2;
448 return join("", @cvsps);
452 sub rcs_getctime ($) {
455 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
457 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
458 || error "couldn't get cvs log output: $!\n";
462 if (/$cvs_log_infoline/) {
466 close CVSLOG || warn "cvs log $file exited $?";
468 if (! defined $date) {
469 warn "failed to parse cvs log for $file\n";
473 eval q{use Date::Parse};
475 $date=str2time($date, 'UTC');
476 debug("found ctime ".localtime($date)." for $file");