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);
27 my $check_args=<<"EOF";
30 for (j = 1; j < argc; j++)
31 if (strstr(argv[j], "New directory") != NULL)
39 if (! defined $config{cvspath}) {
40 $config{cvspath}="ikiwiki";
42 if (exists $config{cvspath}) {
43 # code depends on the path not having extraneous slashes
44 $config{cvspath}=~tr#/#/#s;
45 $config{cvspath}=~s/\/$//;
46 $config{cvspath}=~s/^\///;
48 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
49 push @{$config{wrappers}}, {
50 wrapper => $config{cvs_wrapper},
51 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
59 safe => 0, # rcs plugin
64 example => "/cvs/wikirepo",
65 description => "cvs repository location",
72 description => "path inside repository where the wiki is located",
78 example => "/cvs/wikirepo/CVSROOT/post-commit",
79 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
86 description => "mode for cvs_wrapper (can safely be made suid)",
92 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
93 description => "cvsweb url to show file history ([[file]] substituted)",
99 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
100 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
110 local $CWD = $config{srcdir};
112 my $info=`cvs status $file`;
113 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
119 unshift @cmd, 'cvs', '-Q';
121 local $CWD = $config{srcdir};
123 open(my $savedout, ">&STDOUT");
124 open(STDOUT, ">", "/dev/null");
125 my $ret = system(@cmd);
126 open(STDOUT, ">&", $savedout);
128 return ($ret == 0) ? 1 : 0;
131 sub cvs_is_controlling {
133 $dir=$config{srcdir} unless defined($dir);
134 return (-d "$dir/CVS") ? 1 : 0;
138 return unless cvs_is_controlling;
139 cvs_runcvs('update', '-dP');
142 sub rcs_prepedit ($) {
143 # Prepares to edit a file under revision control. Returns a token
144 # that must be passed into rcs_commit when the file is ready
146 # The file is relative to the srcdir.
149 return unless cvs_is_controlling;
151 # For cvs, return the revision of the file when
153 my $rev=cvs_info("Repository revision", "$file");
154 return defined $rev ? $rev : "";
157 sub rcs_commit ($$$;$$) {
158 # Tries to commit the page; returns undef on _success_ and
159 # a version of the page with the rcs's conflict markers on failure.
160 # The file is relative to the srcdir.
167 return unless cvs_is_controlling;
170 $message="web commit by $user".(length $message ? ": $message" : "");
172 elsif (defined $ipaddr) {
173 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
176 # Check to see if the page has been changed by someone
177 # else since rcs_prepedit was called.
178 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
179 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
180 if (defined $rev && defined $oldrev && $rev != $oldrev) {
181 # Merge their changes into the file that we've
183 cvs_runcvs('update', $file) ||
184 warn("cvs merge from $oldrev to $rev failed\n");
187 if (! cvs_runcvs('commit', '-m',
188 IkiWiki::possibly_foolish_untaint $message)) {
189 my $conflict=readfile("$config{srcdir}/$file");
190 cvs_runcvs('update', '-C', $file) ||
191 warn("cvs revert failed\n");
195 return undef # success
198 sub rcs_commit_staged ($$$) {
199 # Commits all staged changes. Changes can be staged using rcs_add,
200 # rcs_remove, and rcs_rename.
201 my ($message, $user, $ipaddr)=@_;
204 $message="web commit by $user".(length $message ? ": $message" : "");
206 elsif (defined $ipaddr) {
207 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
210 if (! cvs_runcvs('commit', '-m',
211 IkiWiki::possibly_foolish_untaint $message)) {
212 warn "cvs staged commit failed\n";
215 return undef # success
219 # filename is relative to the root of the srcdir
221 my $parent=IkiWiki::dirname($file);
222 my @files_to_add = ($file);
224 eval q{use File::MimeInfo};
227 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
228 push @files_to_add, $parent;
229 $parent = IkiWiki::dirname($parent);
232 while ($file = pop @files_to_add) {
233 if (@files_to_add == 0) {
235 my $filemime = File::MimeInfo::default($file);
236 if (defined($filemime) && $filemime eq 'text/plain') {
237 cvs_runcvs('add', $file) ||
238 warn("cvs add $file failed\n");
241 cvs_runcvs('add', '-kb', $file) ||
242 warn("cvs add binary $file failed\n");
247 cvs_runcvs('add', $file) ||
248 warn("cvs add $file failed\n");
254 # filename is relative to the root of the srcdir
257 return unless cvs_is_controlling;
259 cvs_runcvs('rm', '-f', $file) ||
260 warn("cvs rm $file failed\n");
263 sub rcs_rename ($$) {
264 # filenames relative to the root of the srcdir
267 return unless cvs_is_controlling;
269 local $CWD = $config{srcdir};
271 if (system("mv", "$src", "$dest") != 0) {
272 warn("filesystem rename failed\n");
279 sub rcs_recentchanges($) {
283 return unless cvs_is_controlling;
285 eval q{use Date::Parse};
288 local $CWD = $config{srcdir};
290 # There's no cvsps option to get the last N changesets.
291 # Write full output to a temp file and read backwards.
293 eval q{use File::Temp qw/tempfile/};
295 eval q{use File::ReadBackwards};
298 my (undef, $tmpfile) = tempfile(OPEN=>0);
299 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
301 error "couldn't run cvsps: $!\n";
302 } elsif (($? >> 8) != 0) {
303 error "cvsps exited " . ($? >> 8) . ": $!\n";
306 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
307 || error "couldn't open $tmpfile for read: $!\n";
309 while (my $line = <SPSVC>) {
310 $line =~ /^$/ || error "expected blank line, got $line";
312 my ($rev, $user, $committype, $when);
313 my (@message, @pages);
315 # We're reading backwards.
316 # Forwards, an entry looks like so:
317 # ---------------------
320 # Author: $user (or user CGI runs as, for web commits)
326 # @pages (and revisions)
329 while ($line = <SPSVC>) {
330 last if ($line =~ /^Members:/);
335 my ($page, $revs) = split(/:/, $line);
336 my ($oldrev, $newrev) = split(/->/, $revs);
337 $oldrev =~ s/INITIAL/0/;
338 $newrev =~ s/\(DEAD\)//;
339 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
340 $diffurl=~s/\[\[file\]\]/$page/g;
341 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
342 $diffurl=~s/\[\[r2\]\]/$newrev/g;
344 page => pagename($page),
349 while ($line = <SPSVC>) {
350 last if ($line =~ /^Log:$/);
352 unshift @message, { line => $line };
355 if (defined $message[0] &&
356 $message[0]->{line}=~/$config{web_commit_regexp}/) {
357 $user=defined $2 ? "$2" : "$3";
358 $message[0]->{line}=$4;
364 $line = <SPSVC>; # Tag
365 $line = <SPSVC>; # Branch
368 if ($line =~ /^Author: (.*)$/) {
369 $user = $1 unless defined $user && length $user;
372 error "expected Author, got $line";
376 if ($line =~ /^Date: (.*)$/) {
377 $when = str2time($1, 'UTC');
380 error "expected Date, got $line";
384 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 local $CWD = $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;
427 return join("", @cvsps);
431 sub rcs_getctime ($) {
434 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
436 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
437 || error "couldn't get cvs log output: $!\n";
441 if (/$cvs_log_infoline/) {
445 close CVSLOG || warn "cvs log $file exited $?";
447 if (! defined $date) {
448 warn "failed to parse cvs log for $file\n";
452 eval q{use Date::Parse};
454 $date=str2time($date, 'UTC');
455 debug("found ctime ".localtime($date)." for $file");