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 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
108 open(my $savedout, ">&STDOUT");
109 open(STDOUT, ">", "/dev/null");
110 my $ret = system(@cmd);
111 open(STDOUT, ">&", $savedout);
113 return ($ret == 0) ? 1 : 0;
116 sub cvs_is_controlling {
118 $dir=$config{srcdir} unless defined($dir);
119 return (-d "$dir/CVS") ? 1 : 0;
123 return unless cvs_is_controlling;
124 cvs_runcvs('update', '-dP');
127 sub rcs_prepedit ($) {
128 # Prepares to edit a file under revision control. Returns a token
129 # that must be passed into rcs_commit when the file is ready
131 # The file is relative to the srcdir.
134 return unless cvs_is_controlling;
136 # For cvs, return the revision of the file when
138 my $rev=cvs_info("Repository revision", "$file");
139 return defined $rev ? $rev : "";
142 sub rcs_commit ($$$;$$) {
143 # Tries to commit the page; returns undef on _success_ and
144 # a version of the page with the rcs's conflict markers on failure.
145 # The file is relative to the srcdir.
152 return unless cvs_is_controlling;
155 $message="web commit by $user".(length $message ? ": $message" : "");
157 elsif (defined $ipaddr) {
158 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
161 # Check to see if the page has been changed by someone
162 # else since rcs_prepedit was called.
163 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
164 my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
165 if (defined $rev && defined $oldrev && $rev != $oldrev) {
166 # Merge their changes into the file that we've
168 cvs_runcvs('update', $file) ||
169 warn("cvs merge from $oldrev to $rev failed\n");
172 if (! cvs_runcvs('commit', '-m',
173 IkiWiki::possibly_foolish_untaint $message)) {
174 my $conflict=readfile("$config{srcdir}/$file");
175 cvs_runcvs('update', '-C', $file) ||
176 warn("cvs revert failed\n");
180 return undef # success
183 sub rcs_commit_staged ($$$) {
184 # Commits all staged changes. Changes can be staged using rcs_add,
185 # rcs_remove, and rcs_rename.
186 my ($message, $user, $ipaddr)=@_;
189 $message="web commit by $user".(length $message ? ": $message" : "");
191 elsif (defined $ipaddr) {
192 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
195 if (! cvs_runcvs('commit', '-m',
196 IkiWiki::possibly_foolish_untaint $message)) {
197 warn "cvs staged commit failed\n";
200 return undef # success
204 # filename is relative to the root of the srcdir
206 my $parent=IkiWiki::dirname($file);
207 my @files_to_add = ($file);
209 eval q{use File::MimeInfo};
212 until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
213 push @files_to_add, $parent;
214 $parent = IkiWiki::dirname($parent);
217 while ($file = pop @files_to_add) {
218 if (@files_to_add == 0) {
220 my $filemime = File::MimeInfo::default($file);
221 if (defined($filemime) && $filemime eq 'text/plain') {
222 cvs_runcvs('add', $file) ||
223 warn("cvs add $file failed\n");
226 cvs_runcvs('add', '-kb', $file) ||
227 warn("cvs add binary $file failed\n");
232 cvs_runcvs('add', $file) ||
233 warn("cvs add $file failed\n");
239 # filename is relative to the root of the srcdir
242 return unless cvs_is_controlling;
244 cvs_runcvs('rm', '-f', $file) ||
245 warn("cvs rm $file failed\n");
248 sub rcs_rename ($$) {
249 # filenames relative to the root of the srcdir
252 return unless cvs_is_controlling;
254 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
256 if (system("mv", "$src", "$dest") != 0) {
257 warn("filesystem rename failed\n");
264 sub rcs_recentchanges($) {
268 return unless cvs_is_controlling;
270 eval q{use Date::Parse};
273 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
275 # There's no cvsps option to get the last N changesets.
276 # Write full output to a temp file and read backwards.
278 eval q{use File::Temp qw/tempfile/};
280 eval q{use File::ReadBackwards};
283 my (undef, $tmpfile) = tempfile(OPEN=>0);
284 system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
286 error "couldn't run cvsps: $!\n";
287 } elsif (($? >> 8) != 0) {
288 error "cvsps exited " . ($? >> 8) . ": $!\n";
291 tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
292 || error "couldn't open $tmpfile for read: $!\n";
294 while (my $line = <SPSVC>) {
295 $line =~ /^$/ || error "expected blank line, got $line";
297 my ($rev, $user, $committype, $when);
298 my (@message, @pages);
300 # We're reading backwards.
301 # Forwards, an entry looks like so:
302 # ---------------------
305 # Author: $user (or user CGI runs as, for web commits)
311 # @pages (and revisions)
314 while ($line = <SPSVC>) {
315 last if ($line =~ /^Members:/);
320 my ($page, $revs) = split(/:/, $line);
321 my ($oldrev, $newrev) = split(/->/, $revs);
322 $oldrev =~ s/INITIAL/0/;
323 $newrev =~ s/\(DEAD\)//;
324 my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
325 $diffurl=~s/\[\[file\]\]/$page/g;
326 $diffurl=~s/\[\[r1\]\]/$oldrev/g;
327 $diffurl=~s/\[\[r2\]\]/$newrev/g;
329 page => pagename($page),
334 while ($line = <SPSVC>) {
335 last if ($line =~ /^Log:$/);
337 unshift @message, { line => $line };
340 if (defined $message[0] &&
341 $message[0]->{line}=~/$config{web_commit_regexp}/) {
342 $user=defined $2 ? "$2" : "$3";
343 $message[0]->{line}=$4;
349 $line = <SPSVC>; # Tag
350 $line = <SPSVC>; # Branch
353 if ($line =~ /^Author: (.*)$/) {
354 $user = $1 unless defined $user && length $user;
357 error "expected Author, got $line";
361 if ($line =~ /^Date: (.*)$/) {
362 $when = str2time($1, 'UTC');
365 error "expected Date, got $line";
369 if ($line =~ /^PatchSet (.*)$/) {
373 error "expected PatchSet, got $line";
376 $line = <SPSVC>; # ---------------------
381 committype => $committype,
383 message => [@message],
386 last if @ret >= $num;
389 unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
395 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
397 chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
399 # diff output is unavoidably preceded by the cvsps PatchSet entry
400 my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
401 my $blank_lines_seen = 0;
403 while (my $line = shift @cvsps) {
404 $blank_lines_seen++ if ($line =~ /^$/);
405 last if $blank_lines_seen == 2;
412 return join("", @cvsps);
416 sub rcs_getctime ($) {
419 my $cvs_log_infoline=qr/^date: (.+);\s+author/;
421 open CVSLOG, "cvs -Q log -r1.1 '$file' |"
422 || error "couldn't get cvs log output: $!\n";
426 if (/$cvs_log_infoline/) {
430 close CVSLOG || warn "cvs log $file exited $?";
432 if (! defined $date) {
433 warn "failed to parse cvs log for $file\n";
437 eval q{use Date::Parse};
439 $date=str2time($date, 'UTC');
440 debug("found ctime ".localtime($date)." for $file");