]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/cvs.pm
don't force a rebuild
[ikiwiki.git] / IkiWiki / Plugin / cvs.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::cvs;
3
4 # Copyright (c) 2009 Amitai Schlair
5 # All rights reserved.
6 #
7 # This code is derived from software contributed to ikiwiki
8 # by Amitai Schlair.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions
12 # are met:
13 # 1. Redistributions of source code must retain the above copyright
14 #    notice, this list of conditions and the following disclaimer.
15 # 2. Redistributions in binary form must reproduce the above copyright
16 #    notice, this list of conditions and the following disclaimer in the
17 #    documentation and/or other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY IKIWIKI AND CONTRIBUTORS ``AS IS''
20 # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
22 # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION
23 # OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
25 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
26 # USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
27 # ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
29 # OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
30 # SUCH DAMAGE.
31
32 use warnings;
33 use strict;
34 use IkiWiki;
35
36 use File::chdir;
37
38 sub import {
39         hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
40         hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
41         hook(type => "getsetup", id => "cvs", call => \&getsetup);
42         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
43         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
44         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
45         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
46         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
47         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
48         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
49         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
50         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
51         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
52 }
53
54 sub genwrapper () {
55         return <<EOF;
56         {
57                 int j;
58                 for (j = 1; j < argc; j++)
59                         if (strstr(argv[j], "New directory") != NULL)
60                                 exit(0);
61         }
62 EOF
63 }
64
65 sub checkconfig () {
66         if (! defined $config{cvspath}) {
67                 $config{cvspath}="ikiwiki";
68         }
69         if (exists $config{cvspath}) {
70                 # code depends on the path not having extraneous slashes
71                 $config{cvspath}=~tr#/#/#s;
72                 $config{cvspath}=~s/\/$//;
73                 $config{cvspath}=~s/^\///;
74         }
75         if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
76                 push @{$config{wrappers}}, {
77                         wrapper => $config{cvs_wrapper},
78                         wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
79                 };
80         }
81 }
82
83 sub getsetup () {
84         return
85                 plugin => {
86                         safe => 0, # rcs plugin
87                         rebuild => undef,
88                         section => "rcs",
89                 },
90                 cvsrepo => {
91                         type => "string",
92                         example => "/cvs/wikirepo",
93                         description => "cvs repository location",
94                         safe => 0, # path
95                         rebuild => 0,
96                 },
97                 cvspath => {
98                         type => "string",
99                         example => "ikiwiki",
100                         description => "path inside repository where the wiki is located",
101                         safe => 0, # paranoia
102                         rebuild => 0,
103                 },
104                 cvs_wrapper => {
105                         type => "string",
106                         example => "/cvs/wikirepo/CVSROOT/post-commit",
107                         description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry)",
108                         safe => 0, # file
109                         rebuild => 0,
110                 },
111                 cvs_wrappermode => {
112                         type => "string",
113                         example => '04755',
114                         description => "mode for cvs_wrapper (can safely be made suid)",
115                         safe => 0,
116                         rebuild => 0,
117                 },
118                 historyurl => {
119                         type => "string",
120                         example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
121                         description => "cvsweb url to show file history ([[file]] substituted)",
122                         safe => 1,
123                         rebuild => 1,
124                 },
125                 diffurl => {
126                         type => "string",
127                         example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&amp;tr1=[[r1]]&amp;r2=text&amp;tr2=[[r2]]",
128                         description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
129                         safe => 1,
130                         rebuild => 1,
131                 },
132 }
133
134 sub cvs_info ($$) {
135         my $field=shift;
136         my $file=shift;
137
138         local $CWD = $config{srcdir};
139
140         my $info=`cvs status $file`;
141         my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
142         return $ret;
143 }
144
145 sub cvs_runcvs(@) {
146         my @cmd = @_;
147         unshift @cmd, 'cvs', '-Q';
148
149         local $CWD = $config{srcdir};
150
151         open(my $savedout, ">&STDOUT");
152         open(STDOUT, ">", "/dev/null");
153         my $ret = system(@cmd);
154         open(STDOUT, ">&", $savedout);
155
156         return ($ret == 0) ? 1 : 0;
157 }
158
159 sub cvs_is_controlling {
160         my $dir=shift;
161         $dir=$config{srcdir} unless defined($dir);
162         return (-d "$dir/CVS") ? 1 : 0;
163 }
164
165 sub rcs_update () {
166         return unless cvs_is_controlling;
167         cvs_runcvs('update', '-dP');
168 }
169
170 sub rcs_prepedit ($) {
171         # Prepares to edit a file under revision control. Returns a token
172         # that must be passed into rcs_commit when the file is ready
173         # for committing.
174         # The file is relative to the srcdir.
175         my $file=shift;
176
177         return unless cvs_is_controlling;
178
179         # For cvs, return the revision of the file when
180         # editing begins.
181         my $rev=cvs_info("Repository revision", "$file");
182         return defined $rev ? $rev : "";
183 }
184
185 sub rcs_commit ($$$;$$) {
186         # Tries to commit the page; returns undef on _success_ and
187         # a version of the page with the rcs's conflict markers on failure.
188         # The file is relative to the srcdir.
189         my $file=shift;
190         my $message=shift;
191         my $rcstoken=shift;
192         my $user=shift;
193         my $ipaddr=shift;
194
195         return unless cvs_is_controlling;
196
197         if (defined $user) {
198                 $message="web commit by $user".(length $message ? ": $message" : "");
199         }
200         elsif (defined $ipaddr) {
201                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
202         }
203
204         # Check to see if the page has been changed by someone
205         # else since rcs_prepedit was called.
206         my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
207         my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
208         if (defined $rev && defined $oldrev && $rev != $oldrev) {
209                 # Merge their changes into the file that we've
210                 # changed.
211                 cvs_runcvs('update', $file) ||
212                         warn("cvs merge from $oldrev to $rev failed\n");
213         }
214
215         if (! cvs_runcvs('commit', '-m',
216                          IkiWiki::possibly_foolish_untaint $message)) {
217                 my $conflict=readfile("$config{srcdir}/$file");
218                 cvs_runcvs('update', '-C', $file) ||
219                         warn("cvs revert failed\n");
220                 return $conflict;
221         }
222
223         return undef # success
224 }
225
226 sub rcs_commit_staged ($$$) {
227         # Commits all staged changes. Changes can be staged using rcs_add,
228         # rcs_remove, and rcs_rename.
229         my ($message, $user, $ipaddr)=@_;
230
231         if (defined $user) {
232                 $message="web commit by $user".(length $message ? ": $message" : "");
233         }
234         elsif (defined $ipaddr) {
235                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
236         }
237
238         if (! cvs_runcvs('commit', '-m',
239                          IkiWiki::possibly_foolish_untaint $message)) {
240                 warn "cvs staged commit failed\n";
241                 return 1; # failure
242         }
243         return undef # success
244 }
245
246 sub rcs_add ($) {
247         # filename is relative to the root of the srcdir
248         my $file=shift;
249         my $parent=IkiWiki::dirname($file);
250         my @files_to_add = ($file);
251
252         eval q{use File::MimeInfo};
253         error($@) if $@;
254
255         until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
256                 push @files_to_add, $parent;
257                 $parent = IkiWiki::dirname($parent);
258         }
259
260         while ($file = pop @files_to_add) {
261                 if (@files_to_add == 0) {
262                         # file
263                         my $filemime = File::MimeInfo::default($file);
264                         if (defined($filemime) && $filemime eq 'text/plain') {
265                                 cvs_runcvs('add', $file) ||
266                                         warn("cvs add $file failed\n");
267                         }
268                         else {
269                                 cvs_runcvs('add', '-kb', $file) ||
270                                         warn("cvs add binary $file failed\n");
271                         }
272                 }
273                 else {
274                         # directory
275                         cvs_runcvs('add', $file) ||
276                                 warn("cvs add $file failed\n");
277                 }
278         }
279 }
280
281 sub rcs_remove ($) {
282         # filename is relative to the root of the srcdir
283         my $file=shift;
284
285         return unless cvs_is_controlling;
286
287         cvs_runcvs('rm', '-f', $file) ||
288                 warn("cvs rm $file failed\n");
289 }
290
291 sub rcs_rename ($$) {
292         # filenames relative to the root of the srcdir
293         my ($src, $dest)=@_;
294
295         return unless cvs_is_controlling;
296
297         local $CWD = $config{srcdir};
298
299         if (system("mv", "$src", "$dest") != 0) {
300                 warn("filesystem rename failed\n");
301         }
302
303         rcs_add($dest);
304         rcs_remove($src);
305 }
306
307 sub rcs_recentchanges($) {
308         my $num = shift;
309         my @ret;
310
311         return unless cvs_is_controlling;
312
313         eval q{use Date::Parse};
314         error($@) if $@;
315
316         local $CWD = $config{srcdir};
317
318         # There's no cvsps option to get the last N changesets.
319         # Write full output to a temp file and read backwards.
320
321         eval q{use File::Temp qw/tempfile/};
322         error($@) if $@;
323         eval q{use File::ReadBackwards};
324         error($@) if $@;
325
326         my ($tmphandle, $tmpfile) = tempfile();
327         system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
328         if ($? == -1) {
329                 error "couldn't run cvsps: $!\n";
330         }
331         elsif (($? >> 8) != 0) {
332                 error "cvsps exited " . ($? >> 8) . ": $!\n";
333         }
334
335         tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
336                 || error "couldn't open $tmpfile for read: $!\n";
337
338         while (my $line = <SPSVC>) {
339                 $line =~ /^$/ || error "expected blank line, got $line";
340
341                 my ($rev, $user, $committype, $when);
342                 my (@message, @pages);
343
344                 # We're reading backwards.
345                 # Forwards, an entry looks like so:
346                 # ---------------------
347                 # PatchSet $rev
348                 # Date: $when
349                 # Author: $user (or user CGI runs as, for web commits)
350                 # Branch: branch
351                 # Tag: tag
352                 # Log:
353                 # @message_lines
354                 # Members:
355                 #       @pages (and revisions)
356                 #
357
358                 while ($line = <SPSVC>) {
359                         last if ($line =~ /^Members:/);
360                         for ($line) {
361                                 s/^\s+//;
362                                 s/\s+$//;
363                         }
364                         my ($page, $revs) = split(/:/, $line);
365                         my ($oldrev, $newrev) = split(/->/, $revs);
366                         $oldrev =~ s/INITIAL/0/;
367                         $newrev =~ s/\(DEAD\)//;
368                         my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
369                         $diffurl=~s/\[\[file\]\]/$page/g;
370                         $diffurl=~s/\[\[r1\]\]/$oldrev/g;
371                         $diffurl=~s/\[\[r2\]\]/$newrev/g;
372                         unshift @pages, {
373                                 page => pagename($page),
374                                 diffurl => $diffurl,
375                         } if length $page;
376                 }
377
378                 while ($line = <SPSVC>) {
379                         last if ($line =~ /^Log:$/);
380                         chomp $line;
381                         unshift @message, { line => $line };
382                 }
383                 $committype = "web";
384                 if (defined $message[0] &&
385                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
386                         $user=defined $2 ? "$2" : "$3";
387                         $message[0]->{line}=$4;
388                 }
389                 else {
390                         $committype="cvs";
391                 }
392
393                 $line = <SPSVC>;        # Tag
394                 $line = <SPSVC>;        # Branch
395
396                 $line = <SPSVC>;
397                 if ($line =~ /^Author: (.*)$/) {
398                         $user = $1 unless defined $user && length $user;
399                 }
400                 else {
401                         error "expected Author, got $line";
402                 }
403
404                 $line = <SPSVC>;
405                 if ($line =~ /^Date: (.*)$/) {
406                         $when = str2time($1, 'UTC');
407                 }
408                 else {
409                         error "expected Date, got $line";
410                 }
411
412                 $line = <SPSVC>;
413                 if ($line =~ /^PatchSet (.*)$/) {
414                         $rev = $1;
415                 }
416                 else {
417                         error "expected PatchSet, got $line";
418                 }
419
420                 $line = <SPSVC>;        # ---------------------
421
422                 push @ret, {
423                         rev => $rev,
424                         user => $user,
425                         committype => $committype,
426                         when => $when,
427                         message => [@message],
428                         pages => [@pages],
429                 } if @pages;
430                 last if @ret >= $num;
431         }
432
433         unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
434
435         return @ret;
436 }
437
438 sub rcs_diff ($) {
439         my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
440
441         local $CWD = $config{srcdir};
442
443         # diff output is unavoidably preceded by the cvsps PatchSet entry
444         my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
445         my $blank_lines_seen = 0;
446
447         while (my $line = shift @cvsps) {
448                 $blank_lines_seen++ if ($line =~ /^$/);
449                 last if $blank_lines_seen == 2;
450         }
451
452         if (wantarray) {
453                 return @cvsps;
454         }
455         else {
456                 return join("", @cvsps);
457         }
458 }
459
460 sub rcs_getctime ($) {
461         my $file=shift;
462
463         my $cvs_log_infoline=qr/^date: (.+);\s+author/;
464
465         open CVSLOG, "cvs -Q log -r1.1 '$file' |"
466                 || error "couldn't get cvs log output: $!\n";
467
468         my $date;
469         while (<CVSLOG>) {
470                 if (/$cvs_log_infoline/) {
471                         $date=$1;
472                 }
473         }
474         close CVSLOG || warn "cvs log $file exited $?";
475
476         if (! defined $date) {
477                 warn "failed to parse cvs log for $file\n";
478                 return 0;
479         }
480
481         eval q{use Date::Parse};
482         error($@) if $@;
483         $date=str2time($date, 'UTC');
484         debug("found ctime ".localtime($date)." for $file");
485         return $date;
486 }
487
488 1