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 commitmessage
(@
) {
189 if (defined $params{session
}) {
190 if (defined $params{session
}->param("name")) {
191 return "web commit by ".
192 $params{session
}->param("name").
193 (length $params{message
} ?
": $params{message}" : "");
195 elsif (defined $params{session
}->remote_addr()) {
196 return "web commit from ".
197 $params{session
}->remote_addr().
198 (length $params{message
} ?
": $params{message}" : "");
201 return $params{message
};
205 # Tries to commit the page; returns undef on _success_ and
206 # a version of the page with the rcs's conflict markers on failure.
207 # The file is relative to the srcdir.
210 return unless cvs_is_controlling
;
212 # Check to see if the page has been changed by someone
213 # else since rcs_prepedit was called.
214 my ($oldrev)=$params{token
}=~/^([0-9]+)$/; # untaint
215 my $rev=cvs_info
("Repository revision", "$config{srcdir}/$params{file}");
216 if (defined $rev && defined $oldrev && $rev != $oldrev) {
217 # Merge their changes into the file that we've
219 cvs_runcvs
('update', $params{file
}) ||
220 warn("cvs merge from $oldrev to $rev failed\n");
223 if (! cvs_runcvs
('commit', '-m',
224 IkiWiki
::possibly_foolish_untaint
(commitmessage
(%params)))) {
225 my $conflict=readfile
("$config{srcdir}/$params{file}");
226 cvs_runcvs
('update', '-C', $params{file
}) ||
227 warn("cvs revert failed\n");
231 return undef # success
234 sub rcs_commit_staged
(@
) {
235 # Commits all staged changes. Changes can be staged using rcs_add,
236 # rcs_remove, and rcs_rename.
239 if (! cvs_runcvs
('commit', '-m',
240 IkiWiki
::possibly_foolish_untaint
(commitmessage
(%params)))) {
241 warn "cvs staged commit failed\n";
244 return undef # success
248 # filename is relative to the root of the srcdir
250 my $parent=IkiWiki
::dirname
($file);
251 my @files_to_add = ($file);
253 eval q{use File::MimeInfo};
256 until ((length($parent) == 0) || cvs_is_controlling
("$config{srcdir}/$parent")){
257 push @files_to_add, $parent;
258 $parent = IkiWiki
::dirname
($parent);
261 while ($file = pop @files_to_add) {
262 if (@files_to_add == 0) {
264 my $filemime = File
::MimeInfo
::default($file);
265 if (defined($filemime) && $filemime eq 'text/plain') {
266 cvs_runcvs
('add', $file) ||
267 warn("cvs add $file failed\n");
270 cvs_runcvs
('add', '-kb', $file) ||
271 warn("cvs add binary $file failed\n");
276 cvs_runcvs
('add', $file) ||
277 warn("cvs add $file failed\n");
283 # filename is relative to the root of the srcdir
286 return unless cvs_is_controlling
;
288 cvs_runcvs
('rm', '-f', $file) ||
289 warn("cvs rm $file failed\n");
292 sub rcs_rename
($$) {
293 # filenames relative to the root of the srcdir
296 return unless cvs_is_controlling
;
298 local $CWD = $config{srcdir
};
300 if (system("mv", "$src", "$dest") != 0) {
301 warn("filesystem rename failed\n");
308 sub rcs_recentchanges
($) {
312 return unless cvs_is_controlling
;
314 eval q{use Date::Parse};
317 local $CWD = $config{srcdir
};
319 # There's no cvsps option to get the last N changesets.
320 # Write full output to a temp file and read backwards.
322 eval q{use File::Temp qw/tempfile/};
324 eval q{use File::ReadBackwards};
327 my ($tmphandle, $tmpfile) = tempfile
();
328 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
330 error
"couldn't run cvsps: $!\n";
332 elsif (($?
>> 8) != 0) {
333 error
"cvsps exited " . ($?
>> 8) . ": $!\n";
336 tie
(*SPSVC
, 'File::ReadBackwards', $tmpfile)
337 || error
"couldn't open $tmpfile for read: $!\n";
339 while (my $line = <SPSVC
>) {
340 $line =~ /^$/ || error
"expected blank line, got $line";
342 my ($rev, $user, $committype, $when);
343 my (@message, @pages);
345 # We're reading backwards.
346 # Forwards, an entry looks like so:
347 # ---------------------
350 # Author: $user (or user CGI runs as, for web commits)
356 # @pages (and revisions)
359 while ($line = <SPSVC
>) {
360 last if ($line =~ /^Members:/);
365 my ($page, $revs) = split(/:/, $line);
366 my ($oldrev, $newrev) = split(/->/, $revs);
367 $oldrev =~ s/INITIAL/0/;
368 $newrev =~ s/\(DEAD\)//;
369 my $diffurl = defined $config{diffurl
} ?
$config{diffurl
} : "";
370 $diffurl=~s/\[\[file\]\]/$page/g;
371 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
372 $diffurl=~s/\[\[r2\]\]/$newrev/g;
374 page
=> pagename
($page),
379 while ($line = <SPSVC
>) {
380 last if ($line =~ /^Log:$/);
382 unshift @message, { line
=> $line };
385 if (defined $message[0] &&
386 $message[0]->{line
}=~/$config{web_commit_regexp}/) {
387 $user=defined $2 ?
"$2" : "$3";
388 $message[0]->{line
}=$4;
394 $line = <SPSVC
>; # Tag
395 $line = <SPSVC
>; # Branch
398 if ($line =~ /^Author: (.*)$/) {
399 $user = $1 unless defined $user && length $user;
402 error
"expected Author, got $line";
406 if ($line =~ /^Date: (.*)$/) {
407 $when = str2time
($1, 'UTC');
410 error
"expected Date, got $line";
414 if ($line =~ /^PatchSet (.*)$/) {
418 error
"expected PatchSet, got $line";
421 $line = <SPSVC
>; # ---------------------
426 committype
=> $committype,
428 message
=> [@message],
431 last if @ret >= $num;
434 unlink($tmpfile) || error
"couldn't unlink $tmpfile: $!\n";
440 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 local $CWD = $config{srcdir
};
467 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
469 open CVSLOG
, "cvs -Q log -r1.1 '$file' |"
470 || error
"couldn't get cvs log output: $!\n";
474 if (/$cvs_log_infoline/) {
478 close CVSLOG
|| warn "cvs log $file exited $?";
480 if (! defined $date) {
481 warn "failed to parse cvs log for $file\n";
485 eval q{use Date::Parse};
487 $date=str2time
($date, 'UTC');
488 debug
("found ctime ".localtime($date)." for $file");
492 sub rcs_getmtime
($) {
493 error
"rcs_getmtime is not implemented for cvs\n"; # TODO