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
51 example => "/svn/wiki",
52 description => "subversion repository location",
59 description => "path inside repository where the wiki is located",
65 example => "/svn/wikirepo/hooks/post-commit",
66 description => "svn post-commit hook to generate",
73 description => "mode for svn_wrapper (can safely be made suid)",
79 example => "http://svn.example.org/trunk/[[file]]",
80 description => "viewvc url to show file history ([[file]] substituted)",
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)",
93 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
95 my $current = setlocale(LC_CTYPE());
96 return $current if $current =~ m/UTF-?8$/i;
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);
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`)) {
106 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
109 # fallback to the current locale
112 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
118 my $info=`LANG=C svn info $file`;
119 my ($ret)=$info=~/^$field: (.*)$/m;
124 if (-d "$config{srcdir}/.svn") {
125 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
126 warn("svn update failed\n");
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
135 # The file is relative to the srcdir.
138 if (-d "$config{srcdir}/.svn") {
139 # For subversion, return the revision of the file when
141 my $rev=svn_info("Revision", "$config{srcdir}/$file");
142 return defined $rev ? $rev : "";
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.
157 $message="web commit by $user".(length $message ? ": $message" : "");
159 elsif (defined $ipaddr) {
160 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
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
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");
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");
188 return undef # success
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)=@_;
197 $message="web commit by $user".(length $message ? ": $message" : "");
199 elsif (defined $ipaddr) {
200 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
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");
210 return undef # success
214 # filename is relative to the root of the srcdir
217 if (-d "$config{srcdir}/.svn") {
218 my $parent=IkiWiki::dirname($file);
219 while (! -d "$config{srcdir}/$parent/.svn") {
221 $parent=IkiWiki::dirname($file);
224 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
225 warn("svn add failed\n");
231 # filename is relative to the root of the srcdir
234 if (-d "$config{srcdir}/.svn") {
235 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
236 warn("svn rm failed\n");
241 sub rcs_rename ($$) {
242 # filenames relative to the root of the srcdir
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);
252 if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
253 warn("svn add $parent failed\n");
257 if (system("svn", "mv", "--force", "--quiet",
258 "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
259 warn("svn rename failed\n");
264 sub rcs_recentchanges ($) {
268 return unless -d "$config{srcdir}/.svn";
277 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
278 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
280 $XML::Simple::PREFERRED_PARSER = pop @parsers;
281 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
283 # --limit is only supported on Subversion 1.2.0+
284 my $svn_version=`svn --version -q`;
286 $svn_limit="--limit $num"
287 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
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' },
295 foreach my $logentry (@{$xml->{logentry}}) {
296 my (@pages, @message);
298 my $rev = $logentry->{revision};
299 my $user = $logentry->{author};
301 my $when=str2time($logentry->{date}, 'UTC');
303 foreach my $msgline (split(/\n/, $logentry->{msg})) {
304 push @message, { line => $msgline };
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;
317 foreach my $file (keys %{$logentry->{paths}}) {
318 if (length $config{svnpath}) {
319 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
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;
329 page => pagename($file),
336 committype => $committype,
338 message => [@message],
341 return @ret if @ret >= $num;
348 my $rev=IkiWiki::possibly_foolish_untaint(int(shift));
349 return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
352 sub rcs_getctime ($) {
355 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
357 my $child = open(SVNLOG, "-|");
359 exec("svn", "log", $file) || error("svn log $file failed to run");
364 if (/$svn_log_infoline/) {
368 close SVNLOG || warn "svn log $file exited $?";
370 if (! defined $date) {
371 warn "failed to parse svn log for $file\n";
375 eval q{use Date::Parse};
377 $date=str2time($date);
378 debug("found ctime ".localtime($date)." for $file");