2 package IkiWiki::Plugin::cvs;
9 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
10 hook(type => "getsetup", id => "cvs", call => \&getsetup);
11 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
12 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
13 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
14 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
15 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
16 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
17 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
18 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
19 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
20 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
24 if (! defined $config{cvspath}) {
25 $config{cvspath}="ikiwiki";
27 if (exists $config{cvspath}) {
28 # code depends on the path not having extraneous slashes
29 $config{cvspath}=~tr#/#/#s;
30 $config{cvspath}=~s/\/$//;
31 $config{cvspath}=~s/^\///;
33 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
34 push @{$config{wrappers}}, {
35 wrapper => $config{cvs_wrapper},
36 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
44 safe => 0, # rcs plugin
49 example => "/cvs/wikirepo",
50 description => "cvs repository location",
57 description => "path inside repository where the wiki is located",
63 example => "/cvs/wikirepo/CVSROOT/post-commit",
64 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry",
71 description => "mode for cvs_wrapper (can safely be made suid)",
77 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
78 description => "cvsweb url to show file history ([[file]] substituted)",
84 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
85 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
95 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
97 my $info=`cvs status $file`;
98 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
104 unshift @$cmd, 'cvs', '-Q';
106 eval q{use IPC::Cmd};
109 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
111 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
112 IPC::Cmd::run(command => $cmd, verbose => 0);
114 warn(join(" ", @$cmd) . " exited with code $error_code\n");
115 warn(join "", @$stderr_buf);
120 sub cvs_shquote_commit ($) {
123 eval q{use String::ShellQuote};
126 return shell_quote(IkiWiki::possibly_foolish_untaint($message));
129 sub cvs_is_controlling {
131 $dir=$config{srcdir} unless defined($dir);
132 return (-d "$dir/CVS") ? 1 : 0;
136 return unless cvs_is_controlling;
137 cvs_runcvs(['update', '-dP']);
140 sub rcs_prepedit ($) {
141 # Prepares to edit a file under revision control. Returns a token
142 # that must be passed into rcs_commit when the file is ready
144 # The file is relative to the srcdir.
147 return unless cvs_is_controlling;
149 # For cvs, return the revision of the file when
151 my $rev=cvs_info("Repository revision", "$file");
152 return defined $rev ? $rev : "";
155 sub rcs_commit ($$$;$$) {
156 # Tries to commit the page; returns undef on _success_ and
157 # a version of the page with the rcs's conflict markers on failure.
158 # The file is relative to the srcdir.
165 return unless cvs_is_controlling;
168 $message="web commit by $user".(length $message ? ": $message" : "");
170 elsif (defined $ipaddr) {
171 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
174 # Check to see if the page has been changed by someone
175 # else since rcs_prepedit was called.
176 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
177 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
178 if (defined $rev && defined $oldrev && $rev != $oldrev) {
179 # Merge their changes into the file that we've
181 cvs_runcvs(['update', $file]) ||
182 warn("cvs merge from $oldrev to $rev failed\n");
185 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
186 my $conflict=readfile("$config{srcdir}/$file");
187 cvs_runcvs(['update', '-C', $file]) ||
188 warn("cvs revert failed\n");
192 return undef # success
195 sub rcs_commit_staged ($$$) {
196 # Commits all staged changes. Changes can be staged using rcs_add,
197 # rcs_remove, and rcs_rename.
198 my ($message, $user, $ipaddr)=@_;
201 $message="web commit by $user".(length $message ? ": $message" : "");
203 elsif (defined $ipaddr) {
204 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
207 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
208 warn "cvs staged commit failed\n";
211 return undef # success
215 # filename is relative to the root of the srcdir
217 my $parent=IkiWiki::dirname($file);
218 my @files_to_add = ($file);
220 eval q{use File::MimeInfo};
223 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
224 push @files_to_add, $parent;
225 $parent = IkiWiki::dirname($parent);
228 while ($file = pop @files_to_add) {
229 if ((@files_to_add == 0) &&
230 (File::MimeInfo::default $file ne 'text/plain')) {
231 # it's a binary file, add specially
232 cvs_runcvs(['add', '-kb', $file]) ||
233 warn("cvs add $file failed\n");
235 # directory or regular file
236 cvs_runcvs(['add', $file]) ||
237 warn("cvs add $file failed\n");
243 # filename is relative to the root of the srcdir
246 return unless cvs_is_controlling;
248 cvs_runcvs(['rm', '-f', $file]) ||
249 warn("cvs rm $file failed\n");
252 sub rcs_rename ($$) {
253 # filenames relative to the root of the srcdir
256 return unless cvs_is_controlling;
258 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
260 if (system("mv", "$src", "$dest") != 0) {
261 warn("filesystem rename failed\n");
268 sub rcs_recentchanges($) {
272 return unless cvs_is_controlling;
274 eval q{use Date::Parse};
277 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
279 # There's no cvsps option to get the last N changesets.
280 # Write full output to a temp file and read backwards.
282 eval q{use File::Temp qw/tempfile/};
284 eval q{use File::ReadBackwards};
287 my (undef, $tmpfile) = tempfile(OPEN=>0);
288 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
290 error "couldn't run cvsps: $!\n";
291 } elsif (($? >>8) != 0) {
292 error "cvsps exited " . ($? >> 8) . ": $!\n";
295 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
296 || error "couldn't open $tmpfile for read: $!\n";
298 while (my $line = <SPSVC>) {
299 $line =~ /^$/ || error "expected blank line, got $line";
301 my ($rev, $user, $committype, $when);
302 my (@message, @pages);
304 # We're reading backwards.
305 # Forwards, an entry looks like so:
306 # ---------------------
309 # Author: $user (or user CGI runs as, for web commits)
315 # @pages (and revisions)
318 while ($line = <SPSVC>) {
319 last if ($line =~ /^Members:/);
324 my ($page, $revs) = split(/:/, $line);
325 my ($oldrev, $newrev) = split(/->/, $revs);
326 $oldrev =~ s/INITIAL/0/;
327 $newrev =~ s/\(DEAD\)//;
328 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
329 $diffurl=~s/\[\[file\]\]/$page/g;
330 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
331 $diffurl=~s/\[\[r2\]\]/$newrev/g;
333 page => pagename($page),
338 while ($line = <SPSVC>) {
339 last if ($line =~ /^Log:$/);
341 unshift @message, { line => $line };
344 if (defined $message[0] &&
345 $message[0]->{line}=~/$config{web_commit_regexp}/) {
346 $user=defined $2 ? "$2" : "$3";
347 $message[0]->{line}=$4;
352 $line = <SPSVC>; # Tag
353 $line = <SPSVC>; # Branch
356 if ($line =~ /^Author: (.*)$/) {
357 $user = $1 unless defined $user && length $user;
359 error "expected Author, got $line";
363 if ($line =~ /^Date: (.*)$/) {
364 $when = str2time($1, 'UTC');
366 error "expected Date, got $line";
370 if ($line =~ /^PatchSet (.*)$/) {
373 error "expected PatchSet, got $line";
376 $line = <SPSVC>; # ---------------------
381 committype => $committype,
383 message => [@message],
386 last if @ret >= $num;
389 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
395 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
397 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
399 # diff output is unavoidably preceded by the cvsps PatchSet entry
400 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
401 my $blank_lines_seen = 0;
403 while (my $line = shift @cvsps) {
404 $blank_lines_seen++ if ($line =~ /^$/);
405 last if $blank_lines_seen == 2;
411 return join("", @cvsps);
415 sub rcs_getctime ($) {
418 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
420 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
421 || error "couldn't get cvs log output: $!\n";
425 if (/$cvs_log_infoline/) {
429 close CVSLOG || warn "cvs log $file exited $?";
431 if (! defined $date) {
432 warn "failed to parse cvs log for $file\n";
436 eval q{use Date::Parse};
438 $date=str2time($date, 'UTC');
439 debug("found ctime ".localtime($date)." for $file");