2 package IkiWiki::Plugin::svn;
7 use POSIX qw(setlocale LC_CTYPE);
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 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
26 if (! defined $config{svnpath}) {
27 $config{svnpath}="trunk";
29 if (exists $config{svnpath}) {
30 # code depends on the path not having extraneous slashes
31 $config{svnpath}=~tr#/#/#s;
32 $config{svnpath}=~s/\/$//;
33 $config{svnpath}=~s/^\///;
35 if (defined $config{svn_wrapper} && length $config{svn_wrapper}) {
36 push @{$config{wrappers}}, {
37 wrapper => $config{svn_wrapper},
38 wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
46 safe => 0, # rcs plugin
52 example => "/svn/wiki",
53 description => "subversion repository location",
60 description => "path inside repository where the wiki is located",
66 example => "/svn/wikirepo/hooks/post-commit",
67 description => "svn post-commit hook to generate",
74 description => "mode for svn_wrapper (can safely be made suid)",
80 example => "http://svn.example.org/trunk/[[file]]",
81 description => "viewvc url to show file history ([[file]] substituted)",
87 example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
88 description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
94 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
96 my $current = setlocale(LC_CTYPE());
97 return $current if $current =~ m/UTF-?8$/i;
99 # Make some obvious attempts to avoid calling `locale -a`
100 foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
101 return $locale if setlocale(LC_CTYPE(), $locale);
104 # Try to get all available locales and pick the first UTF-8 one found.
105 if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
107 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
110 # fallback to the current locale
113 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
119 my $info=`LANG=C svn info $file`;
120 my ($ret)=$info=~/^$field: (.*)$/m;
125 if (-d "$config{srcdir}/.svn") {
126 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
127 warn("svn update failed\n");
132 sub rcs_prepedit ($) {
133 # Prepares to edit a file under revision control. Returns a token
134 # that must be passed into rcs_commit when the file is ready
136 # The file is relative to the srcdir.
139 if (-d "$config{srcdir}/.svn") {
140 # For subversion, return the revision of the file when
142 my $rev=svn_info("Revision", "$config{srcdir}/$file");
143 return defined $rev ? $rev : "";
147 sub commitmessage (@) {
150 if (defined $params{session}) {
151 if (defined $params{session}->param("name")) {
152 return "web commit by ".
153 $params{session}->param("name").
154 (length $params{message} ? ": $params{message}" : "");
156 elsif (defined $params{session}->remote_addr()) {
157 return "web commit from ".
158 $params{session}->remote_addr().
159 (length $params{message} ? ": $params{message}" : "");
162 return $params{message};
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.
171 if (-d "$config{srcdir}/.svn") {
172 # Check to see if the page has been changed by someone
173 # else since rcs_prepedit was called.
174 my ($oldrev)=$params{token}=~/^([0-9]+)$/; # untaint
175 my $rev=svn_info("Revision", "$config{srcdir}/$params{file}");
176 if (defined $rev && defined $oldrev && $rev != $oldrev) {
177 # Merge their changes into the file that we've
179 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
180 "$config{srcdir}/$params{file}", "$config{srcdir}/$params{file}") != 0) {
181 warn("svn merge -r$oldrev:$rev failed\n");
185 if (system("svn", "commit", "--quiet",
186 "--encoding", "UTF-8", "-m",
187 IkiWiki::possibly_foolish_untaint(commitmessage(%params)),
188 $config{srcdir}) != 0) {
189 my $conflict=readfile("$config{srcdir}/$params{file}");
190 if (system("svn", "revert", "--quiet", "$config{srcdir}/$params{file}") != 0) {
191 warn("svn revert failed\n");
196 return undef # success
199 sub rcs_commit_staged (@) {
200 # Commits all staged changes. Changes can be staged using rcs_add,
201 # rcs_remove, and rcs_rename.
204 if (system("svn", "commit", "--quiet",
205 "--encoding", "UTF-8", "-m",
206 IkiWiki::possibly_foolish_untaint(commitmessage(%params)),
207 $config{srcdir}) != 0) {
208 warn("svn commit failed\n");
211 return undef # success
215 # filename is relative to the root of the srcdir
218 if (-d "$config{srcdir}/.svn") {
219 my $parent=IkiWiki::dirname($file);
220 while (! -d "$config{srcdir}/$parent/.svn") {
222 $parent=IkiWiki::dirname($file);
225 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
226 warn("svn add failed\n");
232 # filename is relative to the root of the srcdir
235 if (-d "$config{srcdir}/.svn") {
236 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
237 warn("svn rm failed\n");
242 sub rcs_rename ($$) {
243 # filenames relative to the root of the srcdir
246 if (-d "$config{srcdir}/.svn") {
247 # Add parent directory for $dest
248 my $parent=IkiWiki::dirname($dest);
249 if (! -d "$config{srcdir}/$parent/.svn") {
250 while (! -d "$config{srcdir}/$parent/.svn") {
251 $parent=IkiWiki::dirname($dest);
253 if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
254 warn("svn add $parent failed\n");
258 if (system("svn", "mv", "--force", "--quiet",
259 "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
260 warn("svn rename failed\n");
265 sub rcs_recentchanges ($) {
269 return unless -d "$config{srcdir}/.svn";
278 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
279 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
281 $XML::Simple::PREFERRED_PARSER = pop @parsers;
282 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
284 # --limit is only supported on Subversion 1.2.0+
285 my $svn_version=`svn --version -q`;
287 $svn_limit="--limit $num"
288 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
290 my $svn_url=svn_info("URL", $config{srcdir});
291 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
292 ForceArray => [ 'logentry', 'path' ],
293 GroupTags => { paths => 'path' },
294 KeyAttr => { path => 'content' },
296 foreach my $logentry (@{$xml->{logentry}}) {
297 my (@pages, @message);
299 my $rev = $logentry->{revision};
300 my $user = $logentry->{author};
302 my $when=str2time($logentry->{date}, 'UTC');
304 foreach my $msgline (split(/\n/, $logentry->{msg})) {
305 push @message, { line => $msgline };
308 my $committype="web";
309 if (defined $message[0] &&
310 $message[0]->{line}=~/$config{web_commit_regexp}/) {
311 $user=defined $2 ? "$2" : "$3";
312 $message[0]->{line}=$4;
318 foreach my $file (keys %{$logentry->{paths}}) {
319 if (length $config{svnpath}) {
320 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
324 my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
325 $diffurl=~s/\[\[file\]\]/$file/g;
326 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
327 $diffurl=~s/\[\[r2\]\]/$rev/g;
330 page => pagename($file),
337 committype => $committype,
339 message => [@message],
342 return @ret if @ret >= $num;
349 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
351 return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
356 my ($lastfile, $lastmtime, $lastctime);
361 if (defined $lastfile && $lastfile eq $file) {
362 return $lastmtime, $lastctime;
366 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
368 my $child = open(SVNLOG, "-|");
370 exec("svn", "log", "$config{srcdir}/$file") || error("svn log failed to run");
375 if (/$svn_log_infoline/) {
377 $mdate=$1 unless defined $mdate;
380 close SVNLOG || error "svn log exited $?";
382 if (! defined $cdate) {
383 error "failed to parse svn log for $file";
386 eval q{use Date::Parse};
389 $lastctime=str2time($cdate);
390 $lastmtime=str2time($mdate);
391 return $lastmtime, $lastctime;
396 sub rcs_getctime ($) {
399 return (findtimes($file))[1];
402 sub rcs_getmtime ($) {
405 return (findtimes($file))[0];