3 package IkiWiki::Rcs::svn;
8 use POSIX qw(setlocale LC_CTYPE);
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_remove ($) { #{{{
138 # filename is relative to the root of the srcdir
141 if (-d "$config{srcdir}/.svn") {
142 my $parent=dirname($file);
143 while (! -d "$config{srcdir}/$parent/.svn") {
145 $parent=dirname($file);
148 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
149 warn("svn rm failed\n");
154 sub rcs_recentchanges ($) { #{{{
158 return unless -d "$config{srcdir}/.svn";
167 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
168 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
170 $XML::Simple::PREFERRED_PARSER = pop @parsers;
171 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
173 # --limit is only supported on Subversion 1.2.0+
174 my $svn_version=`svn --version -q`;
176 $svn_limit="--limit $num"
177 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
179 my $svn_url=svn_info("URL", $config{srcdir});
180 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
181 ForceArray => [ 'logentry', 'path' ],
182 GroupTags => { paths => 'path' },
183 KeyAttr => { path => 'content' },
185 foreach my $logentry (@{$xml->{logentry}}) {
186 my (@pages, @message);
188 my $rev = $logentry->{revision};
189 my $user = $logentry->{author};
191 my $when=str2time($logentry->{date}, 'UTC');
193 foreach my $msgline (split(/\n/, $logentry->{msg})) {
194 push @message, { line => $msgline };
197 my $committype="web";
198 if (defined $message[0] &&
199 $message[0]->{line}=~/$config{web_commit_regexp}/) {
200 $user=defined $2 ? "$2" : "$3";
201 $message[0]->{line}=$4;
207 foreach my $file (keys %{$logentry->{paths}}) {
208 if (length $config{svnpath}) {
209 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
213 my $diffurl=$config{diffurl};
214 $diffurl=~s/\[\[file\]\]/$file/g;
215 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
216 $diffurl=~s/\[\[r2\]\]/$rev/g;
219 page => pagename($file),
226 committype => $committype,
228 message => [@message],
231 return @ret if @ret >= $num;
237 sub rcs_diff ($) { #{{{
238 my $rev=possibly_foolish_untaint(int(shift));
239 return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
242 sub rcs_getctime ($) { #{{{
245 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
247 my $child = open(SVNLOG, "-|");
249 exec("svn", "log", $file) || error("svn log $file failed to run");
254 if (/$svn_log_infoline/) {
258 close SVNLOG || warn "svn log $file exited $?";
260 if (! defined $date) {
261 warn "failed to parse svn log for $file\n";
265 eval q{use Date::Parse};
267 $date=str2time($date);
268 debug("found ctime ".localtime($date)." for $file");