]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/svn.pm
fix in a branch
[ikiwiki.git] / IkiWiki / Plugin / svn.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::svn;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use POSIX qw(setlocale LC_CTYPE);
8
9 sub import {
10         hook(type => "checkconfig", id => "svn", call => \&checkconfig);
11         hook(type => "getsetup", id => "svn", 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 checkconfig () {
25         if (! defined $config{svnpath}) {
26                 $config{svnpath}="trunk";
27         }
28         if (exists $config{svnpath}) {
29                 # code depends on the path not having extraneous slashes
30                 $config{svnpath}=~tr#/#/#s;
31                 $config{svnpath}=~s/\/$//;
32                 $config{svnpath}=~s/^\///;
33         }
34         if (defined $config{svn_wrapper} && length $config{svn_wrapper}) {
35                 push @{$config{wrappers}}, {
36                         wrapper => $config{svn_wrapper},
37                         wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
38                 };
39         }
40 }
41
42 sub getsetup () {
43         return
44                 plugin => {
45                         safe => 0, # rcs plugin
46                         rebuild => undef,
47                         section => "rcs",
48                 },
49                 svnrepo => {
50                         type => "string",
51                         example => "/svn/wiki",
52                         description => "subversion repository location",
53                         safe => 0, # path
54                         rebuild => 0,
55                 },
56                 svnpath => {
57                         type => "string",
58                         example => "trunk",
59                         description => "path inside repository where the wiki is located",
60                         safe => 0, # paranoia
61                         rebuild => 0,
62                 },
63                 svn_wrapper => {
64                         type => "string",
65                         example => "/svn/wikirepo/hooks/post-commit",
66                         description => "svn post-commit hook to generate",
67                         safe => 0, # file
68                         rebuild => 0,
69                 },
70                 svn_wrappermode => {
71                         type => "string",
72                         example => '04755',
73                         description => "mode for svn_wrapper (can safely be made suid)",
74                         safe => 0,
75                         rebuild => 0,
76                 },
77                 historyurl => {
78                         type => "string",
79                         example => "http://svn.example.org/trunk/[[file]]",
80                         description => "viewvc url to show file history ([[file]] substituted)",
81                         safe => 1,
82                         rebuild => 1,
83                 },
84                 diffurl => {
85                         type => "string",
86                         example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
87                         description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
88                         safe => 1,
89                         rebuild => 1,
90                 },
91 }
92
93 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
94 sub find_lc_ctype() {
95         my $current = setlocale(LC_CTYPE());
96         return $current if $current =~ m/UTF-?8$/i;
97
98         # Make some obvious attempts to avoid calling `locale -a`
99         foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
100                 return $locale if setlocale(LC_CTYPE(), $locale);
101         }
102
103         # Try to get all available locales and pick the first UTF-8 one found.
104         if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
105                 chomp @locale;
106                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
107         }
108
109         # fallback to the current locale
110         return $current;
111 }
112 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
113
114 sub svn_info ($$) {
115         my $field=shift;
116         my $file=shift;
117
118         my $info=`LANG=C svn info $file`;
119         my ($ret)=$info=~/^$field: (.*)$/m;
120         return $ret;
121 }
122
123 sub rcs_update () {
124         if (-d "$config{srcdir}/.svn") {
125                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
126                         warn("svn update failed\n");
127                 }
128         }
129 }
130
131 sub rcs_prepedit ($) {
132         # Prepares to edit a file under revision control. Returns a token
133         # that must be passed into rcs_commit when the file is ready
134         # for committing.
135         # The file is relative to the srcdir.
136         my $file=shift;
137         
138         if (-d "$config{srcdir}/.svn") {
139                 # For subversion, return the revision of the file when
140                 # editing begins.
141                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
142                 return defined $rev ? $rev : "";
143         }
144 }
145
146 sub rcs_commit ($$$;$$) {
147         # Tries to commit the page; returns undef on _success_ and
148         # a version of the page with the rcs's conflict markers on failure.
149         # The file is relative to the srcdir.
150         my $file=shift;
151         my $message=shift;
152         my $rcstoken=shift;
153         my $user=shift;
154         my $ipaddr=shift;
155
156         if (defined $user) {
157                 $message="web commit by $user".(length $message ? ": $message" : "");
158         }
159         elsif (defined $ipaddr) {
160                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
161         }
162
163         if (-d "$config{srcdir}/.svn") {
164                 # Check to see if the page has been changed by someone
165                 # else since rcs_prepedit was called.
166                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
167                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
168                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
169                         # Merge their changes into the file that we've
170                         # changed.
171                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
172                                    "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
173                                 warn("svn merge -r$oldrev:$rev failed\n");
174                         }
175                 }
176
177                 if (system("svn", "commit", "--quiet", 
178                            "--encoding", "UTF-8", "-m",
179                            IkiWiki::possibly_foolish_untaint($message),
180                            $config{srcdir}) != 0) {
181                         my $conflict=readfile("$config{srcdir}/$file");
182                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
183                                 warn("svn revert failed\n");
184                         }
185                         return $conflict;
186                 }
187         }
188         return undef # success
189 }
190
191 sub rcs_commit_staged ($$$) {
192         # Commits all staged changes. Changes can be staged using rcs_add,
193         # rcs_remove, and rcs_rename.
194         my ($message, $user, $ipaddr)=@_;
195         
196         if (defined $user) {
197                 $message="web commit by $user".(length $message ? ": $message" : "");
198         }
199         elsif (defined $ipaddr) {
200                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
201         }
202         
203         if (system("svn", "commit", "--quiet",
204                    "--encoding", "UTF-8", "-m",
205                    IkiWiki::possibly_foolish_untaint($message),
206                    $config{srcdir}) != 0) {
207                 warn("svn commit failed\n");
208                 return 1; # failure     
209         }
210         return undef # success
211 }
212
213 sub rcs_add ($) {
214         # filename is relative to the root of the srcdir
215         my $file=shift;
216
217         if (-d "$config{srcdir}/.svn") {
218                 my $parent=IkiWiki::dirname($file);
219                 while (! -d "$config{srcdir}/$parent/.svn") {
220                         $file=$parent;
221                         $parent=IkiWiki::dirname($file);
222                 }
223                 
224                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
225                         warn("svn add failed\n");
226                 }
227         }
228 }
229
230 sub rcs_remove ($) {
231         # filename is relative to the root of the srcdir
232         my $file=shift;
233
234         if (-d "$config{srcdir}/.svn") {
235                 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
236                         warn("svn rm failed\n");
237                 }
238         }
239 }
240
241 sub rcs_rename ($$) {
242         # filenames relative to the root of the srcdir
243         my ($src, $dest)=@_;
244         
245         if (-d "$config{srcdir}/.svn") {
246                 # Add parent directory for $dest
247                 my $parent=IkiWiki::dirname($dest);
248                 if (! -d "$config{srcdir}/$parent/.svn") {
249                         while (! -d "$config{srcdir}/$parent/.svn") {
250                                 $parent=IkiWiki::dirname($dest);
251                         }
252                         if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
253                                 warn("svn add $parent failed\n");
254                         }
255                 }
256
257                 if (system("svn", "mv", "--force", "--quiet", 
258                     "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
259                         warn("svn rename failed\n");
260                 }
261         }
262 }
263
264 sub rcs_recentchanges ($) {
265         my $num=shift;
266         my @ret;
267         
268         return unless -d "$config{srcdir}/.svn";
269
270         eval q{
271                 use Date::Parse;
272                 use XML::SAX;
273                 use XML::Simple;
274         };
275         error($@) if $@;
276
277         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
278         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
279         do {
280                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
281         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
282
283         # --limit is only supported on Subversion 1.2.0+
284         my $svn_version=`svn --version -q`;
285         my $svn_limit='';
286         $svn_limit="--limit $num"
287                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
288
289         my $svn_url=svn_info("URL", $config{srcdir});
290         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
291                 ForceArray => [ 'logentry', 'path' ],
292                 GroupTags => { paths => 'path' },
293                 KeyAttr => { path => 'content' },
294         );
295         foreach my $logentry (@{$xml->{logentry}}) {
296                 my (@pages, @message);
297
298                 my $rev = $logentry->{revision};
299                 my $user = $logentry->{author};
300
301                 my $when=str2time($logentry->{date}, 'UTC');
302
303                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
304                         push @message, { line => $msgline };
305                 }
306
307                 my $committype="web";
308                 if (defined $message[0] &&
309                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
310                         $user=defined $2 ? "$2" : "$3";
311                         $message[0]->{line}=$4;
312                 }
313                 else {
314                         $committype="svn";
315                 }
316
317                 foreach my $file (keys %{$logentry->{paths}}) {
318                         if (length $config{svnpath}) {
319                                 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
320                                 $file=$1;
321                         }
322
323                         my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
324                         $diffurl=~s/\[\[file\]\]/$file/g;
325                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
326                         $diffurl=~s/\[\[r2\]\]/$rev/g;
327
328                         push @pages, {
329                                 page => pagename($file),
330                                 diffurl => $diffurl,
331                         } if length $file;
332                 }
333                 push @ret, {
334                         rev => $rev,
335                         user => $user,
336                         committype => $committype,
337                         when => $when,
338                         message => [@message],
339                         pages => [@pages],
340                 } if @pages;
341                 return @ret if @ret >= $num;
342         }
343
344         return @ret;
345 }
346
347 sub rcs_diff ($) {
348         my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
349         return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
350 }
351
352 sub rcs_getctime ($) {
353         my $file=shift;
354
355         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
356                 
357         my $child = open(SVNLOG, "-|");
358         if (! $child) {
359                 exec("svn", "log", $file) || error("svn log $file failed to run");
360         }
361
362         my $date;
363         while (<SVNLOG>) {
364                 if (/$svn_log_infoline/) {
365                         $date=$1;
366                 }
367         }
368         close SVNLOG || warn "svn log $file exited $?";
369
370         if (! defined $date) {
371                 warn "failed to parse svn log for $file\n";
372                 return 0;
373         }
374                 
375         eval q{use Date::Parse};
376         error($@) if $@;
377         $date=str2time($date);
378         debug("found ctime ".localtime($date)." for $file");
379         return $date;
380 }
381
382 1