]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/cvs.pm
Catch up to the new genwrapper hook.
[ikiwiki.git] / IkiWiki / Plugin / cvs.pm
1 #!/usr/pkg/bin/perl
2 package IkiWiki::Plugin::cvs;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 use File::chdir;
9
10 sub import {
11         hook(type => "genwrapper", id => "cvs", call => \&genwrapper);
12         hook(type => "checkconfig", id => "cvs", call => \&checkconfig);
13         hook(type => "getsetup", id => "cvs", call => \&getsetup);
14         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
15         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
16         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
17         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
18         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
19         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
20         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
21         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
22         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
23         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
24 }
25
26 sub genwrapper () {
27         my $check_args=<<"EOF";
28         {
29                 int j;
30                 for (j = 1; j < argc; j++)
31                         if (strstr(argv[j], "New directory") != NULL)
32                                 exit(0);
33         }
34 EOF
35         return $check_args;
36 }
37
38 sub checkconfig () {
39         if (! defined $config{cvspath}) {
40                 $config{cvspath}="ikiwiki";
41         }
42         if (exists $config{cvspath}) {
43                 # code depends on the path not having extraneous slashes
44                 $config{cvspath}=~tr#/#/#s;
45                 $config{cvspath}=~s/\/$//;
46                 $config{cvspath}=~s/^\///;
47         }
48         if (defined $config{cvs_wrapper} && length $config{cvs_wrapper}) {
49                 push @{$config{wrappers}}, {
50                         wrapper => $config{cvs_wrapper},
51                         wrappermode => (defined $config{cvs_wrappermode} ? $config{cvs_wrappermode} : "04755"),
52                 };
53         }
54 }
55
56 sub getsetup () {
57         return
58                 plugin => {
59                         safe => 0, # rcs plugin
60                         rebuild => undef,
61                 },
62                 cvsrepo => {
63                         type => "string",
64                         example => "/cvs/wikirepo",
65                         description => "cvs repository location",
66                         safe => 0, # path
67                         rebuild => 0,
68                 },
69                 cvspath => {
70                         type => "string",
71                         example => "ikiwiki",
72                         description => "path inside repository where the wiki is located",
73                         safe => 0, # paranoia
74                         rebuild => 0,
75                 },
76                 cvs_wrapper => {
77                         type => "string",
78                         example => "/cvs/wikirepo/CVSROOT/post-commit",
79                         description => "cvs post-commit hook to generate (triggered by CVSROOT/loginfo entry",
80                         safe => 0, # file
81                         rebuild => 0,
82                 },
83                 cvs_wrappermode => {
84                         type => "string",
85                         example => '04755',
86                         description => "mode for cvs_wrapper (can safely be made suid)",
87                         safe => 0,
88                         rebuild => 0,
89                 },
90                 historyurl => {
91                         type => "string",
92                         example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]]",
93                         description => "cvsweb url to show file history ([[file]] substituted)",
94                         safe => 1,
95                         rebuild => 1,
96                 },
97                 diffurl => {
98                         type => "string",
99                         example => "http://cvs.example.org/cvsweb.cgi/ikiwiki/[[file]].diff?r1=text&amp;tr1=[[r1]]&amp;r2=text&amp;tr2=[[r2]]",
100                         description => "cvsweb url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
101                         safe => 1,
102                         rebuild => 1,
103                 },
104 }
105
106 sub cvs_info ($$) {
107         my $field=shift;
108         my $file=shift;
109
110         local $CWD = $config{srcdir};
111
112         my $info=`cvs status $file`;
113         my ($ret)=$info=~/^\s*$field:\s*(\S+)/m;
114         return $ret;
115 }
116
117 sub cvs_runcvs(@) {
118         my @cmd = @_;
119         unshift @cmd, 'cvs', '-Q';
120
121         local $CWD = $config{srcdir};
122
123         open(my $savedout, ">&STDOUT");
124         open(STDOUT, ">", "/dev/null");
125         my $ret = system(@cmd);
126         open(STDOUT, ">&", $savedout);
127
128         return ($ret == 0) ? 1 : 0;
129 }
130
131 sub cvs_is_controlling {
132         my $dir=shift;
133         $dir=$config{srcdir} unless defined($dir);
134         return (-d "$dir/CVS") ? 1 : 0;
135 }
136
137 sub rcs_update () {
138         return unless cvs_is_controlling;
139         cvs_runcvs('update', '-dP');
140 }
141
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
145         # for committing.
146         # The file is relative to the srcdir.
147         my $file=shift;
148
149         return unless cvs_is_controlling;
150
151         # For cvs, return the revision of the file when
152         # editing begins.
153         my $rev=cvs_info("Repository revision", "$file");
154         return defined $rev ? $rev : "";
155 }
156
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.
161         my $file=shift;
162         my $message=shift;
163         my $rcstoken=shift;
164         my $user=shift;
165         my $ipaddr=shift;
166
167         return unless cvs_is_controlling;
168
169         if (defined $user) {
170                 $message="web commit by $user".(length $message ? ": $message" : "");
171         }
172         elsif (defined $ipaddr) {
173                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
174         }
175
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
182                 # changed.
183                 cvs_runcvs('update', $file) ||
184                         warn("cvs merge from $oldrev to $rev failed\n");
185         }
186
187         if (! cvs_runcvs('commit', '-m',
188                          IkiWiki::possibly_foolish_untaint $message)) {
189                 my $conflict=readfile("$config{srcdir}/$file");
190                 cvs_runcvs('update', '-C', $file) ||
191                         warn("cvs revert failed\n");
192                 return $conflict;
193         }
194
195         return undef # success
196 }
197
198 sub rcs_commit_staged ($$$) {
199         # Commits all staged changes. Changes can be staged using rcs_add,
200         # rcs_remove, and rcs_rename.
201         my ($message, $user, $ipaddr)=@_;
202
203         if (defined $user) {
204                 $message="web commit by $user".(length $message ? ": $message" : "");
205         }
206         elsif (defined $ipaddr) {
207                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
208         }
209
210         if (! cvs_runcvs('commit', '-m',
211                          IkiWiki::possibly_foolish_untaint $message)) {
212                 warn "cvs staged commit failed\n";
213                 return 1; # failure
214         }
215         return undef # success
216 }
217
218 sub rcs_add ($) {
219         # filename is relative to the root of the srcdir
220         my $file=shift;
221         my $parent=IkiWiki::dirname($file);
222         my @files_to_add = ($file);
223
224         eval q{use File::MimeInfo};
225         error($@) if $@;
226
227         until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
228                 push @files_to_add, $parent;
229                 $parent = IkiWiki::dirname($parent);
230         }
231
232         while ($file = pop @files_to_add) {
233                 if (@files_to_add == 0) {
234                         # file
235                         my $filemime = File::MimeInfo::default($file);
236                         if (defined($filemime) && $filemime eq 'text/plain') {
237                                 cvs_runcvs('add', $file) ||
238                                         warn("cvs add $file failed\n");
239                         } else {
240                                 cvs_runcvs('add', '-kb', $file) ||
241                                         warn("cvs add binary $file failed\n");
242                         }
243                 } else {
244                         # directory
245                         cvs_runcvs('add', $file) ||
246                                 warn("cvs add $file failed\n");
247                 }
248         }
249 }
250
251 sub rcs_remove ($) {
252         # filename is relative to the root of the srcdir
253         my $file=shift;
254
255         return unless cvs_is_controlling;
256
257         cvs_runcvs('rm', '-f', $file) ||
258                 warn("cvs rm $file failed\n");
259 }
260
261 sub rcs_rename ($$) {
262         # filenames relative to the root of the srcdir
263         my ($src, $dest)=@_;
264
265         return unless cvs_is_controlling;
266
267         local $CWD = $config{srcdir};
268
269         if (system("mv", "$src", "$dest") != 0) {
270                 warn("filesystem rename failed\n");
271         }
272
273         rcs_add($dest);
274         rcs_remove($src);
275 }
276
277 sub rcs_recentchanges($) {
278         my $num = shift;
279         my @ret;
280
281         return unless cvs_is_controlling;
282
283         eval q{use Date::Parse};
284         error($@) if $@;
285
286         local $CWD = $config{srcdir};
287
288         # There's no cvsps option to get the last N changesets.
289         # Write full output to a temp file and read backwards.
290
291         eval q{use File::Temp qw/tempfile/};
292         error($@) if $@;
293         eval q{use File::ReadBackwards};
294         error($@) if $@;
295
296         my (undef, $tmpfile) = tempfile(OPEN=>0);
297         system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
298         if ($? == -1) {
299                 error "couldn't run cvsps: $!\n";
300         } elsif (($? >> 8) != 0) {
301                 error "cvsps exited " . ($? >> 8) . ": $!\n";
302         }
303
304         tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
305                 || error "couldn't open $tmpfile for read: $!\n";
306
307         while (my $line = <SPSVC>) {
308                 $line =~ /^$/ || error "expected blank line, got $line";
309
310                 my ($rev, $user, $committype, $when);
311                 my (@message, @pages);
312
313                 # We're reading backwards.
314                 # Forwards, an entry looks like so:
315                 # ---------------------
316                 # PatchSet $rev
317                 # Date: $when
318                 # Author: $user (or user CGI runs as, for web commits)
319                 # Branch: branch
320                 # Tag: tag
321                 # Log:
322                 # @message_lines
323                 # Members:
324                 #       @pages (and revisions)
325                 #
326
327                 while ($line = <SPSVC>) {
328                         last if ($line =~ /^Members:/);
329                         for ($line) {
330                                 s/^\s+//;
331                                 s/\s+$//;
332                         }
333                         my ($page, $revs) = split(/:/, $line);
334                         my ($oldrev, $newrev) = split(/->/, $revs);
335                         $oldrev =~ s/INITIAL/0/;
336                         $newrev =~ s/\(DEAD\)//;
337                         my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
338                         $diffurl=~s/\[\[file\]\]/$page/g;
339                         $diffurl=~s/\[\[r1\]\]/$oldrev/g;
340                         $diffurl=~s/\[\[r2\]\]/$newrev/g;
341                         unshift @pages, {
342                                 page => pagename($page),
343                                 diffurl => $diffurl,
344                         } if length $page;
345                 }
346
347                 while ($line = <SPSVC>) {
348                         last if ($line =~ /^Log:$/);
349                         chomp $line;
350                         unshift @message, { line => $line };
351                 }
352                 $committype = "web";
353                 if (defined $message[0] &&
354                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
355                         $user=defined $2 ? "$2" : "$3";
356                         $message[0]->{line}=$4;
357                 } else {
358                         $committype="cvs";
359                 }
360
361                 $line = <SPSVC>;        # Tag
362                 $line = <SPSVC>;        # Branch
363
364                 $line = <SPSVC>;
365                 if ($line =~ /^Author: (.*)$/) {
366                         $user = $1 unless defined $user && length $user;
367                 } else {
368                         error "expected Author, got $line";
369                 }
370
371                 $line = <SPSVC>;
372                 if ($line =~ /^Date: (.*)$/) {
373                         $when = str2time($1, 'UTC');
374                 } else {
375                         error "expected Date, got $line";
376                 }
377
378                 $line = <SPSVC>;
379                 if ($line =~ /^PatchSet (.*)$/) {
380                         $rev = $1;
381                 } else {
382                         error "expected PatchSet, got $line";
383                 }
384
385                 $line = <SPSVC>;        # ---------------------
386
387                 push @ret, {
388                         rev => $rev,
389                         user => $user,
390                         committype => $committype,
391                         when => $when,
392                         message => [@message],
393                         pages => [@pages],
394                 } if @pages;
395                 last if @ret >= $num;
396         }
397
398         unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
399
400         return @ret;
401 }
402
403 sub rcs_diff ($) {
404         my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
405
406         local $CWD = $config{srcdir};
407
408         # diff output is unavoidably preceded by the cvsps PatchSet entry
409         my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
410         my $blank_lines_seen = 0;
411
412         while (my $line = shift @cvsps) {
413                 $blank_lines_seen++ if ($line =~ /^$/);
414                 last if $blank_lines_seen == 2;
415         }
416
417         if (wantarray) {
418                 return @cvsps;
419         } else {
420                 return join("", @cvsps);
421         }
422 }
423
424 sub rcs_getctime ($) {
425         my $file=shift;
426
427         my $cvs_log_infoline=qr/^date: (.+);\s+author/;
428
429         open CVSLOG, "cvs -Q log -r1.1 '$file' |"
430                 || error "couldn't get cvs log output: $!\n";
431
432         my $date;
433         while (<CVSLOG>) {
434                 if (/$cvs_log_infoline/) {
435                         $date=$1;
436                 }
437         }
438         close CVSLOG || warn "cvs log $file exited $?";
439
440         if (! defined $date) {
441                 warn "failed to parse cvs log for $file\n";
442                 return 0;
443         }
444
445         eval q{use Date::Parse};
446         error($@) if $@;
447         $date=str2time($date, 'UTC');
448         debug("found ctime ".localtime($date)." for $file");
449         return $date;
450 }
451
452 1