2 package IkiWiki::Plugin::cvs;
9 hook(type => "wrapperargcheck", id => "cvs", call => \&wrapperargcheck);
10 hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
11 hook(type => "getsetup", id => "cvs", call => \&getsetup);
12 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
13 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
14 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
15 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
16 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
17 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
18 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
19 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
20 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
21 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
24 sub wrapperargcheck () {
25 my $check_args=<<"EOF";
27 for (j = 1; j < argc; j++)
28 if (strstr(argv[j], "New directory") != NULL)
36 if (! defined $config{cvspath}) {
37 $config{cvspath}="ikiwiki";
39 if (exists $config{cvspath}) {
40 # code depends on the path not having extraneous slashes
41 $config{cvspath}=~tr#/#/#s;
42 $config{cvspath}=~s/\/$//;
43 $config{cvspath}=~s/^\///;
45 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
46 push @{$config{wrappers}}, {
47 wrapper => $config{cvs_wrapper},
48 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
56 safe => 0, # rcs plugin
61 example => "/cvs/wikirepo",
62 description => "cvs repository location",
69 description => "path inside repository where the wiki is located",
75 example => "/cvs/wikirepo/CVSROOT/post-commit",
76 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry",
83 description => "mode for cvs_wrapper (can safely be made suid)",
89 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
90 description => "cvsweb url to show file history ([[file]] substituted)",
96 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
97 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
107 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
109 my $info=`cvs status $file`;
110 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
116 unshift @cmd, 'cvs', '-Q';
118 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
120 open(my $savedout, ">&STDOUT");
121 open(STDOUT, ">", "/dev/null");
122 my $ret = system(@cmd);
123 open(STDOUT, ">&", $savedout);
125 return ($ret == 0) ? 1 : 0;
128 sub cvs_is_controlling {
130 $dir=$config{srcdir} unless defined($dir);
131 return (-d "$dir/CVS") ? 1 : 0;
135 return unless cvs_is_controlling;
136 cvs_runcvs('update', '-dP');
139 sub rcs_prepedit ($) {
140 # Prepares to edit a file under revision control. Returns a token
141 # that must be passed into rcs_commit when the file is ready
143 # The file is relative to the srcdir.
146 return unless cvs_is_controlling;
148 # For cvs, return the revision of the file when
150 my $rev=cvs_info("Repository revision", "$file");
151 return defined $rev ? $rev : "";
154 sub rcs_commit ($$$;$$) {
155 # Tries to commit the page; returns undef on _success_ and
156 # a version of the page with the rcs's conflict markers on failure.
157 # The file is relative to the srcdir.
164 return unless cvs_is_controlling;
167 $message="web commit by $user".(length $message ? ": $message" : "");
169 elsif (defined $ipaddr) {
170 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
173 # Check to see if the page has been changed by someone
174 # else since rcs_prepedit was called.
175 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
176 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
177 if (defined $rev && defined $oldrev && $rev != $oldrev) {
178 # Merge their changes into the file that we've
180 cvs_runcvs('update', $file) ||
181 warn("cvs merge from $oldrev to $rev failed\n");
184 if (! cvs_runcvs('commit', '-m',
185 IkiWiki::possibly_foolish_untaint $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',
208 IkiWiki::possibly_foolish_untaint $message)) {
209 warn "cvs staged commit failed\n";
212 return undef # success
216 # filename is relative to the root of the srcdir
218 my $parent=IkiWiki::dirname($file);
219 my @files_to_add = ($file);
221 eval q{use File::MimeInfo};
224 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
225 push @files_to_add, $parent;
226 $parent = IkiWiki::dirname($parent);
229 while ($file = pop @files_to_add) {
230 if (@files_to_add == 0) {
232 my $filemime = File::MimeInfo::default($file);
233 if (defined($filemime) && $filemime eq 'text/plain') {
234 cvs_runcvs('add', $file) ||
235 warn("cvs add $file failed\n");
237 cvs_runcvs('add', '-kb', $file) ||
238 warn("cvs add binary $file failed\n");
242 cvs_runcvs('add', $file) ||
243 warn("cvs add $file failed\n");
249 # filename is relative to the root of the srcdir
252 return unless cvs_is_controlling;
254 cvs_runcvs('rm', '-f', $file) ||
255 warn("cvs rm $file failed\n");
258 sub rcs_rename ($$) {
259 # filenames relative to the root of the srcdir
262 return unless cvs_is_controlling;
264 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
266 if (system("mv", "$src", "$dest") != 0) {
267 warn("filesystem rename failed\n");
274 sub rcs_recentchanges($) {
278 return unless cvs_is_controlling;
280 eval q{use Date::Parse};
283 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
285 # There's no cvsps option to get the last N changesets.
286 # Write full output to a temp file and read backwards.
288 eval q{use File::Temp qw/tempfile/};
290 eval q{use File::ReadBackwards};
293 my (undef, $tmpfile) = tempfile(OPEN=>0);
294 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
296 error "couldn't run cvsps: $!\n";
297 } elsif (($? >> 8) != 0) {
298 error "cvsps exited " . ($? >> 8) . ": $!\n";
301 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
302 || error "couldn't open $tmpfile for read: $!\n";
304 while (my $line = <SPSVC>) {
305 $line =~ /^$/ || error "expected blank line, got $line";
307 my ($rev, $user, $committype, $when);
308 my (@message, @pages);
310 # We're reading backwards.
311 # Forwards, an entry looks like so:
312 # ---------------------
315 # Author: $user (or user CGI runs as, for web commits)
321 # @pages (and revisions)
324 while ($line = <SPSVC>) {
325 last if ($line =~ /^Members:/);
330 my ($page, $revs) = split(/:/, $line);
331 my ($oldrev, $newrev) = split(/->/, $revs);
332 $oldrev =~ s/INITIAL/0/;
333 $newrev =~ s/\(DEAD\)//;
334 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
335 $diffurl=~s/\[\[file\]\]/$page/g;
336 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
337 $diffurl=~s/\[\[r2\]\]/$newrev/g;
339 page => pagename($page),
344 while ($line = <SPSVC>) {
345 last if ($line =~ /^Log:$/);
347 unshift @message, { line => $line };
350 if (defined $message[0] &&
351 $message[0]->{line}=~/$config{web_commit_regexp}/) {
352 $user=defined $2 ? "$2" : "$3";
353 $message[0]->{line}=$4;
358 $line = <SPSVC>; # Tag
359 $line = <SPSVC>; # Branch
362 if ($line =~ /^Author: (.*)$/) {
363 $user = $1 unless defined $user && length $user;
365 error "expected Author, got $line";
369 if ($line =~ /^Date: (.*)$/) {
370 $when = str2time($1, 'UTC');
372 error "expected Date, got $line";
376 if ($line =~ /^PatchSet (.*)$/) {
379 error "expected PatchSet, got $line";
382 $line = <SPSVC>; # ---------------------
387 committype => $committype,
389 message => [@message],
392 last if @ret >= $num;
395 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
401 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
403 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
405 # diff output is unavoidably preceded by the cvsps PatchSet entry
406 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
407 my $blank_lines_seen = 0;
409 while (my $line = shift @cvsps) {
410 $blank_lines_seen++ if ($line =~ /^$/);
411 last if $blank_lines_seen == 2;
417 return join("", @cvsps);
421 sub rcs_getctime ($) {
424 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
426 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
427 || error "couldn't get cvs log output: $!\n";
431 if (/$cvs_log_infoline/) {
435 close CVSLOG || warn "cvs log $file exited $?";
437 if (! defined $date) {
438 warn "failed to parse cvs log for $file\n";
442 eval q{use Date::Parse};
444 $date=str2time($date, 'UTC');
445 debug("found ctime ".localtime($date)." for $file");