]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
Merge branch 'master' into tova
[ikiwiki.git] / IkiWiki / Rcs / svn.pm
1 #!/usr/bin/perl
2
3 package IkiWiki::Rcs::svn;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use POSIX qw(setlocale LC_CTYPE);
9
10 sub import { #{{{
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/^\///;
16         }
17 } #}}}
18
19
20 package IkiWiki;
21
22 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
23 sub find_lc_ctype() {
24         my $current = setlocale(LC_CTYPE());
25         return $current if $current =~ m/UTF-?8$/i;
26
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);
30         }
31
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`)) {
34                 chomp @locale;
35                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
36         }
37
38         # fallback to the current locale
39         return $current;
40 } # }}}
41 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
42
43 sub svn_info ($$) { #{{{
44         my $field=shift;
45         my $file=shift;
46
47         my $info=`LANG=C svn info $file`;
48         my ($ret)=$info=~/^$field: (.*)$/m;
49         return $ret;
50 } #}}}
51
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");
56                 }
57         }
58 } #}}}
59
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
63         # for committing.
64         # The file is relative to the srcdir.
65         my $file=shift;
66         
67         if (-d "$config{srcdir}/.svn") {
68                 # For subversion, return the revision of the file when
69                 # editing begins.
70                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
71                 return defined $rev ? $rev : "";
72         }
73 } #}}}
74
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.
79         my $file=shift;
80         my $message=shift;
81         my $rcstoken=shift;
82         my $user=shift;
83         my $ipaddr=shift;
84
85         if (defined $user) {
86                 $message="web commit by $user".(length $message ? ": $message" : "");
87         }
88         elsif (defined $ipaddr) {
89                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
90         }
91
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
99                         # changed.
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");
103                         }
104                 }
105
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");
113                         }
114                         return $conflict;
115                 }
116         }
117         return undef # success
118 } #}}}
119
120 sub rcs_commit_staged ($$$) {
121         # Commits all staged changes. Changes can be staged using rcs_add,
122         # rcs_remove, and rcs_rename.
123         my ($message, $user, $ipaddr)=@_;
124         
125         if (defined $user) {
126                 $message="web commit by $user".(length $message ? ": $message" : "");
127         }
128         elsif (defined $ipaddr) {
129                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
130         }
131         
132         if (system("svn", "commit", "--quiet",
133                    "--encoding", "UTF-8", "-m",
134                    possibly_foolish_untaint($message),
135                    $config{srcdir}) != 0) {
136                 warn("svn commit failed\n");
137                 return 1; # failure     
138         }
139         return undef # success
140 }
141
142 sub rcs_add ($) { #{{{
143         # filename is relative to the root of the srcdir
144         my $file=shift;
145
146         if (-d "$config{srcdir}/.svn") {
147                 my $parent=dirname($file);
148                 while (! -d "$config{srcdir}/$parent/.svn") {
149                         $file=$parent;
150                         $parent=dirname($file);
151                 }
152                 
153                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
154                         warn("svn add failed\n");
155                 }
156         }
157 } #}}}
158
159 sub rcs_remove ($) { #{{{
160         # filename is relative to the root of the srcdir
161         my $file=shift;
162
163         if (-d "$config{srcdir}/.svn") {
164                 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
165                         warn("svn rm failed\n");
166                 }
167         }
168 } #}}}
169
170 sub rcs_rename ($$) { #{{{
171         # filenames relative to the root of the srcdir
172         my ($src, $dest)=@_;
173         
174         if (-d "$config{srcdir}/.svn") {
175                 # Add parent directory for $dest
176                 my $parent=dirname($dest);
177                 if (! -d "$config{srcdir}/$parent/.svn") {
178                         while (! -d "$config{srcdir}/$parent/.svn") {
179                                 $parent=dirname($dest);
180                         }
181                         if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
182                                 warn("svn add $parent failed\n");
183                         }
184                 }
185
186                 if (system("svn", "mv", "--force", "--quiet", 
187                     "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
188                         warn("svn rename failed\n");
189                 }
190         }
191 } #}}}
192
193 sub rcs_recentchanges ($) { #{{{
194         my $num=shift;
195         my @ret;
196         
197         return unless -d "$config{srcdir}/.svn";
198
199         eval q{
200                 use Date::Parse;
201                 use XML::SAX;
202                 use XML::Simple;
203         };
204         error($@) if $@;
205
206         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
207         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
208         do {
209                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
210         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
211
212         # --limit is only supported on Subversion 1.2.0+
213         my $svn_version=`svn --version -q`;
214         my $svn_limit='';
215         $svn_limit="--limit $num"
216                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
217
218         my $svn_url=svn_info("URL", $config{srcdir});
219         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
220                 ForceArray => [ 'logentry', 'path' ],
221                 GroupTags => { paths => 'path' },
222                 KeyAttr => { path => 'content' },
223         );
224         foreach my $logentry (@{$xml->{logentry}}) {
225                 my (@pages, @message);
226
227                 my $rev = $logentry->{revision};
228                 my $user = $logentry->{author};
229
230                 my $when=str2time($logentry->{date}, 'UTC');
231
232                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
233                         push @message, { line => $msgline };
234                 }
235
236                 my $committype="web";
237                 if (defined $message[0] &&
238                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
239                         $user=defined $2 ? "$2" : "$3";
240                         $message[0]->{line}=$4;
241                 }
242                 else {
243                         $committype="svn";
244                 }
245
246                 foreach my $file (keys %{$logentry->{paths}}) {
247                         if (length $config{svnpath}) {
248                                 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
249                                 $file=$1;
250                         }
251
252                         my $diffurl=$config{diffurl};
253                         $diffurl=~s/\[\[file\]\]/$file/g;
254                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
255                         $diffurl=~s/\[\[r2\]\]/$rev/g;
256
257                         push @pages, {
258                                 page => pagename($file),
259                                 diffurl => $diffurl,
260                         } if length $file;
261                 }
262                 push @ret, {
263                         rev => $rev,
264                         user => $user,
265                         committype => $committype,
266                         when => $when,
267                         message => [@message],
268                         pages => [@pages],
269                 } if @pages;
270                 return @ret if @ret >= $num;
271         }
272
273         return @ret;
274 } #}}}
275
276 sub rcs_diff ($) { #{{{
277         my $rev=possibly_foolish_untaint(int(shift));
278         return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
279 } #}}}
280
281 sub rcs_getctime ($) { #{{{
282         my $file=shift;
283
284         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
285                 
286         my $child = open(SVNLOG, "-|");
287         if (! $child) {
288                 exec("svn", "log", $file) || error("svn log $file failed to run");
289         }
290
291         my $date;
292         while (<SVNLOG>) {
293                 if (/$svn_log_infoline/) {
294                         $date=$1;
295                 }
296         }
297         close SVNLOG || warn "svn log $file exited $?";
298
299         if (! defined $date) {
300                 warn "failed to parse svn log for $file\n";
301                 return 0;
302         }
303                 
304         eval q{use Date::Parse};
305         error($@) if $@;
306         $date=str2time($date);
307         debug("found ctime ".localtime($date)." for $file");
308         return $date;
309 } #}}}
310
311 1