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