]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/cvs.pm
remove two plugins that are unrelated to cvs
[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         eval q{use IPC::Cmd};
107         error($@) if $@;
108
109         chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
110
111         my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
112                 IPC::Cmd::run(command => $cmd, verbose => 0);
113         if (! $success) {
114                 warn(join(" ", @$cmd) . " exited with code $error_code\n");
115                 warn(join "", @$stderr_buf);
116         }
117         return $success;
118 }
119
120 sub cvs_shquote_commit ($) {
121         my $message = shift;
122         my $test_message = "CVS autodiscover quoting CVS";
123
124         eval q{use String::ShellQuote};
125         error($@) if $@;
126         eval q{use IPC::Cmd};
127         error($@) if $@;
128
129         my $cmd = ['echo', shell_quote($test_message)];
130         my ($success, $error_code, $full_buf, $stdout_buf, $stderr_buf) =
131                 IPC::Cmd::run(command => $cmd, verbose => 0);
132         if ((grep /'$test_message'/, @$stdout_buf) > 0) {
133                 return IkiWiki::possibly_foolish_untaint($message);
134         } else {
135                 return shell_quote(IkiWiki::possibly_foolish_untaint($message));
136         }
137 }
138
139 sub cvs_is_controlling {
140         my $dir=shift;
141         $dir=$config{srcdir} unless defined($dir);
142         return (-d "$dir/CVS") ? 1 : 0;
143 }
144
145 sub rcs_update () {
146         return unless cvs_is_controlling;
147         cvs_runcvs(['update', '-dP']);
148 }
149
150 sub rcs_prepedit ($) {
151         # Prepares to edit a file under revision control. Returns a token
152         # that must be passed into rcs_commit when the file is ready
153         # for committing.
154         # The file is relative to the srcdir.
155         my $file=shift;
156
157         return unless cvs_is_controlling;
158
159         # For cvs, return the revision of the file when
160         # editing begins.
161         my $rev=cvs_info("Repository revision", "$file");
162         return defined $rev ? $rev : "";
163 }
164
165 sub rcs_commit ($$$;$$) {
166         # Tries to commit the page; returns undef on _success_ and
167         # a version of the page with the rcs's conflict markers on failure.
168         # The file is relative to the srcdir.
169         my $file=shift;
170         my $message=shift;
171         my $rcstoken=shift;
172         my $user=shift;
173         my $ipaddr=shift;
174
175         return unless cvs_is_controlling;
176
177         if (defined $user) {
178                 $message="web commit by $user".(length $message ? ": $message" : "");
179         }
180         elsif (defined $ipaddr) {
181                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
182         }
183
184         # Check to see if the page has been changed by someone
185         # else since rcs_prepedit was called.
186         my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
187         my $rev=cvs_info("Repository revision", "$config{srcdir}/$file");
188         if (defined $rev && defined $oldrev && $rev != $oldrev) {
189                 # Merge their changes into the file that we've
190                 # changed.
191                 cvs_runcvs(['update', $file]) ||
192                         warn("cvs merge from $oldrev to $rev failed\n");
193         }
194
195         if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
196                 my $conflict=readfile("$config{srcdir}/$file");
197                 cvs_runcvs(['update', '-C', $file]) ||
198                         warn("cvs revert failed\n");
199                 return $conflict;
200         }
201
202         return undef # success
203 }
204
205 sub rcs_commit_staged ($$$) {
206         # Commits all staged changes. Changes can be staged using rcs_add,
207         # rcs_remove, and rcs_rename.
208         my ($message, $user, $ipaddr)=@_;
209
210         if (defined $user) {
211                 $message="web commit by $user".(length $message ? ": $message" : "");
212         }
213         elsif (defined $ipaddr) {
214                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
215         }
216
217         if (! cvs_runcvs(['commit', '-m', cvs_shquote_commit $message])) {
218                 warn "cvs staged commit failed\n";
219                 return 1; # failure
220         }
221         return undef # success
222 }
223
224 sub rcs_add ($) {
225         # filename is relative to the root of the srcdir
226         my $file=shift;
227         my $parent=IkiWiki::dirname($file);
228         my @files_to_add = ($file);
229
230         eval q{use File::MimeInfo};
231         error($@) if $@;
232
233         until ((length($parent) == 0) || cvs_is_controlling("$config{srcdir}/$parent")){
234                 push @files_to_add, $parent;
235                 $parent = IkiWiki::dirname($parent);
236         }
237
238         while ($file = pop @files_to_add) {
239                 if (@files_to_add == 0) {
240                         # file
241                         my $filemime = File::MimeInfo::default($file);
242                         if (defined($filemime) && $filemime eq 'text/plain') {
243                                 cvs_runcvs(['add', $file]) ||
244                                         warn("cvs add $file failed\n");
245                         } else {
246                                 cvs_runcvs(['add', '-kb', $file]) ||
247                                         warn("cvs add binary $file failed\n");
248                         }
249                 } else {
250                         # directory
251                         cvs_runcvs(['add', $file]) ||
252                                 warn("cvs add $file failed\n");
253                 }
254         }
255 }
256
257 sub rcs_remove ($) {
258         # filename is relative to the root of the srcdir
259         my $file=shift;
260
261         return unless cvs_is_controlling;
262
263         cvs_runcvs(['rm', '-f', $file]) ||
264                 warn("cvs rm $file failed\n");
265 }
266
267 sub rcs_rename ($$) {
268         # filenames relative to the root of the srcdir
269         my ($src, $dest)=@_;
270
271         return unless cvs_is_controlling;
272
273         chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
274
275         if (system("mv", "$src", "$dest") != 0) {
276                 warn("filesystem rename failed\n");
277         }
278
279         rcs_add($dest);
280         rcs_remove($src);
281 }
282
283 sub rcs_recentchanges($) {
284         my $num = shift;
285         my @ret;
286
287         return unless cvs_is_controlling;
288
289         eval q{use Date::Parse};
290         error($@) if $@;
291
292         chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
293
294         # There's no cvsps option to get the last N changesets.
295         # Write full output to a temp file and read backwards.
296
297         eval q{use File::Temp qw/tempfile/};
298         error($@) if $@;
299         eval q{use File::ReadBackwards};
300         error($@) if $@;
301
302         my (undef, $tmpfile) = tempfile(OPEN=>0);
303         system("env TZ=UTC cvsps -q --cvs-direct -z 30 -x >$tmpfile");
304         if ($? == -1) {
305                 error "couldn't run cvsps: $!\n";
306         } elsif (($? >> 8) != 0) {
307                 error "cvsps exited " . ($? >> 8) . ": $!\n";
308         }
309
310         tie(*SPSVC, 'File::ReadBackwards', $tmpfile)
311                 || error "couldn't open $tmpfile for read: $!\n";
312
313         while (my $line = <SPSVC>) {
314                 $line =~ /^$/ || error "expected blank line, got $line";
315
316                 my ($rev, $user, $committype, $when);
317                 my (@message, @pages);
318
319                 # We're reading backwards.
320                 # Forwards, an entry looks like so:
321                 # ---------------------
322                 # PatchSet $rev
323                 # Date: $when
324                 # Author: $user (or user CGI runs as, for web commits)
325                 # Branch: branch
326                 # Tag: tag
327                 # Log:
328                 # @message_lines
329                 # Members:
330                 #       @pages (and revisions)
331                 #
332
333                 while ($line = <SPSVC>) {
334                         last if ($line =~ /^Members:/);
335                         for ($line) {
336                                 s/^\s+//;
337                                 s/\s+$//;
338                         }
339                         my ($page, $revs) = split(/:/, $line);
340                         my ($oldrev, $newrev) = split(/->/, $revs);
341                         $oldrev =~ s/INITIAL/0/;
342                         $newrev =~ s/\(DEAD\)//;
343                         my $diffurl = defined $config{diffurl} ? $config{diffurl} : "";
344                         $diffurl=~s/\[\[file\]\]/$page/g;
345                         $diffurl=~s/\[\[r1\]\]/$oldrev/g;
346                         $diffurl=~s/\[\[r2\]\]/$newrev/g;
347                         unshift @pages, {
348                                 page => pagename($page),
349                                 diffurl => $diffurl,
350                         } if length $page;
351                 }
352
353                 while ($line = <SPSVC>) {
354                         last if ($line =~ /^Log:$/);
355                         chomp $line;
356                         unshift @message, { line => $line };
357                 }
358                 $committype = "web";
359                 if (defined $message[0] &&
360                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
361                         $user=defined $2 ? "$2" : "$3";
362                         $message[0]->{line}=$4;
363                 } else {
364                         $committype="cvs";
365                 }
366
367                 $line = <SPSVC>;        # Tag
368                 $line = <SPSVC>;        # Branch
369
370                 $line = <SPSVC>;
371                 if ($line =~ /^Author: (.*)$/) {
372                         $user = $1 unless defined $user && length $user;
373                 } else {
374                         error "expected Author, got $line";
375                 }
376
377                 $line = <SPSVC>;
378                 if ($line =~ /^Date: (.*)$/) {
379                         $when = str2time($1, 'UTC');
380                 } else {
381                         error "expected Date, got $line";
382                 }
383
384                 $line = <SPSVC>;
385                 if ($line =~ /^PatchSet (.*)$/) {
386                         $rev = $1;
387                 } else {
388                         error "expected PatchSet, got $line";
389                 }
390
391                 $line = <SPSVC>;        # ---------------------
392
393                 push @ret, {
394                         rev => $rev,
395                         user => $user,
396                         committype => $committype,
397                         when => $when,
398                         message => [@message],
399                         pages => [@pages],
400                 } if @pages;
401                 last if @ret >= $num;
402         }
403
404         unlink($tmpfile) || error "couldn't unlink $tmpfile: $!\n";
405
406         return @ret;
407 }
408
409 sub rcs_diff ($) {
410         my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
411
412         chdir $config{srcdir} || error("Cannot chdir to $config{srcdir}: $!");
413
414         # diff output is unavoidably preceded by the cvsps PatchSet entry
415         my @cvsps = `env TZ=UTC cvsps -q --cvs-direct -z 30 -g -s $rev`;
416         my $blank_lines_seen = 0;
417
418         while (my $line = shift @cvsps) {
419                 $blank_lines_seen++ if ($line =~ /^$/);
420                 last if $blank_lines_seen == 2;
421         }
422
423         if (wantarray) {
424                 return @cvsps;
425         } else {
426                 return join("", @cvsps);
427         }
428 }
429
430 sub rcs_getctime ($) {
431         my $file=shift;
432
433         my $cvs_log_infoline=qr/^date: (.+);\s+author/;
434
435         open CVSLOG, "cvs -Q log -r1.1 '$file' |"
436                 || error "couldn't get cvs log output: $!\n";
437
438         my $date;
439         while (<CVSLOG>) {
440                 if (/$cvs_log_infoline/) {
441                         $date=$1;
442                 }
443         }
444         close CVSLOG || warn "cvs log $file exited $?";
445
446         if (! defined $date) {
447                 warn "failed to parse cvs log for $file\n";
448                 return 0;
449         }
450
451         eval q{use Date::Parse};
452         error($@) if $@;
453         $date=str2time($date, 'UTC');
454         debug("found ctime ".localtime($date)." for $file");
455         return $date;
456 }
457
458 1