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);
25 if (! defined $config{svnpath}) {
26 $config{svnpath}="trunk";
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/^\///;
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"),
45 safe => 0, # rcs plugin
50 example => "/svn/wiki",
51 description => "subversion repository location",
58 description => "path inside repository where the wiki is located",
64 example => "/svn/wikirepo/hooks/post-commit",
65 description => "svn post-commit hook to generate",
72 description => "mode for svn_wrapper (can safely be made suid)",
78 example => "http://svn.example.org/trunk/[[file]]",
79 description => "viewvc url to show file history ([[file]] substituted)",
85 example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
86 description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
92 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
94 my $current = setlocale(LC_CTYPE());
95 return $current if $current =~ m/UTF-?8$/i;
97 # Make some obvious attempts to avoid calling `locale -a`
98 foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
99 return $locale if setlocale(LC_CTYPE(), $locale);
102 # Try to get all available locales and pick the first UTF-8 one found.
103 if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
105 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
108 # fallback to the current locale
111 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
117 my $info=`LANG=C svn info $file`;
118 my ($ret)=$info=~/^$field: (.*)$/m;
123 if (-d "$config{srcdir}/.svn") {
124 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
125 warn("svn update failed\n");
130 sub rcs_prepedit ($) {
131 # Prepares to edit a file under revision control. Returns a token
132 # that must be passed into rcs_commit when the file is ready
134 # The file is relative to the srcdir.
137 if (-d "$config{srcdir}/.svn") {
138 # For subversion, return the revision of the file when
140 my $rev=svn_info("Revision", "$config{srcdir}/$file");
141 return defined $rev ? $rev : "";
145 sub rcs_commit ($$$;$$) {
146 # Tries to commit the page; returns undef on _success_ and
147 # a version of the page with the rcs's conflict markers on failure.
148 # The file is relative to the srcdir.
156 $message="web commit by $user".(length $message ? ": $message" : "");
158 elsif (defined $ipaddr) {
159 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
162 if (-d "$config{srcdir}/.svn") {
163 # Check to see if the page has been changed by someone
164 # else since rcs_prepedit was called.
165 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
166 my $rev=svn_info("Revision", "$config{srcdir}/$file");
167 if (defined $rev && defined $oldrev && $rev != $oldrev) {
168 # Merge their changes into the file that we've
170 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
171 "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
172 warn("svn merge -r$oldrev:$rev failed\n");
176 if (system("svn", "commit", "--quiet",
177 "--encoding", "UTF-8", "-m",
178 IkiWiki::possibly_foolish_untaint($message),
179 $config{srcdir}) != 0) {
180 my $conflict=readfile("$config{srcdir}/$file");
181 if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
182 warn("svn revert failed\n");
187 return undef # success
190 sub rcs_commit_staged ($$$) {
191 # Commits all staged changes. Changes can be staged using rcs_add,
192 # rcs_remove, and rcs_rename.
193 my ($message, $user, $ipaddr)=@_;
196 $message="web commit by $user".(length $message ? ": $message" : "");
198 elsif (defined $ipaddr) {
199 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
202 if (system("svn", "commit", "--quiet",
203 "--encoding", "UTF-8", "-m",
204 IkiWiki::possibly_foolish_untaint($message),
205 $config{srcdir}) != 0) {
206 warn("svn commit failed\n");
209 return undef # success
213 # filename is relative to the root of the srcdir
216 if (-d "$config{srcdir}/.svn") {
217 my $parent=IkiWiki::dirname($file);
218 while (! -d "$config{srcdir}/$parent/.svn") {
220 $parent=IkiWiki::dirname($file);
223 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
224 warn("svn add failed\n");
230 # filename is relative to the root of the srcdir
233 if (-d "$config{srcdir}/.svn") {
234 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
235 warn("svn rm failed\n");
240 sub rcs_rename ($$) {
241 # filenames relative to the root of the srcdir
244 if (-d "$config{srcdir}/.svn") {
245 # Add parent directory for $dest
246 my $parent=dirname($dest);
247 if (! -d "$config{srcdir}/$parent/.svn") {
248 while (! -d "$config{srcdir}/$parent/.svn") {
249 $parent=dirname($dest);
251 if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
252 warn("svn add $parent failed\n");
256 if (system("svn", "mv", "--force", "--quiet",
257 "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
258 warn("svn rename failed\n");
263 sub rcs_recentchanges ($) {
267 return unless -d "$config{srcdir}/.svn";
276 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
277 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
279 $XML::Simple::PREFERRED_PARSER = pop @parsers;
280 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
282 # --limit is only supported on Subversion 1.2.0+
283 my $svn_version=`svn --version -q`;
285 $svn_limit="--limit $num"
286 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
288 my $svn_url=svn_info("URL", $config{srcdir});
289 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
290 ForceArray => [ 'logentry', 'path' ],
291 GroupTags => { paths => 'path' },
292 KeyAttr => { path => 'content' },
294 foreach my $logentry (@{$xml->{logentry}}) {
295 my (@pages, @message);
297 my $rev = $logentry->{revision};
298 my $user = $logentry->{author};
300 my $when=str2time($logentry->{date}, 'UTC');
302 foreach my $msgline (split(/\n/, $logentry->{msg})) {
303 push @message, { line => $msgline };
306 my $committype="web";
307 if (defined $message[0] &&
308 $message[0]->{line}=~/$config{web_commit_regexp}/) {
309 $user=defined $2 ? "$2" : "$3";
310 $message[0]->{line}=$4;
316 foreach my $file (keys %{$logentry->{paths}}) {
317 if (length $config{svnpath}) {
318 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
322 my $diffurl=defined $config{diffurl} ? $config{diffurl} : "";
323 $diffurl=~s/\[\[file\]\]/$file/g;
324 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
325 $diffurl=~s/\[\[r2\]\]/$rev/g;
328 page => pagename($file),
335 committype => $committype,
337 message => [@message],
340 return @ret if @ret >= $num;
347 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
348 return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
351 sub rcs_getctime ($) {
354 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
356 my $child = open(SVNLOG, "-|");
358 exec("svn", "log", $file) || error("svn log $file failed to run");
363 if (/$svn_log_infoline/) {
367 close SVNLOG || warn "svn log $file exited $?";
369 if (! defined $date) {
370 warn "failed to parse svn log for $file\n";
374 eval q{use Date::Parse};
376 $date=str2time($date);
377 debug("found ctime ".localtime($date)." for $file");