8 use POSIX qw(setlocale LC_CTYPE);
10 hook(type => "checkconfig", id => "svn", call => sub { #{{{
11 if (! defined $config{diffurl}) {
14 if (! defined $config{svnpath}) {
15 $config{svnpath}="trunk";
17 if (exists $config{svnpath}) {
18 # code depends on the path not having extraneous slashes
19 $config{svnpath}=~tr#/#/#s;
20 $config{svnpath}=~s/\/$//;
21 $config{svnpath}=~s/^\///;
23 if (exists $config{svn_wrapper}) {
24 push @{$config{wrappers}}, {
25 wrapper => $config{svn_wrapper},
26 wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
31 hook(type => "getsetup", id => "svn", call => sub { #{{{
35 example => "/svn/wiki",
36 description => "subversion repository location",
43 description => "path inside repository where the wiki is located",
49 example => "/svn/wikirepo/hooks/post-commit",
50 description => "svn post-commit executable to generate",
57 description => "mode for svn_wrapper (can safely be made suid)",
63 example => "http://svn.example.org/trunk/[[file]]",
64 description => "viewvc url to show file history ([[file]] substituted)",
70 example => "http://svn.example.org/trunk/[[file]]?root=wiki&r1=[[r1]]&r2=[[r2]]",
71 description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
77 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
79 my $current = setlocale(LC_CTYPE());
80 return $current if $current =~ m/UTF-?8$/i;
82 # Make some obvious attempts to avoid calling `locale -a`
83 foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
84 return $locale if setlocale(LC_CTYPE(), $locale);
87 # Try to get all available locales and pick the first UTF-8 one found.
88 if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
90 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
93 # fallback to the current locale
96 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
98 sub svn_info ($$) { #{{{
102 my $info=`LANG=C svn info $file`;
103 my ($ret)=$info=~/^$field: (.*)$/m;
107 sub rcs_update () { #{{{
108 if (-d "$config{srcdir}/.svn") {
109 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
110 warn("svn update failed\n");
115 sub rcs_prepedit ($) { #{{{
116 # Prepares to edit a file under revision control. Returns a token
117 # that must be passed into rcs_commit when the file is ready
119 # The file is relative to the srcdir.
122 if (-d "$config{srcdir}/.svn") {
123 # For subversion, return the revision of the file when
125 my $rev=svn_info("Revision", "$config{srcdir}/$file");
126 return defined $rev ? $rev : "";
130 sub rcs_commit ($$$;$$) { #{{{
131 # Tries to commit the page; returns undef on _success_ and
132 # a version of the page with the rcs's conflict markers on failure.
133 # The file is relative to the srcdir.
141 $message="web commit by $user".(length $message ? ": $message" : "");
143 elsif (defined $ipaddr) {
144 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
147 if (-d "$config{srcdir}/.svn") {
148 # Check to see if the page has been changed by someone
149 # else since rcs_prepedit was called.
150 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
151 my $rev=svn_info("Revision", "$config{srcdir}/$file");
152 if (defined $rev && defined $oldrev && $rev != $oldrev) {
153 # Merge their changes into the file that we've
155 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
156 "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
157 warn("svn merge -r$oldrev:$rev failed\n");
161 if (system("svn", "commit", "--quiet",
162 "--encoding", "UTF-8", "-m",
163 possibly_foolish_untaint($message),
164 $config{srcdir}) != 0) {
165 my $conflict=readfile("$config{srcdir}/$file");
166 if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
167 warn("svn revert failed\n");
172 return undef # success
175 sub rcs_commit_staged ($$$) {
176 # Commits all staged changes. Changes can be staged using rcs_add,
177 # rcs_remove, and rcs_rename.
178 my ($message, $user, $ipaddr)=@_;
181 $message="web commit by $user".(length $message ? ": $message" : "");
183 elsif (defined $ipaddr) {
184 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
187 if (system("svn", "commit", "--quiet",
188 "--encoding", "UTF-8", "-m",
189 possibly_foolish_untaint($message),
190 $config{srcdir}) != 0) {
191 warn("svn commit failed\n");
194 return undef # success
197 sub rcs_add ($) { #{{{
198 # filename is relative to the root of the srcdir
201 if (-d "$config{srcdir}/.svn") {
202 my $parent=dirname($file);
203 while (! -d "$config{srcdir}/$parent/.svn") {
205 $parent=dirname($file);
208 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
209 warn("svn add failed\n");
214 sub rcs_remove ($) { #{{{
215 # filename is relative to the root of the srcdir
218 if (-d "$config{srcdir}/.svn") {
219 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
220 warn("svn rm failed\n");
225 sub rcs_rename ($$) { #{{{
226 # filenames relative to the root of the srcdir
229 if (-d "$config{srcdir}/.svn") {
230 # Add parent directory for $dest
231 my $parent=dirname($dest);
232 if (! -d "$config{srcdir}/$parent/.svn") {
233 while (! -d "$config{srcdir}/$parent/.svn") {
234 $parent=dirname($dest);
236 if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
237 warn("svn add $parent failed\n");
241 if (system("svn", "mv", "--force", "--quiet",
242 "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
243 warn("svn rename failed\n");
248 sub rcs_recentchanges ($) { #{{{
252 return unless -d "$config{srcdir}/.svn";
261 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
262 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
264 $XML::Simple::PREFERRED_PARSER = pop @parsers;
265 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
267 # --limit is only supported on Subversion 1.2.0+
268 my $svn_version=`svn --version -q`;
270 $svn_limit="--limit $num"
271 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
273 my $svn_url=svn_info("URL", $config{srcdir});
274 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
275 ForceArray => [ 'logentry', 'path' ],
276 GroupTags => { paths => 'path' },
277 KeyAttr => { path => 'content' },
279 foreach my $logentry (@{$xml->{logentry}}) {
280 my (@pages, @message);
282 my $rev = $logentry->{revision};
283 my $user = $logentry->{author};
285 my $when=str2time($logentry->{date}, 'UTC');
287 foreach my $msgline (split(/\n/, $logentry->{msg})) {
288 push @message, { line => $msgline };
291 my $committype="web";
292 if (defined $message[0] &&
293 $message[0]->{line}=~/$config{web_commit_regexp}/) {
294 $user=defined $2 ? "$2" : "$3";
295 $message[0]->{line}=$4;
301 foreach my $file (keys %{$logentry->{paths}}) {
302 if (length $config{svnpath}) {
303 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
307 my $diffurl=$config{diffurl};
308 $diffurl=~s/\[\[file\]\]/$file/g;
309 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
310 $diffurl=~s/\[\[r2\]\]/$rev/g;
313 page => pagename($file),
320 committype => $committype,
322 message => [@message],
325 return @ret if @ret >= $num;
331 sub rcs_diff ($) { #{{{
332 my $rev=possibly_foolish_untaint(int(shift));
333 return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
336 sub rcs_getctime ($) { #{{{
339 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
341 my $child = open(SVNLOG, "-|");
343 exec("svn", "log", $file) || error("svn log $file failed to run");
348 if (/$svn_log_infoline/) {
352 close SVNLOG || warn "svn log $file exited $?";
354 if (! defined $date) {
355 warn "failed to parse svn log for $file\n";
359 eval q{use Date::Parse};
361 $date=str2time($date);
362 debug("found ctime ".localtime($date)." for $file");