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);
52 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
59 for (j = 1; j < argc; j++)
60 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
93 example => "/cvs/wikirepo",
94 description => "cvs repository location",
100 example => "ikiwiki",
101 description => "path inside repository where the wiki is located",
102 safe => 0, # paranoia
107 example => "/cvs/wikirepo/CVSROOT/post-commit",
108 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
115 description => "mode for cvs_wrapper (can safely be made suid)",
121 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
122 description => "cvsweb url to show file history ([[file]] substituted)",
128 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
129 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
139 local $CWD = $config{srcdir};
141 my $info=`cvs status $file`;
142 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
148 unshift @cmd, 'cvs', '-Q';
150 local $CWD = $config{srcdir};
152 open(my $savedout, ">&STDOUT");
153 open(STDOUT, ">", "/dev/null");
154 my $ret = system(@cmd);
155 open(STDOUT, ">&", $savedout);
157 return ($ret == 0) ? 1 : 0;
160 sub cvs_is_controlling {
162 $dir=$config{srcdir} unless defined($dir);
163 return (-d "$dir/CVS") ? 1 : 0;
167 return unless cvs_is_controlling;
168 cvs_runcvs('update', '-dP');
171 sub rcs_prepedit ($) {
172 # Prepares to edit a file under revision control. Returns a token
173 # that must be passed into rcs_commit when the file is ready
175 # The file is relative to the srcdir.
178 return unless cvs_is_controlling;
180 # For cvs, return the revision of the file when
182 my $rev=cvs_info("Repository revision", "$file");
183 return defined $rev ? $rev : "";
186 sub rcs_commit ($$$;$$$) {
187 # Tries to commit the page; returns undef on _success_ and
188 # a version of the page with the rcs's conflict markers on failure.
189 # The file is relative to the srcdir.
197 return unless cvs_is_controlling;
200 $message="web commit by $user".(length $message ? ": $message" : "");
202 elsif (defined $ipaddr) {
203 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
206 # Check to see if the page has been changed by someone
207 # else since rcs_prepedit was called.
208 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
209 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
210 if (defined $rev && defined $oldrev && $rev != $oldrev) {
211 # Merge their changes into the file that we've
213 cvs_runcvs('update', $file) ||
214 warn("cvs merge from $oldrev to $rev failed\n");
217 if (! cvs_runcvs('commit', '-m',
218 IkiWiki::possibly_foolish_untaint $message)) {
219 my $conflict=readfile("$config{srcdir}/$file");
220 cvs_runcvs('update', '-C', $file) ||
221 warn("cvs revert failed\n");
225 return undef # success
228 sub rcs_commit_staged ($$$;$) {
229 # Commits all staged changes. Changes can be staged using rcs_add,
230 # rcs_remove, and rcs_rename.
231 my ($message, $user, $ipaddr, $emailuser)=@_;
234 $message="web commit by $user".(length $message ? ": $message" : "");
236 elsif (defined $ipaddr) {
237 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
240 if (! cvs_runcvs('commit', '-m',
241 IkiWiki::possibly_foolish_untaint $message)) {
242 warn "cvs staged commit failed\n";
245 return undef # success
249 # filename is relative to the root of the srcdir
251 my $parent=IkiWiki::dirname($file);
252 my @files_to_add = ($file);
254 eval q{use File::MimeInfo};
257 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
258 push @files_to_add, $parent;
259 $parent = IkiWiki::dirname($parent);
262 while ($file = pop @files_to_add) {
263 if (@files_to_add == 0) {
265 my $filemime = File::MimeInfo::default($file);
266 if (defined($filemime) && $filemime eq 'text/plain') {
267 cvs_runcvs('add', $file) ||
268 warn("cvs add $file failed\n");
271 cvs_runcvs('add', '-kb', $file) ||
272 warn("cvs add binary $file failed\n");
277 cvs_runcvs('add', $file) ||
278 warn("cvs add $file failed\n");
284 # filename is relative to the root of the srcdir
287 return unless cvs_is_controlling;
289 cvs_runcvs('rm', '-f', $file) ||
290 warn("cvs rm $file failed\n");
293 sub rcs_rename ($$) {
294 # filenames relative to the root of the srcdir
297 return unless cvs_is_controlling;
299 local $CWD = $config{srcdir};
301 if (system("mv", "$src", "$dest") != 0) {
302 warn("filesystem rename failed\n");
309 sub rcs_recentchanges ($) {
313 return unless cvs_is_controlling;
315 eval q{use Date::Parse};
318 local $CWD = $config{srcdir};
320 # There's no cvsps option to get the last N changesets.
321 # Write full output to a temp file and read backwards.
323 eval q{use File::Temp qw/tempfile/};
325 eval q{use File::ReadBackwards};
328 my ($tmphandle, $tmpfile) = tempfile();
329 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
331 error "couldn't run cvsps: $!\n";
333 elsif (($? >> 8) != 0) {
334 error "cvsps exited " . ($? >> 8) . ": $!\n";
337 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
338 || error "couldn't open $tmpfile for read: $!\n";
340 while (my $line = <SPSVC>) {
341 $line =~ /^$/ || error "expected blank line, got $line";
343 my ($rev, $user, $committype, $when);
344 my (@message, @pages);
346 # We're reading backwards.
347 # Forwards, an entry looks like so:
348 # ---------------------
351 # Author: $user (or user CGI runs as, for web commits)
357 # @pages (and revisions)
360 while ($line = <SPSVC>) {
361 last if ($line =~ /^Members:/);
366 my ($page, $revs) = split(/:/, $line);
367 my ($oldrev, $newrev) = split(/->/, $revs);
368 $oldrev =~ s/INITIAL/0/;
369 $newrev =~ s/\(DEAD\)//;
370 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
371 $diffurl=~s/\[\[file\]\]/$page/g;
372 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
373 $diffurl=~s/\[\[r2\]\]/$newrev/g;
375 page => pagename($page),
380 while ($line = <SPSVC>) {
381 last if ($line =~ /^Log:$/);
383 unshift @message, { line => $line };
386 if (defined $message[0] &&
387 $message[0]->{line}=~/$config{web_commit_regexp}/) {
388 $user=defined $2 ? "$2" : "$3";
389 $message[0]->{line}=$4;
395 $line = <SPSVC>; # Tag
396 $line = <SPSVC>; # Branch
399 if ($line =~ /^Author: (.*)$/) {
400 $user = $1 unless defined $user && length $user;
403 error "expected Author, got $line";
407 if ($line =~ /^Date: (.*)$/) {
408 $when = str2time($1, 'UTC');
411 error "expected Date, got $line";
415 if ($line =~ /^PatchSet (.*)$/) {
419 error "expected PatchSet, got $line";
422 $line = <SPSVC>; # ---------------------
427 committype => $committype,
429 message => [@message],
432 last if @ret >= $num;
435 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
441 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
443 local $CWD = $config{srcdir};
445 # diff output is unavoidably preceded by the cvsps PatchSet entry
446 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
447 my $blank_lines_seen = 0;
449 while (my $line = shift @cvsps) {
450 $blank_lines_seen++ if ($line =~ /^$/);
451 last if $blank_lines_seen == 2;
458 return join("", @cvsps);
462 sub rcs_getctime ($) {
465 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
467 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
468 || error "couldn't get cvs log output: $!\n";
472 if (/$cvs_log_infoline/) {
476 close CVSLOG || warn "cvs log $file exited $?";
478 if (! defined $date) {
479 warn "failed to parse cvs log for $file\n";
483 eval q{use Date::Parse};
485 $date=str2time($date, 'UTC');
486 debug("found ctime ".localtime($date)." for $file");
490 sub rcs_getmtime ($) {
491 error "rcs_getmtime is not implemented for cvs\n"; # TODO