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);
58 for (j = 1; j < argc; j++)
59 if (strstr(argv[j], "New directory") != NULL)
66 if (! defined $config{cvspath}) {
67 $config{cvspath}="ikiwiki";
69 if (exists $config{cvspath}) {
70 # code depends on the path not having extraneous slashes
71 $config{cvspath}=~tr#/#/#s;
72 $config{cvspath}=~s/\/$//;
73 $config{cvspath}=~s/^\///;
75 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
76 push @{$config{wrappers}}, {
77 wrapper => $config{cvs_wrapper},
78 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
86 safe => 0, # rcs plugin
92 example => "/cvs/wikirepo",
93 description => "cvs repository location",
100 description => "path inside repository where the wiki is located",
101 safe => 0, # paranoia
106 example => "/cvs/wikirepo/CVSROOT/post-commit",
107 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
114 description => "mode for cvs_wrapper (can safely be made suid)",
120 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
121 description => "cvsweb url to show file history ([[file]] substituted)",
127 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
128 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
138 local $CWD = $config{srcdir};
140 my $info=`cvs status $file`;
141 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
147 unshift @cmd, 'cvs', '-Q';
149 local $CWD = $config{srcdir};
151 open(my $savedout, ">&STDOUT");
152 open(STDOUT, ">", "/dev/null");
153 my $ret = system(@cmd);
154 open(STDOUT, ">&", $savedout);
156 return ($ret == 0) ? 1 : 0;
159 sub cvs_is_controlling {
161 $dir=$config{srcdir} unless defined($dir);
162 return (-d "$dir/CVS") ? 1 : 0;
166 return unless cvs_is_controlling;
167 cvs_runcvs('update', '-dP');
170 sub rcs_prepedit ($) {
171 # Prepares to edit a file under revision control. Returns a token
172 # that must be passed into rcs_commit when the file is ready
174 # The file is relative to the srcdir.
177 return unless cvs_is_controlling;
179 # For cvs, return the revision of the file when
181 my $rev=cvs_info("Repository revision", "$file");
182 return defined $rev ? $rev : "";
185 sub rcs_commit ($$$;$$) {
186 # Tries to commit the page; returns undef on _success_ and
187 # a version of the page with the rcs's conflict markers on failure.
188 # The file is relative to the srcdir.
195 return unless cvs_is_controlling;
198 $message="web commit by $user".(length $message ? ": $message" : "");
200 elsif (defined $ipaddr) {
201 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
204 # Check to see if the page has been changed by someone
205 # else since rcs_prepedit was called.
206 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
207 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
208 if (defined $rev && defined $oldrev && $rev != $oldrev) {
209 # Merge their changes into the file that we've
211 cvs_runcvs('update', $file) ||
212 warn("cvs merge from $oldrev to $rev failed\n");
215 if (! cvs_runcvs('commit', '-m',
216 IkiWiki::possibly_foolish_untaint $message)) {
217 my $conflict=readfile("$config{srcdir}/$file");
218 cvs_runcvs('update', '-C', $file) ||
219 warn("cvs revert failed\n");
223 return undef # success
226 sub rcs_commit_staged ($$$) {
227 # Commits all staged changes. Changes can be staged using rcs_add,
228 # rcs_remove, and rcs_rename.
229 my ($message, $user, $ipaddr)=@_;
232 $message="web commit by $user".(length $message ? ": $message" : "");
234 elsif (defined $ipaddr) {
235 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
238 if (! cvs_runcvs('commit', '-m',
239 IkiWiki::possibly_foolish_untaint $message)) {
240 warn "cvs staged commit failed\n";
243 return undef # success
247 # filename is relative to the root of the srcdir
249 my $parent=IkiWiki::dirname($file);
250 my @files_to_add = ($file);
252 eval q{use File::MimeInfo};
255 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
256 push @files_to_add, $parent;
257 $parent = IkiWiki::dirname($parent);
260 while ($file = pop @files_to_add) {
261 if (@files_to_add == 0) {
263 my $filemime = File::MimeInfo::default($file);
264 if (defined($filemime) && $filemime eq 'text/plain') {
265 cvs_runcvs('add', $file) ||
266 warn("cvs add $file failed\n");
269 cvs_runcvs('add', '-kb', $file) ||
270 warn("cvs add binary $file failed\n");
275 cvs_runcvs('add', $file) ||
276 warn("cvs add $file failed\n");
282 # filename is relative to the root of the srcdir
285 return unless cvs_is_controlling;
287 cvs_runcvs('rm', '-f', $file) ||
288 warn("cvs rm $file failed\n");
291 sub rcs_rename ($$) {
292 # filenames relative to the root of the srcdir
295 return unless cvs_is_controlling;
297 local $CWD = $config{srcdir};
299 if (system("mv", "$src", "$dest") != 0) {
300 warn("filesystem rename failed\n");
307 sub rcs_recentchanges($) {
311 return unless cvs_is_controlling;
313 eval q{use Date::Parse};
316 local $CWD = $config{srcdir};
318 # There's no cvsps option to get the last N changesets.
319 # Write full output to a temp file and read backwards.
321 eval q{use File::Temp qw/tempfile/};
323 eval q{use File::ReadBackwards};
326 my ($tmphandle, $tmpfile) = tempfile();
327 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
329 error "couldn't run cvsps: $!\n";
331 elsif (($? >> 8) != 0) {
332 error "cvsps exited " . ($? >> 8) . ": $!\n";
335 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
336 || error "couldn't open $tmpfile for read: $!\n";
338 while (my $line = <SPSVC>) {
339 $line =~ /^$/ || error "expected blank line, got $line";
341 my ($rev, $user, $committype, $when);
342 my (@message, @pages);
344 # We're reading backwards.
345 # Forwards, an entry looks like so:
346 # ---------------------
349 # Author: $user (or user CGI runs as, for web commits)
355 # @pages (and revisions)
358 while ($line = <SPSVC>) {
359 last if ($line =~ /^Members:/);
364 my ($page, $revs) = split(/:/, $line);
365 my ($oldrev, $newrev) = split(/->/, $revs);
366 $oldrev =~ s/INITIAL/0/;
367 $newrev =~ s/\(DEAD\)//;
368 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
369 $diffurl=~s/\[\[file\]\]/$page/g;
370 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
371 $diffurl=~s/\[\[r2\]\]/$newrev/g;
373 page => pagename($page),
378 while ($line = <SPSVC>) {
379 last if ($line =~ /^Log:$/);
381 unshift @message, { line => $line };
384 if (defined $message[0] &&
385 $message[0]->{line}=~/$config{web_commit_regexp}/) {
386 $user=defined $2 ? "$2" : "$3";
387 $message[0]->{line}=$4;
393 $line = <SPSVC>; # Tag
394 $line = <SPSVC>; # Branch
397 if ($line =~ /^Author: (.*)$/) {
398 $user = $1 unless defined $user && length $user;
401 error "expected Author, got $line";
405 if ($line =~ /^Date: (.*)$/) {
406 $when = str2time($1, 'UTC');
409 error "expected Date, got $line";
413 if ($line =~ /^PatchSet (.*)$/) {
417 error "expected PatchSet, got $line";
420 $line = <SPSVC>; # ---------------------
425 committype => $committype,
427 message => [@message],
430 last if @ret >= $num;
433 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
439 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
441 local $CWD = $config{srcdir};
443 # diff output is unavoidably preceded by the cvsps PatchSet entry
444 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
445 my $blank_lines_seen = 0;
447 while (my $line = shift @cvsps) {
448 $blank_lines_seen++ if ($line =~ /^$/);
449 last if $blank_lines_seen == 2;
456 return join("", @cvsps);
460 sub rcs_getctime ($) {
463 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
465 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
466 || error "couldn't get cvs log output: $!\n";
470 if (/$cvs_log_infoline/) {
474 close CVSLOG || warn "cvs log $file exited $?";
476 if (! defined $date) {
477 warn "failed to parse cvs log for $file\n";
481 eval q{use Date::Parse};
483 $date=str2time($date, 'UTC');
484 debug("found ctime ".localtime($date)." for $file");