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 = uri_escape_utf8($page);
318 $diffurl=~s/\[\[file\]\]/$epage/g;
319 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
320 $diffurl=~s/\[\[r2\]\]/$newrev/g;
322 page => pagename($page),
327 while ($line = <SPSVC>) {
328 last if ($line =~ /^Log:$/);
330 unshift @message, { line => $line };
333 if (defined $message[0] &&
334 $message[0]->{line}=~/$config{web_commit_regexp}/) {
335 $user=defined $2 ? "$2" : "$3";
336 $message[0]->{line}=$4;
342 $line = <SPSVC>; # Tag
343 $line = <SPSVC>; # Branch
346 if ($line =~ /^Author: (.*)$/) {
347 $user = $1 unless defined $user && length $user;
350 error "expected Author, got $line";
354 if ($line =~ /^Date: (.*)$/) {
355 $when = str2time($1, 'UTC');
358 error "expected Date, got $line";
362 if ($line =~ /^PatchSet (.*)$/) {
366 error "expected PatchSet, got $line";
369 $line = <SPSVC>; # ---------------------
374 committype => $committype,
376 message => [@message],
379 last if @ret >= $num;
382 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
388 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
391 local $CWD = $config{srcdir};
393 # diff output is unavoidably preceded by the cvsps PatchSet entry
394 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
395 my $blank_lines_seen = 0;
397 # skip log, get to the diff
398 while (my $line = shift @cvsps) {
399 $blank_lines_seen++ if ($line =~ /^$/);
400 last if $blank_lines_seen == 2;
403 @cvsps = @cvsps[0..$maxlines-1]
404 if defined $maxlines && @cvsps > $maxlines;
410 return join("", @cvsps);
414 sub rcs_getctime ($) {
417 local $CWD = $config{srcdir};
419 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
421 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
422 || error "couldn't get cvs log output: $!\n";
426 if (/$cvs_log_infoline/) {
430 close CVSLOG || warn "cvs log $file exited $?";
432 if (! defined $date) {
433 warn "failed to parse cvs log for $file\n";
437 eval q{use Date::Parse};
439 $date=str2time($date, 'UTC');
440 debug("found ctime ".localtime($date)." for $file");
444 sub rcs_getmtime ($) {
445 error "rcs_getmtime is not implemented for cvs\n"; # TODO
449 # INTERNAL SUPPORT ROUTINES
451 sub commitmessage (@) {
454 if (defined $params{session}) {
455 if (defined $params{session}->param("name")) {
456 return "web commit by ".
457 $params{session}->param("name").
458 (length $params{message} ? ": $params{message}" : "");
460 elsif (defined $params{session}->remote_addr()) {
461 return "web commit from ".
462 $params{session}->remote_addr().
463 (length $params{message} ? ": $params{message}" : "");
466 return $params{message};
473 local $CWD = $config{srcdir};
475 my $info=`cvs status $file`;
476 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
480 sub cvs_is_controlling {
482 $dir=$config{srcdir} unless defined($dir);
483 return (-d "$dir/CVS") ? 1 : 0;
486 sub cvs_keyword_subst_args ($) {
489 local $CWD = $config{srcdir};
491 eval q{use File::MimeInfo};
493 my $filemime = File::MimeInfo::default($file);
496 defined($filemime) && $filemime eq 'text/plain'
497 ? return ('-kkv', $file)
498 : return ('-kb', $file);
503 unshift @cmd, 'cvs', '-Q';
505 local $CWD = $config{srcdir};
509 use Symbol qw(gensym);
516 local *CATCHERR = IO::File->new_tmpfile;
517 my $pid = open3(gensym(), \*CATCHOUT, ">&CATCHERR", @cmd);
518 while (my $l = <CATCHOUT>) {
525 while (my $l = <CATCHERR>) {
527 unless $l =~ /^cvs commit: changing keyword expansion /;
530 print STDOUT $cvsout;
531 print STDERR $cvserr;
533 return ($ret == 0) ? 1 : 0;