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 # GENERAL PLUGIN API CALLS
42 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
43 hook(type => "getsetup", id => "cvs", call => \&getsetup);
44 hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
46 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
47 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
48 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
49 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
50 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
51 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
52 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
53 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
54 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
55 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
56 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
60 if (! defined $config{cvspath}) {
61 $config{cvspath}="ikiwiki";
63 if (exists $config{cvspath}) {
64 # code depends on the path not having extraneous slashes
65 $config{cvspath}=~tr#/#/#s;
66 $config{cvspath}=~s/\/$//;
67 $config{cvspath}=~s/^\///;
69 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
70 push @{$config{wrappers}}, {
71 wrapper => $config{cvs_wrapper},
72 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
80 safe => 0, # rcs plugin
86 example => "/cvs/wikirepo",
87 description => "cvs repository location",
94 description => "path inside repository where the wiki is located",
100 example => "/cvs/wikirepo/CVSROOT/post-commit",
101 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
108 description => "mode for cvs_wrapper (can safely be made suid)",
114 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
115 description => "cvsweb url to show file history ([[file]] substituted)",
121 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
122 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
132 for (j = 1; j < argc; j++)
133 if (strstr(argv[j], "New directory") != NULL)
140 # VCS PLUGIN API CALLS
143 return unless cvs_is_controlling();
144 cvs_runcvs('update', '-dP');
147 sub rcs_prepedit ($) {
148 # Prepares to edit a file under revision control. Returns a token
149 # that must be passed into rcs_commit when the file is ready
151 # The file is relative to the srcdir.
154 return unless cvs_is_controlling();
156 # For cvs, return the revision of the file when
158 my $rev=cvs_info("Repository revision", "$file");
159 return defined $rev ? $rev : "";
163 # Tries to commit the page; returns undef on _success_ and
164 # a version of the page with the rcs's conflict markers on failure.
165 # The file is relative to the srcdir.
168 return unless cvs_is_controlling();
170 # Check to see if the page has been changed by someone
171 # else since rcs_prepedit was called.
172 my ($oldrev)=$params{token}=~/^([0-9]+)$/; # untaint
173 my $rev=cvs_info("Repository revision", "$config{srcdir}/$params{file}");
174 if (defined $rev && defined $oldrev && $rev != $oldrev) {
175 # Merge their changes into the file that we've
177 cvs_runcvs('update', $params{file}) ||
178 warn("cvs merge from $oldrev to $rev failed\n");
181 if (! cvs_runcvs('commit', '-m',
182 IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
183 my $conflict=readfile("$config{srcdir}/$params{file}");
184 cvs_runcvs('update', '-C', $params{file}) ||
185 warn("cvs revert failed\n");
189 return undef # success
192 sub rcs_commit_staged (@) {
193 # Commits all staged changes. Changes can be staged using rcs_add,
194 # rcs_remove, and rcs_rename.
197 if (! cvs_runcvs('commit', '-m',
198 IkiWiki::possibly_foolish_untaint(commitmessage(%params)))) {
199 warn "cvs staged commit failed\n";
202 return undef # success
206 # filename is relative to the root of the srcdir
208 my $parent=IkiWiki::dirname($file);
209 my @files_to_add = ($file);
211 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
212 push @files_to_add, $parent;
213 $parent = IkiWiki::dirname($parent);
216 while ($file = pop @files_to_add) {
217 if (@files_to_add == 0) {
219 cvs_runcvs('add', cvs_keyword_subst_args($file)) ||
220 warn("cvs add $file failed\n");
224 cvs_runcvs('add', $file) ||
225 warn("cvs add $file failed\n");
231 # filename is relative to the root of the srcdir
234 return unless cvs_is_controlling();
236 cvs_runcvs('rm', '-f', $file) ||
237 warn("cvs rm $file failed\n");
240 sub rcs_rename ($$) {
241 # filenames relative to the root of the srcdir
244 return unless cvs_is_controlling();
246 local $CWD = $config{srcdir};
248 if (system("mv", "$src", "$dest") != 0) {
249 warn("filesystem rename failed\n");
256 sub rcs_recentchanges ($) {
260 return unless cvs_is_controlling();
262 eval q{use Date::Parse};
265 local $CWD = $config{srcdir};
267 # There's no cvsps option to get the last N changesets.
268 # Write full output to a temp file and read backwards.
270 eval q{use File::Temp qw/tempfile/};
272 eval q{use File::ReadBackwards};
275 my ($tmphandle, $tmpfile) = tempfile();
276 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
278 error "couldn't run cvsps: $!\n";
280 elsif (($? >> 8) != 0) {
281 error "cvsps exited " . ($? >> 8) . ": $!\n";
284 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
285 || error "couldn't open $tmpfile for read: $!\n";
287 while (my $line = <SPSVC>) {
288 $line =~ /^$/ || error "expected blank line, got $line";
290 my ($rev, $user, $committype, $when);
291 my (@message, @pages);
293 # We're reading backwards.
294 # Forwards, an entry looks like so:
295 # ---------------------
298 # Author: $user (or user CGI runs as, for web commits)
304 # @pages (and revisions)
307 while ($line = <SPSVC>) {
308 last if ($line =~ /^Members:/);
313 my ($page, $revs) = split(/:/, $line);
314 my ($oldrev, $newrev) = split(/->/, $revs);
315 $oldrev =~ s/INITIAL/0/;
316 $newrev =~ s/\(DEAD\)//;
317 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
318 $diffurl=~s/\[\[file\]\]/$page/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 while (my $line = shift @cvsps) {
398 $blank_lines_seen++ if ($line =~ /^$/);
399 last if $blank_lines_seen == 2;
406 return join("", @cvsps);
410 sub rcs_getctime ($) {
413 local $CWD = $config{srcdir};
415 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
417 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
418 || error "couldn't get cvs log output: $!\n";
422 if (/$cvs_log_infoline/) {
426 close CVSLOG || warn "cvs log $file exited $?";
428 if (! defined $date) {
429 warn "failed to parse cvs log for $file\n";
433 eval q{use Date::Parse};
435 $date=str2time($date, 'UTC');
436 debug("found ctime ".localtime($date)." for $file");
440 sub rcs_getmtime ($) {
441 error "rcs_getmtime is not implemented for cvs\n"; # TODO
445 # INTERNAL SUPPORT ROUTINES
447 sub commitmessage (@) {
450 if (defined $params{session}) {
451 if (defined $params{session}->param("name")) {
452 return "web commit by ".
453 $params{session}->param("name").
454 (length $params{message} ? ": $params{message}" : "");
456 elsif (defined $params{session}->remote_addr()) {
457 return "web commit from ".
458 $params{session}->remote_addr().
459 (length $params{message} ? ": $params{message}" : "");
462 return $params{message};
469 local $CWD = $config{srcdir};
471 my $info=`cvs status $file`;
472 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
476 sub cvs_is_controlling {
478 $dir=$config{srcdir} unless defined($dir);
479 return (-d "$dir/CVS") ? 1 : 0;
482 sub cvs_keyword_subst_args ($) {
485 local $CWD = $config{srcdir};
487 eval q{use File::MimeInfo};
489 my $filemime = File::MimeInfo::default($file);
492 if (defined($filemime) && $filemime eq 'text/plain') {
496 return ('-kb', $file);
502 unshift @cmd, 'cvs', '-Q';
504 local $CWD = $config{srcdir};
506 open(my $savedout, ">&STDOUT");
507 open(STDOUT, ">", "/dev/null");
508 my $ret = system(@cmd);
509 open(STDOUT, ">&", $savedout);
511 return ($ret == 0) ? 1 : 0;