6 use POSIX qw(setlocale LC_CTYPE);
8 package IkiWiki::Rcs::svn;
11 if (exists $IkiWiki::config{svnpath}) {
12 # code depends on the path not having extraneous slashes
13 $IkiWiki::config{svnpath}=~tr#/#/#s;
14 $IkiWiki::config{svnpath}=~s/\/$//;
15 $IkiWiki::config{svnpath}=~s/^\///;
22 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
24 my $current = setlocale(LC_CTYPE());
25 return $current if $current =~ m/UTF-?8$/i;
27 # Make some obvious attempts to avoid calling `locale -a`
28 foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
29 return $locale if setlocale(LC_CTYPE(), $locale);
32 # Try to get all available locales and pick the first UTF-8 one found.
33 if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
35 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
38 # fallback to the current locale
41 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
43 sub svn_info ($$) { #{{{
47 my $info=`LANG=C svn info $file`;
48 my ($ret)=$info=~/^$field: (.*)$/m;
52 sub rcs_update () { #{{{
53 if (-d "$config{srcdir}/.svn") {
54 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
55 warn("svn update failed\n");
60 sub rcs_prepedit ($) { #{{{
61 # Prepares to edit a file under revision control. Returns a token
62 # that must be passed into rcs_commit when the file is ready
64 # The file is relative to the srcdir.
67 if (-d "$config{srcdir}/.svn") {
68 # For subversion, return the revision of the file when
70 my $rev=svn_info("Revision", "$config{srcdir}/$file");
71 return defined $rev ? $rev : "";
75 sub rcs_commit ($$$;$$) { #{{{
76 # Tries to commit the page; returns undef on _success_ and
77 # a version of the page with the rcs's conflict markers on failure.
78 # The file is relative to the srcdir.
86 $message="web commit by $user".(length $message ? ": $message" : "");
88 elsif (defined $ipaddr) {
89 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
92 if (-d "$config{srcdir}/.svn") {
93 # Check to see if the page has been changed by someone
94 # else since rcs_prepedit was called.
95 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
96 my $rev=svn_info("Revision", "$config{srcdir}/$file");
97 if (defined $rev && defined $oldrev && $rev != $oldrev) {
98 # Merge their changes into the file that we've
100 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
101 "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
102 warn("svn merge -r$oldrev:$rev failed\n");
106 if (system("svn", "commit", "--quiet",
107 "--encoding", "UTF-8", "-m",
108 possibly_foolish_untaint($message),
109 $config{srcdir}) != 0) {
110 my $conflict=readfile("$config{srcdir}/$file");
111 if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
112 warn("svn revert failed\n");
117 return undef # success
120 sub rcs_add ($) { #{{{
121 # filename is relative to the root of the srcdir
124 if (-d "$config{srcdir}/.svn") {
125 my $parent=dirname($file);
126 while (! -d "$config{srcdir}/$parent/.svn") {
128 $parent=dirname($file);
131 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
132 warn("svn add failed\n");
137 sub rcs_recentchanges ($) { #{{{
141 return unless -d "$config{srcdir}/.svn";
150 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
151 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
153 $XML::Simple::PREFERRED_PARSER = pop @parsers;
154 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
156 # --limit is only supported on Subversion 1.2.0+
157 my $svn_version=`svn --version -q`;
159 $svn_limit="--limit $num"
160 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
162 my $svn_url=svn_info("URL", $config{srcdir});
163 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
164 ForceArray => [ 'logentry', 'path' ],
165 GroupTags => { paths => 'path' },
166 KeyAttr => { path => 'content' },
168 foreach my $logentry (@{$xml->{logentry}}) {
169 my (@pages, @message);
171 my $rev = $logentry->{revision};
172 my $user = $logentry->{author};
174 my $when=time - str2time($logentry->{date}, 'UTC');
176 foreach my $msgline (split(/\n/, $logentry->{msg})) {
177 push @message, { line => $msgline };
180 my $committype="web";
181 if (defined $message[0] &&
182 $message[0]->{line}=~/$config{web_commit_regexp}/) {
183 $user=defined $2 ? "$2" : "$3";
184 $message[0]->{line}=$4;
190 foreach my $file (keys %{$logentry->{paths}}) {
191 if (length $config{svnpath}) {
192 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
196 my $diffurl=$config{diffurl};
197 $diffurl=~s/\[\[file\]\]/$file/g;
198 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
199 $diffurl=~s/\[\[r2\]\]/$rev/g;
202 page => pagename($file),
206 push @ret, { rev => $rev,
208 committype => $committype,
210 message => [@message],
213 return @ret if @ret >= $num;
219 sub rcs_notify () { #{{{
220 if (! exists $ENV{REV}) {
221 error(gettext("REV is not set, not running from svn post-commit hook, cannot send notifications"));
223 my $rev=int(possibly_foolish_untaint($ENV{REV}));
225 my $user=`svnlook author $config{svnrepo} -r $rev`;
228 my $message=`svnlook log $config{svnrepo} -r $rev`;
229 if ($message=~/$config{web_commit_regexp}/) {
230 $user=defined $2 ? "$2" : "$3";
235 foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
237 if (length $config{svnpath}) {
238 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
239 push @changed_pages, $1;
243 push @changed_pages, $change;
247 require IkiWiki::UserInfo;
253 `svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
254 }, $user, @changed_pages);
257 sub rcs_getctime ($) { #{{{
260 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
262 my $child = open(SVNLOG, "-|");
264 exec("svn", "log", $file) || error("svn log $file failed to run");
269 if (/$svn_log_infoline/) {
273 close SVNLOG || warn "svn log $file exited $?";
275 if (! defined $date) {
276 warn "failed to parse svn log for $file\n";
280 eval q{use Date::Parse};
282 $date=str2time($date);
283 debug("found ctime ".localtime($date)." for $file");