2 package IkiWiki::Plugin::cvs;
4 # Copyright (c) 2009 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);
58 for (j = 1; j < argc; j++)
59 if (strstr(argv[j], "New directory") != NULL)
66 if (! defined $config{cvspath}) {
67 $config{cvspath}="ikiwiki";
69 if (exists $config{cvspath}) {
70 # code depends on the path not having extraneous slashes
71 $config{cvspath}=~tr#/#/#s;
72 $config{cvspath}=~s/\/$//;
73 $config{cvspath}=~s/^\///;
75 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
76 push @{$config{wrappers}}, {
77 wrapper => $config{cvs_wrapper},
78 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
86 safe => 0, # rcs plugin
91 example => "/cvs/wikirepo",
92 description => "cvs repository location",
99 description => "path inside repository where the wiki is located",
100 safe => 0, # paranoia
105 example => "/cvs/wikirepo/CVSROOT/post-commit",
106 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
113 description => "mode for cvs_wrapper (can safely be made suid)",
119 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
120 description => "cvsweb url to show file history ([[file]] substituted)",
126 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
127 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
137 local $CWD = $config{srcdir};
139 my $info=`cvs status $file`;
140 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
146 unshift @cmd, 'cvs', '-Q';
148 local $CWD = $config{srcdir};
150 open(my $savedout, ">&STDOUT");
151 open(STDOUT, ">", "/dev/null");
152 my $ret = system(@cmd);
153 open(STDOUT, ">&", $savedout);
155 return ($ret == 0) ? 1 : 0;
158 sub cvs_is_controlling {
160 $dir=$config{srcdir} unless defined($dir);
161 return (-d "$dir/CVS") ? 1 : 0;
165 return unless cvs_is_controlling;
166 cvs_runcvs('update', '-dP');
169 sub rcs_prepedit ($) {
170 # Prepares to edit a file under revision control. Returns a token
171 # that must be passed into rcs_commit when the file is ready
173 # The file is relative to the srcdir.
176 return unless cvs_is_controlling;
178 # For cvs, return the revision of the file when
180 my $rev=cvs_info("Repository revision", "$file");
181 return defined $rev ? $rev : "";
184 sub rcs_commit ($$$;$$) {
185 # Tries to commit the page; returns undef on _success_ and
186 # a version of the page with the rcs's conflict markers on failure.
187 # The file is relative to the srcdir.
194 return unless cvs_is_controlling;
197 $message="web commit by $user".(length $message ? ": $message" : "");
199 elsif (defined $ipaddr) {
200 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
203 # Check to see if the page has been changed by someone
204 # else since rcs_prepedit was called.
205 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
206 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
207 if (defined $rev && defined $oldrev && $rev != $oldrev) {
208 # Merge their changes into the file that we've
210 cvs_runcvs('update', $file) ||
211 warn("cvs merge from $oldrev to $rev failed\n");
214 if (! cvs_runcvs('commit', '-m',
215 IkiWiki::possibly_foolish_untaint $message)) {
216 my $conflict=readfile("$config{srcdir}/$file");
217 cvs_runcvs('update', '-C', $file) ||
218 warn("cvs revert failed\n");
222 return undef # success
225 sub rcs_commit_staged ($$$) {
226 # Commits all staged changes. Changes can be staged using rcs_add,
227 # rcs_remove, and rcs_rename.
228 my ($message, $user, $ipaddr)=@_;
231 $message="web commit by $user".(length $message ? ": $message" : "");
233 elsif (defined $ipaddr) {
234 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
237 if (! cvs_runcvs('commit', '-m',
238 IkiWiki::possibly_foolish_untaint $message)) {
239 warn "cvs staged commit failed\n";
242 return undef # success
246 # filename is relative to the root of the srcdir
248 my $parent=IkiWiki::dirname($file);
249 my @files_to_add = ($file);
251 eval q{use File::MimeInfo};
254 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
255 push @files_to_add, $parent;
256 $parent = IkiWiki::dirname($parent);
259 while ($file = pop @files_to_add) {
260 if (@files_to_add == 0) {
262 my $filemime = File::MimeInfo::default($file);
263 if (defined($filemime) && $filemime eq 'text/plain') {
264 cvs_runcvs('add', $file) ||
265 warn("cvs add $file failed\n");
268 cvs_runcvs('add', '-kb', $file) ||
269 warn("cvs add binary $file failed\n");
274 cvs_runcvs('add', $file) ||
275 warn("cvs add $file failed\n");
281 # filename is relative to the root of the srcdir
284 return unless cvs_is_controlling;
286 cvs_runcvs('rm', '-f', $file) ||
287 warn("cvs rm $file failed\n");
290 sub rcs_rename ($$) {
291 # filenames relative to the root of the srcdir
294 return unless cvs_is_controlling;
296 local $CWD = $config{srcdir};
298 if (system("mv", "$src", "$dest") != 0) {
299 warn("filesystem rename failed\n");
306 sub rcs_recentchanges($) {
310 return unless cvs_is_controlling;
312 eval q{use Date::Parse};
315 local $CWD = $config{srcdir};
317 # There's no cvsps option to get the last N changesets.
318 # Write full output to a temp file and read backwards.
320 eval q{use File::Temp qw/tempfile/};
322 eval q{use File::ReadBackwards};
325 my (undef, $tmpfile) = tempfile(OPEN=>0);
326 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
328 error "couldn't run cvsps: $!\n";
330 elsif (($? >> 8) != 0) {
331 error "cvsps exited " . ($? >> 8) . ": $!\n";
334 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
335 || error "couldn't open $tmpfile for read: $!\n";
337 while (my $line = <SPSVC>) {
338 $line =~ /^$/ || error "expected blank line, got $line";
340 my ($rev, $user, $committype, $when);
341 my (@message, @pages);
343 # We're reading backwards.
344 # Forwards, an entry looks like so:
345 # ---------------------
348 # Author: $user (or user CGI runs as, for web commits)
354 # @pages (and revisions)
357 while ($line = <SPSVC>) {
358 last if ($line =~ /^Members:/);
363 my ($page, $revs) = split(/:/, $line);
364 my ($oldrev, $newrev) = split(/->/, $revs);
365 $oldrev =~ s/INITIAL/0/;
366 $newrev =~ s/\(DEAD\)//;
367 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
368 $diffurl=~s/\[\[file\]\]/$page/g;
369 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
370 $diffurl=~s/\[\[r2\]\]/$newrev/g;
372 page => pagename($page),
377 while ($line = <SPSVC>) {
378 last if ($line =~ /^Log:$/);
380 unshift @message, { line => $line };
383 if (defined $message[0] &&
384 $message[0]->{line}=~/$config{web_commit_regexp}/) {
385 $user=defined $2 ? "$2" : "$3";
386 $message[0]->{line}=$4;
392 $line = <SPSVC>; # Tag
393 $line = <SPSVC>; # Branch
396 if ($line =~ /^Author: (.*)$/) {
397 $user = $1 unless defined $user && length $user;
400 error "expected Author, got $line";
404 if ($line =~ /^Date: (.*)$/) {
405 $when = str2time($1, 'UTC');
408 error "expected Date, got $line";
412 if ($line =~ /^PatchSet (.*)$/) {
416 error "expected PatchSet, got $line";
419 $line = <SPSVC>; # ---------------------
424 committype => $committype,
426 message => [@message],
429 last if @ret >= $num;
432 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
438 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
440 local $CWD = $config{srcdir};
442 # diff output is unavoidably preceded by the cvsps PatchSet entry
443 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
444 my $blank_lines_seen = 0;
446 while (my $line = shift @cvsps) {
447 $blank_lines_seen++ if ($line =~ /^$/);
448 last if $blank_lines_seen == 2;
455 return join("", @cvsps);
459 sub rcs_getctime ($) {
462 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
464 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
465 || error "couldn't get cvs log output: $!\n";
469 if (/$cvs_log_infoline/) {
473 close CVSLOG || warn "cvs log $file exited $?";
475 if (! defined $date) {
476 warn "failed to parse cvs log for $file\n";
480 eval q{use Date::Parse};
482 $date=str2time($date, 'UTC');
483 debug("found ctime ".localtime($date)." for $file");