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 ($) {
122 my $test_message = "CVS autodiscover quoting CVS";
124 eval q{use String::ShellQuote};
126 eval q{use IPC::Cmd};
129 my $cmd = ['echo', shell_quote($test_message)];
130 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
131 IPC::Cmd::run(command => $cmd, verbose => 0);
132 if ((grep /'$test_message'/, @$stdout_buf) > 0) {
133 return IkiWiki::possibly_foolish_untaint($message);
135 return shell_quote(IkiWiki::possibly_foolish_untaint($message));
139 sub cvs_is_controlling {
141 $dir=$config{srcdir} unless defined($dir);
142 return (-d "$dir/CVS") ? 1 : 0;
146 return unless cvs_is_controlling;
147 cvs_runcvs(['update', '-dP']);
150 sub rcs_prepedit ($) {
151 # Prepares to edit a file under revision control. Returns a token
152 # that must be passed into rcs_commit when the file is ready
154 # The file is relative to the srcdir.
157 return unless cvs_is_controlling;
159 # For cvs, return the revision of the file when
161 my $rev=cvs_info("Repository revision", "$file");
162 return defined $rev ? $rev : "";
165 sub rcs_commit ($$$;$$) {
166 # Tries to commit the page; returns undef on _success_ and
167 # a version of the page with the rcs's conflict markers on failure.
168 # The file is relative to the srcdir.
175 return unless cvs_is_controlling;
178 $message="web commit by $user".(length $message ? ": $message" : "");
180 elsif (defined $ipaddr) {
181 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
184 # Check to see if the page has been changed by someone
185 # else since rcs_prepedit was called.
186 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
187 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
188 if (defined $rev && defined $oldrev && $rev != $oldrev) {
189 # Merge their changes into the file that we've
191 cvs_runcvs(['update', $file]) ||
192 warn("cvs merge from $oldrev to $rev failed\n");
195 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
196 my $conflict=readfile("$config{srcdir}/$file");
197 cvs_runcvs(['update', '-C', $file]) ||
198 warn("cvs revert failed\n");
202 return undef # success
205 sub rcs_commit_staged ($$$) {
206 # Commits all staged changes. Changes can be staged using rcs_add,
207 # rcs_remove, and rcs_rename.
208 my ($message, $user, $ipaddr)=@_;
211 $message="web commit by $user".(length $message ? ": $message" : "");
213 elsif (defined $ipaddr) {
214 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
217 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
218 warn "cvs staged commit failed\n";
221 return undef # success
225 # filename is relative to the root of the srcdir
227 my $parent=IkiWiki::dirname($file);
228 my @files_to_add = ($file);
230 eval q{use File::MimeInfo};
233 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
234 push @files_to_add, $parent;
235 $parent = IkiWiki::dirname($parent);
238 while ($file = pop @files_to_add) {
239 if (@files_to_add == 0) {
241 my $filemime = File::MimeInfo::default($file);
242 if (defined($filemime) && $filemime eq 'text/plain') {
243 cvs_runcvs(['add', $file]) ||
244 warn("cvs add $file failed\n");
246 cvs_runcvs(['add', '-kb', $file]) ||
247 warn("cvs add binary $file failed\n");
251 cvs_runcvs(['add', $file]) ||
252 warn("cvs add $file failed\n");
258 # filename is relative to the root of the srcdir
261 return unless cvs_is_controlling;
263 cvs_runcvs(['rm', '-f', $file]) ||
264 warn("cvs rm $file failed\n");
267 sub rcs_rename ($$) {
268 # filenames relative to the root of the srcdir
271 return unless cvs_is_controlling;
273 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
275 if (system("mv", "$src", "$dest") != 0) {
276 warn("filesystem rename failed\n");
283 sub rcs_recentchanges($) {
287 return unless cvs_is_controlling;
289 eval q{use Date::Parse};
292 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
294 # There's no cvsps option to get the last N changesets.
295 # Write full output to a temp file and read backwards.
297 eval q{use File::Temp qw/tempfile/};
299 eval q{use File::ReadBackwards};
302 my (undef, $tmpfile) = tempfile(OPEN=>0);
303 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
305 error "couldn't run cvsps: $!\n";
306 } elsif (($? >>8) != 0) {
307 error "cvsps exited " . ($? >> 8) . ": $!\n";
310 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
311 || error "couldn't open $tmpfile for read: $!\n";
313 while (my $line = <SPSVC>) {
314 $line =~ /^$/ || error "expected blank line, got $line";
316 my ($rev, $user, $committype, $when);
317 my (@message, @pages);
319 # We're reading backwards.
320 # Forwards, an entry looks like so:
321 # ---------------------
324 # Author: $user (or user CGI runs as, for web commits)
330 # @pages (and revisions)
333 while ($line = <SPSVC>) {
334 last if ($line =~ /^Members:/);
339 my ($page, $revs) = split(/:/, $line);
340 my ($oldrev, $newrev) = split(/->/, $revs);
341 $oldrev =~ s/INITIAL/0/;
342 $newrev =~ s/\(DEAD\)//;
343 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
344 $diffurl=~s/\[\[file\]\]/$page/g;
345 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
346 $diffurl=~s/\[\[r2\]\]/$newrev/g;
348 page => pagename($page),
353 while ($line = <SPSVC>) {
354 last if ($line =~ /^Log:$/);
356 unshift @message, { line => $line };
359 if (defined $message[0] &&
360 $message[0]->{line}=~/$config{web_commit_regexp}/) {
361 $user=defined $2 ? "$2" : "$3";
362 $message[0]->{line}=$4;
367 $line = <SPSVC>; # Tag
368 $line = <SPSVC>; # Branch
371 if ($line =~ /^Author: (.*)$/) {
372 $user = $1 unless defined $user && length $user;
374 error "expected Author, got $line";
378 if ($line =~ /^Date: (.*)$/) {
379 $when = str2time($1, 'UTC');
381 error "expected Date, got $line";
385 if ($line =~ /^PatchSet (.*)$/) {
388 error "expected PatchSet, got $line";
391 $line = <SPSVC>; # ---------------------
396 committype => $committype,
398 message => [@message],
401 last if @ret >= $num;
404 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
410 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
412 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
414 # diff output is unavoidably preceded by the cvsps PatchSet entry
415 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
416 my $blank_lines_seen = 0;
418 while (my $line = shift @cvsps) {
419 $blank_lines_seen++ if ($line =~ /^$/);
420 last if $blank_lines_seen == 2;
426 return join("", @cvsps);
430 sub rcs_getctime ($) {
433 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
435 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
436 || error "couldn't get cvs log output: $!\n";
440 if (/$cvs_log_infoline/) {
444 close CVSLOG || warn "cvs log $file exited $?";
446 if (! defined $date) {
447 warn "failed to parse cvs log for $file\n";
451 eval q{use Date::Parse};
453 $date=str2time($date, 'UTC');
454 debug("found ctime ".localtime($date)." for $file");