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