2 package IkiWiki::Plugin::cvs;
9 hook(type => "getopt", id => "cvs", call => \&getopt);
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);
25 # "cvs add dir" acts immediately on the repository.
26 # post-commit gets confused by this and doesn't need to act on it.
27 # If that's why we're here, terminate the process.
28 @ARGV == 3 && $ARGV[1] eq "NONE" && $ARGV[2] eq "NONE" && exit 0;
32 if (! defined $config{cvspath}) {
33 $config{cvspath}="ikiwiki";
35 if (exists $config{cvspath}) {
36 # code depends on the path not having extraneous slashes
37 $config{cvspath}=~tr#/#/#s;
38 $config{cvspath}=~s/\/$//;
39 $config{cvspath}=~s/^\///;
41 if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
42 push @{$config{wrappers}}, {
43 wrapper => $config{cvs_wrapper},
44 wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
52 safe => 0, # rcs plugin
57 example => "/cvs/wikirepo",
58 description => "cvs repository location",
65 description => "path inside repository where the wiki is located",
71 example => "/cvs/wikirepo/CVSROOT/post-commit",
72 description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry",
79 description => "mode for cvs_wrapper (can safely be made suid)",
85 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
86 description => "cvsweb url to show file history ([[file]] substituted)",
92 example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&tr1=[[r1]]&r2=text&tr2=[[r2]]",
93 description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
103 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
105 my $info=`cvs status $file`;
106 my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
112 unshift @$cmd, 'cvs', '-Q';
119 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
121 debug("runcvs: " . join(" ", @$cmd));
123 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
124 IPC::Cmd::run(command => $cmd, verbose => 0);
126 warn(join(" ", @$cmd) . " exited with code $error_code\n");
127 warn(join "", @$stderr_buf);
132 sub cvs_shquote_commit ($) {
136 use String::ShellQuote;
140 return shell_quote(IkiWiki::possibly_foolish_untaint($message));
143 sub cvs_is_controlling {
145 $dir=$config{srcdir} unless defined($dir);
146 return (-d "$dir/CVS") ? 1 : 0;
150 return unless cvs_is_controlling;
151 cvs_runcvs(['update', '-dP']);
154 sub rcs_prepedit ($) {
155 # Prepares to edit a file under revision control. Returns a token
156 # that must be passed into rcs_commit when the file is ready
158 # The file is relative to the srcdir.
161 return unless cvs_is_controlling;
163 # For cvs, return the revision of the file when
165 my $rev=cvs_info("Repository revision", "$file");
166 return defined $rev ? $rev : "";
169 sub rcs_commit ($$$;$$) {
170 # Tries to commit the page; returns undef on _success_ and
171 # a version of the page with the rcs's conflict markers on failure.
172 # The file is relative to the srcdir.
179 return unless cvs_is_controlling;
182 $message="web commit by $user".(length $message ? ": $message" : "");
184 elsif (defined $ipaddr) {
185 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
188 # Check to see if the page has been changed by someone
189 # else since rcs_prepedit was called.
190 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
191 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
192 if (defined $rev && defined $oldrev && $rev != $oldrev) {
193 # Merge their changes into the file that we've
195 cvs_runcvs(['update', $file]) ||
196 warn("cvs merge from $oldrev to $rev failed\n");
199 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
200 my $conflict=readfile("$config{srcdir}/$file");
201 cvs_runcvs(['update', '-C', $file]) ||
202 warn("cvs revert failed\n");
206 return undef # success
209 sub rcs_commit_staged ($$$) {
210 # Commits all staged changes. Changes can be staged using rcs_add,
211 # rcs_remove, and rcs_rename.
212 my ($message, $user, $ipaddr)=@_;
215 $message="web commit by $user".(length $message ? ": $message" : "");
217 elsif (defined $ipaddr) {
218 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
221 if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
222 warn "cvs staged commit failed\n";
225 return undef # success
229 # filename is relative to the root of the srcdir
231 my $parent=IkiWiki::dirname($file);
232 my @files_to_add = ($file);
234 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
235 push @files_to_add, $parent;
236 $parent = IkiWiki::dirname($parent);
239 while ($file = pop @files_to_add) {
240 cvs_runcvs(['add', $file]) ||
241 warn("cvs add $file failed\n");
246 # filename is relative to the root of the srcdir
249 return unless cvs_is_controlling;
251 cvs_runcvs(['rm', '-f', $file]) ||
252 warn("cvs rm $file failed\n");
255 sub rcs_rename ($$) {
256 # filenames relative to the root of the srcdir
259 return unless cvs_is_controlling;
261 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
263 if (system("mv", "$src", "$dest") != 0) {
264 warn("filesystem rename failed\n");
271 sub rcs_recentchanges($) {
275 return unless cvs_is_controlling;
282 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
284 open CVSPS, "env TZ=UTC cvsps -q --cvs-direct -z 30 -x |" || error "couldn't get cvsps output: $!\n";
285 my @spsvc = reverse <CVSPS>; # is this great? no it is not
286 close CVSPS || error "couldn't close cvsps output: $!\n";
288 while (my $line = shift @spsvc) {
289 $line =~ /^$/ || error "expected blank line, got $line";
291 my ($rev, $user, $committype, $when);
292 my (@message, @pages);
294 # We're reading backwards.
295 # Forwards, an entry looks like so:
296 # ---------------------
299 # Author: $user (or user CGI runs as, for web commits)
305 # @pages (and revisions)
308 while ($line = shift @spsvc) {
309 last if ($line =~ /^Members:/);
314 my ($page, $revs) = split(/:/, $line);
315 my ($oldrev, $newrev) = split(/->/, $revs);
316 $oldrev =~ s/INITIAL/0/;
317 $newrev =~ s/\(DEAD\)//;
318 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
319 $diffurl=~s/\[\[file\]\]/$page/g;
320 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
321 $diffurl=~s/\[\[r2\]\]/$newrev/g;
323 page => pagename($page),
328 while ($line = shift @spsvc) {
329 last if ($line =~ /^Log:$/);
331 unshift @message, { line => $line };
334 if (defined $message[0] &&
335 $message[0]->{line}=~/$config{web_commit_regexp}/) {
336 $user=defined $2 ? "$2" : "$3";
337 $message[0]->{line}=$4;
342 $line = shift @spsvc; # Tag
343 $line = shift @spsvc; # Branch
345 $line = shift @spsvc;
346 if ($line =~ /^Author: (.*)$/) {
347 $user = $1 unless defined $user && length $user;
349 error "expected Author, got $line";
352 $line = shift @spsvc;
353 if ($line =~ /^Date: (.*)$/) {
354 $when = str2time($1, 'UTC');
356 error "expected Date, got $line";
359 $line = shift @spsvc;
360 if ($line =~ /^PatchSet (.*)$/) {
363 error "expected PatchSet, got $line";
366 $line = shift @spsvc; # ---------------------
371 committype => $committype,
373 message => [@message],
376 return @ret if @ret >= $num;
383 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
385 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
387 # diff output is unavoidably preceded by the cvsps PatchSet entry
388 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
389 my $blank_lines_seen = 0;
391 while (my $line = shift @cvsps) {
392 $blank_lines_seen++ if ($line =~ /^$/);
393 last if $blank_lines_seen == 2;
399 return join("", @cvsps);
403 sub rcs_getctime ($) {
406 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
408 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
409 || error "couldn't get cvs log output: $!\n";
413 if (/$cvs_log_infoline/) {
417 close CVSLOG || warn "cvs log $file exited $?";
419 if (! defined $date) {
420 warn "failed to parse cvs log for $file\n";
424 eval q{use Date::Parse};
426 $date=str2time($date, 'UTC');
427 debug("found ctime ".localtime($date)." for $file");