2 package IkiWiki::Plugin::cvs;
11 hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
12 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
13 hook(type => "getsetup", id => "cvs", call => \&getsetup);
14 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
15 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
16 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
17 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
18 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
19 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
20 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
21 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
22 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
23 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
30 for (j = 1; j < argc; j++)
31 if (strstr(argv[j], "New directory") != NULL)
38 if (! defined $config{cvspath}) {
39 $config{cvspath}="ikiwiki";
41 if (exists $config{cvspath}) {
42 # code depends on the path not having extraneous slashes
43 $config{cvspath}=~tr#/#/#s;
44 $config{cvspath}=~s/\/$//;
45 $config{cvspath}=~s/^\///;
47 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
48 push @{$config{wrappers}}, {
49 wrapper => $config{cvs_wrapper},
50 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
58 safe => 0, # rcs plugin
63 example => "/cvs/wikirepo",
64 description => "cvs repository location",
71 description => "path inside repository where the wiki is located",
77 example => "/cvs/wikirepo/CVSROOT/post-commit",
78 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
85 description => "mode for cvs_wrapper (can safely be made suid)",
91 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
92 description => "cvsweb url to show file history ([[file]] substituted)",
98 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
99 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
109 local $CWD = $config{srcdir};
111 my $info=`cvs status $file`;
112 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
118 unshift @cmd, 'cvs', '-Q';
120 local $CWD = $config{srcdir};
122 open(my $savedout, ">&STDOUT");
123 open(STDOUT, ">", "/dev/null");
124 my $ret = system(@cmd);
125 open(STDOUT, ">&", $savedout);
127 return ($ret == 0) ? 1 : 0;
130 sub cvs_is_controlling {
132 $dir=$config{srcdir} unless defined($dir);
133 return (-d "$dir/CVS") ? 1 : 0;
137 return unless cvs_is_controlling;
138 cvs_runcvs('update', '-dP');
141 sub rcs_prepedit ($) {
142 # Prepares to edit a file under revision control. Returns a token
143 # that must be passed into rcs_commit when the file is ready
145 # The file is relative to the srcdir.
148 return unless cvs_is_controlling;
150 # For cvs, return the revision of the file when
152 my $rev=cvs_info("Repository revision", "$file");
153 return defined $rev ? $rev : "";
156 sub rcs_commit ($$$;$$) {
157 # Tries to commit the page; returns undef on _success_ and
158 # a version of the page with the rcs's conflict markers on failure.
159 # The file is relative to the srcdir.
166 return unless cvs_is_controlling;
169 $message="web commit by $user".(length $message ? ": $message" : "");
171 elsif (defined $ipaddr) {
172 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
175 # Check to see if the page has been changed by someone
176 # else since rcs_prepedit was called.
177 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
178 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
179 if (defined $rev && defined $oldrev && $rev != $oldrev) {
180 # Merge their changes into the file that we've
182 cvs_runcvs('update', $file) ||
183 warn("cvs merge from $oldrev to $rev failed\n");
186 if (! cvs_runcvs('commit', '-m',
187 IkiWiki::possibly_foolish_untaint $message)) {
188 my $conflict=readfile("$config{srcdir}/$file");
189 cvs_runcvs('update', '-C', $file) ||
190 warn("cvs revert failed\n");
194 return undef # success
197 sub rcs_commit_staged ($$$) {
198 # Commits all staged changes. Changes can be staged using rcs_add,
199 # rcs_remove, and rcs_rename.
200 my ($message, $user, $ipaddr)=@_;
203 $message="web commit by $user".(length $message ? ": $message" : "");
205 elsif (defined $ipaddr) {
206 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
209 if (! cvs_runcvs('commit', '-m',
210 IkiWiki::possibly_foolish_untaint $message)) {
211 warn "cvs staged commit failed\n";
214 return undef # success
218 # filename is relative to the root of the srcdir
220 my $parent=IkiWiki::dirname($file);
221 my @files_to_add = ($file);
223 eval q{use File::MimeInfo};
226 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
227 push @files_to_add, $parent;
228 $parent = IkiWiki::dirname($parent);
231 while ($file = pop @files_to_add) {
232 if (@files_to_add == 0) {
234 my $filemime = File::MimeInfo::default($file);
235 if (defined($filemime) && $filemime eq 'text/plain') {
236 cvs_runcvs('add', $file) ||
237 warn("cvs add $file failed\n");
240 cvs_runcvs('add', '-kb', $file) ||
241 warn("cvs add binary $file failed\n");
246 cvs_runcvs('add', $file) ||
247 warn("cvs add $file failed\n");
253 # filename is relative to the root of the srcdir
256 return unless cvs_is_controlling;
258 cvs_runcvs('rm', '-f', $file) ||
259 warn("cvs rm $file failed\n");
262 sub rcs_rename ($$) {
263 # filenames relative to the root of the srcdir
266 return unless cvs_is_controlling;
268 local $CWD = $config{srcdir};
270 if (system("mv", "$src", "$dest") != 0) {
271 warn("filesystem rename failed\n");
278 sub rcs_recentchanges($) {
282 return unless cvs_is_controlling;
284 eval q{use Date::Parse};
287 local $CWD = $config{srcdir};
289 # There's no cvsps option to get the last N changesets.
290 # Write full output to a temp file and read backwards.
292 eval q{use File::Temp qw/tempfile/};
294 eval q{use File::ReadBackwards};
297 my (undef, $tmpfile) = tempfile(OPEN=>0);
298 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
300 error "couldn't run cvsps: $!\n";
301 } elsif (($? >> 8) != 0) {
302 error "cvsps exited " . ($? >> 8) . ": $!\n";
305 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
306 || error "couldn't open $tmpfile for read: $!\n";
308 while (my $line = <SPSVC>) {
309 $line =~ /^$/ || error "expected blank line, got $line";
311 my ($rev, $user, $committype, $when);
312 my (@message, @pages);
314 # We're reading backwards.
315 # Forwards, an entry looks like so:
316 # ---------------------
319 # Author: $user (or user CGI runs as, for web commits)
325 # @pages (and revisions)
328 while ($line = <SPSVC>) {
329 last if ($line =~ /^Members:/);
334 my ($page, $revs) = split(/:/, $line);
335 my ($oldrev, $newrev) = split(/->/, $revs);
336 $oldrev =~ s/INITIAL/0/;
337 $newrev =~ s/\(DEAD\)//;
338 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
339 $diffurl=~s/\[\[file\]\]/$page/g;
340 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
341 $diffurl=~s/\[\[r2\]\]/$newrev/g;
343 page => pagename($page),
348 while ($line = <SPSVC>) {
349 last if ($line =~ /^Log:$/);
351 unshift @message, { line => $line };
354 if (defined $message[0] &&
355 $message[0]->{line}=~/$config{web_commit_regexp}/) {
356 $user=defined $2 ? "$2" : "$3";
357 $message[0]->{line}=$4;
363 $line = <SPSVC>; # Tag
364 $line = <SPSVC>; # Branch
367 if ($line =~ /^Author: (.*)$/) {
368 $user = $1 unless defined $user && length $user;
371 error "expected Author, got $line";
375 if ($line =~ /^Date: (.*)$/) {
376 $when = str2time($1, 'UTC');
379 error "expected Date, got $line";
383 if ($line =~ /^PatchSet (.*)$/) {
387 error "expected PatchSet, got $line";
390 $line = <SPSVC>; # ---------------------
395 committype => $committype,
397 message => [@message],
400 last if @ret >= $num;
403 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
409 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
411 local $CWD = $config{srcdir};
413 # diff output is unavoidably preceded by the cvsps PatchSet entry
414 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
415 my $blank_lines_seen = 0;
417 while (my $line = shift @cvsps) {
418 $blank_lines_seen++ if ($line =~ /^$/);
419 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");