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 debug("runcvs: " . join(" ", @$cmd));
113 my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
114 IPC::Cmd::run(command => $cmd, verbose => 0);
116 warn(join(" ", @$cmd) . " exited with code $error_code\n");
117 warn(join "", @$stderr_buf);
122 sub cvs_shquote_commit ($) {
125 eval q{use String::ShellQuote};
128 return shell_quote(IkiWiki::possibly_foolish_untaint($message));
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', cvs_shquote_commit $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', cvs_shquote_commit $message])) {
210 warn "cvs staged commit failed\n";
213 return undef # success
217 # filename is relative to the root of the srcdir
219 my $parent=IkiWiki::dirname($file);
220 my @files_to_add = ($file);
222 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
223 push @files_to_add, $parent;
224 $parent = IkiWiki::dirname($parent);
227 while ($file = pop @files_to_add) {
228 cvs_runcvs(['add', $file]) ||
229 warn("cvs add $file failed\n");
234 # filename is relative to the root of the srcdir
237 return unless cvs_is_controlling;
239 cvs_runcvs(['rm', '-f', $file]) ||
240 warn("cvs rm $file failed\n");
243 sub rcs_rename ($$) {
244 # filenames relative to the root of the srcdir
247 return unless cvs_is_controlling;
249 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
251 if (system("mv", "$src", "$dest") != 0) {
252 warn("filesystem rename failed\n");
259 sub rcs_recentchanges($) {
263 return unless cvs_is_controlling;
265 eval q{use Date::Parse};
268 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
270 # there's no option to get the last N changesets, so read backwards
271 open CVSPS, "env TZ=UTC cvsps -q --cvs-direct -z 30 -x |" || error "couldn't get cvsps output: $!\n";
272 my @spsvc = reverse <CVSPS>; # is this great? no it is not
273 close CVSPS || error "couldn't close cvsps output: $!\n";
275 while (my $line = shift @spsvc) {
276 $line =~ /^$/ || error "expected blank line, got $line";
278 my ($rev, $user, $committype, $when);
279 my (@message, @pages);
281 # We're reading backwards.
282 # Forwards, an entry looks like so:
283 # ---------------------
286 # Author: $user (or user CGI runs as, for web commits)
292 # @pages (and revisions)
295 while ($line = shift @spsvc) {
296 last if ($line =~ /^Members:/);
301 my ($page, $revs) = split(/:/, $line);
302 my ($oldrev, $newrev) = split(/->/, $revs);
303 $oldrev =~ s/INITIAL/0/;
304 $newrev =~ s/\(DEAD\)//;
305 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
306 $diffurl=~s/\[\[file\]\]/$page/g;
307 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
308 $diffurl=~s/\[\[r2\]\]/$newrev/g;
310 page => pagename($page),
315 while ($line = shift @spsvc) {
316 last if ($line =~ /^Log:$/);
318 unshift @message, { line => $line };
321 if (defined $message[0] &&
322 $message[0]->{line}=~/$config{web_commit_regexp}/) {
323 $user=defined $2 ? "$2" : "$3";
324 $message[0]->{line}=$4;
329 $line = shift @spsvc; # Tag
330 $line = shift @spsvc; # Branch
332 $line = shift @spsvc;
333 if ($line =~ /^Author: (.*)$/) {
334 $user = $1 unless defined $user && length $user;
336 error "expected Author, got $line";
339 $line = shift @spsvc;
340 if ($line =~ /^Date: (.*)$/) {
341 $when = str2time($1, 'UTC');
343 error "expected Date, got $line";
346 $line = shift @spsvc;
347 if ($line =~ /^PatchSet (.*)$/) {
350 error "expected PatchSet, got $line";
353 $line = shift @spsvc; # ---------------------
358 committype => $committype,
360 message => [@message],
363 return @ret if @ret >= $num;
370 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
372 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
374 # diff output is unavoidably preceded by the cvsps PatchSet entry
375 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
376 my $blank_lines_seen = 0;
378 while (my $line = shift @cvsps) {
379 $blank_lines_seen++ if ($line =~ /^$/);
380 last if $blank_lines_seen == 2;
386 return join("", @cvsps);
390 sub rcs_getctime ($) {
393 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
395 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
396 || error "couldn't get cvs log output: $!\n";
400 if (/$cvs_log_infoline/) {
404 close CVSLOG || warn "cvs log $file exited $?";
406 if (! defined $date) {
407 warn "failed to parse cvs log for $file\n";
411 eval q{use Date::Parse};
413 $date=str2time($date, 'UTC');
414 debug("found ctime ".localtime($date)." for $file");