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