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