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
36 use URI::Escape q{uri_escape_utf8};
40 # GENERAL PLUGIN API CALLS
43 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
44 hook(type => "getsetup", id => "cvs", call => \&getsetup);
45 hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
47 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
48 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
49 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
50 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
51 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
52 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
53 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
54 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
55 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
56 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
57 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
61 if (! defined $config{cvspath}) {
62 $config{cvspath}="ikiwiki";
64 if (exists $config{cvspath}) {
65 # code depends on the path not having extraneous slashes
66 $config{cvspath}=~tr#/#/#s;
67 $config{cvspath}=~s/\/$//;
68 $config{cvspath}=~s/^\///;
70 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
71 push @{$config{wrappers}}, {
72 wrapper => $config{cvs_wrapper},
73 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
81 safe => 0, # rcs plugin
87 example => "/cvs/wikirepo",
88 description => "cvs repository location",
95 description => "path inside repository where the wiki is located",
101 example => "/cvs/wikirepo/CVSROOT/post-commit",
102 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
109 description => "mode for cvs_wrapper (can safely be made suid)",
115 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
116 description => "cvsweb url to show file history ([[file]] substituted)",
122 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
123 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
133 for (j = 1; j < argc; j++)
134 if (strstr(argv[j], "New directory") != NULL)
141 # VCS PLUGIN API CALLS
144 return unless cvs_is_controlling();
145 cvs_runcvs('update', '-dP');
148 sub rcs_prepedit ($) {
149 # Prepares to edit a file under revision control. Returns a token
150 # that must be passed into rcs_commit when the file is ready
152 # The file is relative to the srcdir.
155 return unless cvs_is_controlling();
157 # For cvs, return the revision of the file when
159 my $rev=cvs_info("Repository revision", "$file");
160 return defined $rev ? $rev : "";
164 # Tries to commit the page; returns undef on _success_ and
165 # a version of the page with the rcs's conflict markers on failure.
166 # The file is relative to the srcdir.
169 return unless cvs_is_controlling();
171 # Check to see if the page has been changed by someone
172 # else since rcs_prepedit was called.
173 my ($oldrev)=$params{token}=~/^([0-9]+)$/; # untaint
174 my $rev=cvs_info("Repository revision", "$config{srcdir}/$params{file}");
175 if (defined $rev && defined $oldrev && $rev != $oldrev) {
176 # Merge their changes into the file that we've
178 cvs_runcvs('update', $params{file}) ||
179 warn("cvs merge from $oldrev to $rev failed\n");
182 if (! cvs_runcvs('commit', '-m',
183 IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
184 my $conflict=readfile("$config{srcdir}/$params{file}");
185 cvs_runcvs('update', '-C', $params{file}) ||
186 warn("cvs revert failed\n");
190 return undef # success
193 sub rcs_commit_staged (@) {
194 # Commits all staged changes. Changes can be staged using rcs_add,
195 # rcs_remove, and rcs_rename.
198 if (! cvs_runcvs('commit', '-m',
199 IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
200 warn "cvs staged commit failed\n";
203 return undef # success
207 # filename is relative to the root of the srcdir
209 my $parent=IkiWiki::dirname($file);
210 my @files_to_add = ($file);
212 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
213 push @files_to_add, $parent;
214 $parent = IkiWiki::dirname($parent);
217 while ($file = pop @files_to_add) {
218 if (@files_to_add == 0) {
219 cvs_runcvs('add', cvs_keyword_subst_args($file)) ||
220 warn("cvs add file $file failed\n");
223 cvs_runcvs('add', $file) ||
224 warn("cvs add dir $file failed\n");
230 # filename is relative to the root of the srcdir
233 return unless cvs_is_controlling();
235 cvs_runcvs('rm', '-f', $file) ||
236 warn("cvs rm $file failed\n");
239 sub rcs_rename ($$) {
240 # filenames relative to the root of the srcdir
243 return unless cvs_is_controlling();
245 local $CWD = $config{srcdir};
247 if (system("mv", "$src", "$dest") != 0) {
248 warn("filesystem rename failed\n");
255 sub rcs_recentchanges ($) {
259 return unless cvs_is_controlling();
261 eval q{use Date::Parse};
264 local $CWD = $config{srcdir};
266 # There's no cvsps option to get the last N changesets.
267 # Write full output to a temp file and read backwards.
269 eval q{use File::Temp qw/tempfile/};
271 eval q{use File::ReadBackwards};
274 my ($tmphandle, $tmpfile) = tempfile();
275 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
277 error "couldn't run cvsps: $!\n";
279 elsif (($? >> 8) != 0) {
280 error "cvsps exited " . ($? >> 8) . ": $!\n";
283 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
284 || error "couldn't open $tmpfile for read: $!\n";
286 while (my $line = <SPSVC>) {
287 $line =~ /^$/ || error "expected blank line, got $line";
289 my ($rev, $user, $committype, $when);
290 my (@message, @pages);
292 # We're reading backwards.
293 # Forwards, an entry looks like so:
294 # ---------------------
297 # Author: $user (or user CGI runs as, for web commits)
303 # @pages (and revisions)
306 while ($line = <SPSVC>) {
307 last if ($line =~ /^Members:/);
312 my ($page, $revs) = split(/:/, $line);
313 my ($oldrev, $newrev) = split(/->/, $revs);
314 $oldrev =~ s/INITIAL/0/;
315 $newrev =~ s/\(DEAD\)//;
316 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
317 my $epage = join('/',
318 map { uri_escape_utf8($_) } split('/', $page)
320 $diffurl=~s/\[\[file\]\]/$epage/g;
321 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
322 $diffurl=~s/\[\[r2\]\]/$newrev/g;
324 page => pagename($page),
329 while ($line = <SPSVC>) {
330 last if ($line =~ /^Log:$/);
332 unshift @message, { line => $line };
335 if (defined $message[0] &&
336 $message[0]->{line}=~/$config{web_commit_regexp}/) {
337 $user=defined $2 ? "$2" : "$3";
338 $message[0]->{line}=$4;
344 $line = <SPSVC>; # Tag
345 $line = <SPSVC>; # Branch
348 if ($line =~ /^Author: (.*)$/) {
349 $user = $1 unless defined $user && length $user;
352 error "expected Author, got $line";
356 if ($line =~ /^Date: (.*)$/) {
357 $when = str2time($1, 'UTC');
360 error "expected Date, got $line";
364 if ($line =~ /^PatchSet (.*)$/) {
368 error "expected PatchSet, got $line";
371 $line = <SPSVC>; # ---------------------
376 committype => $committype,
378 message => [@message],
381 last if @ret >= $num;
384 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
390 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
393 local $CWD = $config{srcdir};
395 # diff output is unavoidably preceded by the cvsps PatchSet entry
396 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
397 my $blank_lines_seen = 0;
399 # skip log, get to the diff
400 while (my $line = shift @cvsps) {
401 $blank_lines_seen++ if ($line =~ /^$/);
402 last if $blank_lines_seen == 2;
405 @cvsps = @cvsps[0..$maxlines-1]
406 if defined $maxlines && @cvsps > $maxlines;
412 return join("", @cvsps);
416 sub rcs_getctime ($) {
419 local $CWD = $config{srcdir};
421 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
423 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
424 || error "couldn't get cvs log output: $!\n";
428 if (/$cvs_log_infoline/) {
432 close CVSLOG || warn "cvs log $file exited $?";
434 if (! defined $date) {
435 warn "failed to parse cvs log for $file\n";
439 eval q{use Date::Parse};
441 $date=str2time($date, 'UTC');
442 debug("found ctime ".localtime($date)." for $file");
446 sub rcs_getmtime ($) {
447 error "rcs_getmtime is not implemented for cvs\n"; # TODO
451 # INTERNAL SUPPORT ROUTINES
453 sub commitmessage (@) {
456 if (defined $params{session}) {
457 if (defined $params{session}->param("name")) {
458 return "web commit by ".
459 IkiWiki::cloak($params{session}->param("name")).
460 (length $params{message} ? ": $params{message}" : "");
462 elsif (defined $params{session}->remote_addr()) {
463 return "web commit from ".
464 IkiWiki::cloak($params{session}->remote_addr()).
465 (length $params{message} ? ": $params{message}" : "");
468 return $params{message};
475 local $CWD = $config{srcdir};
477 my $info=`cvs status $file`;
478 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
482 sub cvs_is_controlling {
484 $dir=$config{srcdir} unless defined($dir);
485 return (-d "$dir/CVS") ? 1 : 0;
488 sub cvs_keyword_subst_args ($) {
491 local $CWD = $config{srcdir};
493 eval q{use File::MimeInfo};
495 my $filemime = File::MimeInfo::default($file);
498 defined($filemime) && $filemime eq 'text/plain'
499 ? return ('-kkv', $file)
500 : return ('-kb', $file);
505 unshift @cmd, 'cvs', '-Q';
507 # CVS can't operate outside a srcdir, so we're always setting $CWD.
508 # "local $CWD" restores the previous value when we go out of scope.
509 # Usually that's correct. But if we're removing the last file from
510 # a directory, the post-commit hook will exec in a working directory
511 # that's about to not exist (CVS will prune it).
513 # chdir() manually here, so we can selectively not chdir() back.
516 chdir($config{srcdir});
520 use Symbol qw(gensym);
527 local *CATCHERR = IO::File->new_tmpfile;
528 my $pid = open3(gensym(), \*CATCHOUT, ">&CATCHERR", @cmd);
529 while (my $l = <CATCHOUT>) {
536 while (my $l = <CATCHERR>) {
538 unless $l =~ /^cvs commit: changing keyword expansion /;
541 print STDOUT $cvsout;
542 print STDERR $cvserr;
544 chdir($oldcwd) if -d $oldcwd;
546 return ($ret == 0) ? 1 : 0;