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) {
220 cvs_runcvs('add', cvs_keyword_subst_args($file)) ||
221 warn("cvs add $file failed\n");
225 cvs_runcvs('add', $file) ||
226 warn("cvs add $file failed\n");
232 # filename is relative to the root of the srcdir
235 return unless cvs_is_controlling();
237 cvs_runcvs('rm', '-f', $file) ||
238 warn("cvs rm $file failed\n");
241 sub rcs_rename ($$) {
242 # filenames relative to the root of the srcdir
245 return unless cvs_is_controlling();
247 local $CWD = $config{srcdir};
249 if (system("mv", "$src", "$dest") != 0) {
250 warn("filesystem rename failed\n");
257 sub rcs_recentchanges ($) {
261 return unless cvs_is_controlling();
263 eval q{use Date::Parse};
266 local $CWD = $config{srcdir};
268 # There's no cvsps option to get the last N changesets.
269 # Write full output to a temp file and read backwards.
271 eval q{use File::Temp qw/tempfile/};
273 eval q{use File::ReadBackwards};
276 my ($tmphandle, $tmpfile) = tempfile();
277 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
279 error "couldn't run cvsps: $!\n";
281 elsif (($? >> 8) != 0) {
282 error "cvsps exited " . ($? >> 8) . ": $!\n";
285 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
286 || error "couldn't open $tmpfile for read: $!\n";
288 while (my $line = <SPSVC>) {
289 $line =~ /^$/ || error "expected blank line, got $line";
291 my ($rev, $user, $committype, $when);
292 my (@message, @pages);
294 # We're reading backwards.
295 # Forwards, an entry looks like so:
296 # ---------------------
299 # Author: $user (or user CGI runs as, for web commits)
305 # @pages (and revisions)
308 while ($line = <SPSVC>) {
309 last if ($line =~ /^Members:/);
314 my ($page, $revs) = split(/:/, $line);
315 my ($oldrev, $newrev) = split(/->/, $revs);
316 $oldrev =~ s/INITIAL/0/;
317 $newrev =~ s/\(DEAD\)//;
318 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
319 my $epage = uri_escape_utf8($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 while (my $line = shift @cvsps) {
400 $blank_lines_seen++ if ($line =~ /^$/);
401 last if $blank_lines_seen == 2;
408 return join("", @cvsps);
412 sub rcs_getctime ($) {
415 local $CWD = $config{srcdir};
417 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
419 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
420 || error "couldn't get cvs log output: $!\n";
424 if (/$cvs_log_infoline/) {
428 close CVSLOG || warn "cvs log $file exited $?";
430 if (! defined $date) {
431 warn "failed to parse cvs log for $file\n";
435 eval q{use Date::Parse};
437 $date=str2time($date, 'UTC');
438 debug("found ctime ".localtime($date)." for $file");
442 sub rcs_getmtime ($) {
443 error "rcs_getmtime is not implemented for cvs\n"; # TODO
447 # INTERNAL SUPPORT ROUTINES
449 sub commitmessage (@) {
452 if (defined $params{session}) {
453 if (defined $params{session}->param("name")) {
454 return "web commit by ".
455 $params{session}->param("name").
456 (length $params{message} ? ": $params{message}" : "");
458 elsif (defined $params{session}->remote_addr()) {
459 return "web commit from ".
460 $params{session}->remote_addr().
461 (length $params{message} ? ": $params{message}" : "");
464 return $params{message};
471 local $CWD = $config{srcdir};
473 my $info=`cvs status $file`;
474 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
478 sub cvs_is_controlling {
480 $dir=$config{srcdir} unless defined($dir);
481 return (-d "$dir/CVS") ? 1 : 0;
484 sub cvs_keyword_subst_args ($) {
487 local $CWD = $config{srcdir};
489 eval q{use File::MimeInfo};
491 my $filemime = File::MimeInfo::default($file);
494 if (defined($filemime) && $filemime eq 'text/plain') {
498 return ('-kb', $file);
504 unshift @cmd, 'cvs', '-Q';
506 local $CWD = $config{srcdir};
508 open(my $savedout, ">&STDOUT");
509 open(STDOUT, ">", "/dev/null");
510 my $ret = system(@cmd);
511 open(STDOUT, ">&", $savedout);
513 return ($ret == 0) ? 1 : 0;