]> 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_add ($) { #{{{
121         # filename is relative to the root of the srcdir
122         my $file=shift;
123
124         if (-d "$config{srcdir}/.svn") {
125                 my $parent=dirname($file);
126                 while (! -d "$config{srcdir}/$parent/.svn") {
127                         $file=$parent;
128                         $parent=dirname($file);
129                 }
130                 
131                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
132                         warn("svn add failed\n");
133                 }
134         }
135 } #}}}
136
137 sub rcs_remove ($) { #{{{
138         # filename is relative to the root of the srcdir
139         my $file=shift;
140
141         if (-d "$config{srcdir}/.svn") {
142                 my $parent=dirname($file);
143                 while (! -d "$config{srcdir}/$parent/.svn") {
144                         $file=$parent;
145                         $parent=dirname($file);
146                 }
147                 
148                 if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
149                         warn("svn rm failed\n");
150                 }
151         }
152 } #}}}
153
154 sub rcs_recentchanges ($) { #{{{
155         my $num=shift;
156         my @ret;
157         
158         return unless -d "$config{srcdir}/.svn";
159
160         eval q{
161                 use Date::Parse;
162                 use XML::SAX;
163                 use XML::Simple;
164         };
165         error($@) if $@;
166
167         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
168         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
169         do {
170                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
171         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
172
173         # --limit is only supported on Subversion 1.2.0+
174         my $svn_version=`svn --version -q`;
175         my $svn_limit='';
176         $svn_limit="--limit $num"
177                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
178
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' },
184         );
185         foreach my $logentry (@{$xml->{logentry}}) {
186                 my (@pages, @message);
187
188                 my $rev = $logentry->{revision};
189                 my $user = $logentry->{author};
190
191                 my $when=str2time($logentry->{date}, 'UTC');
192
193                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
194                         push @message, { line => $msgline };
195                 }
196
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;
202                 }
203                 else {
204                         $committype="svn";
205                 }
206
207                 foreach my $file (keys %{$logentry->{paths}}) {
208                         if (length $config{svnpath}) {
209                                 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
210                                 $file=$1;
211                         }
212
213                         my $diffurl=$config{diffurl};
214                         $diffurl=~s/\[\[file\]\]/$file/g;
215                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
216                         $diffurl=~s/\[\[r2\]\]/$rev/g;
217
218                         push @pages, {
219                                 page => pagename($file),
220                                 diffurl => $diffurl,
221                         } if length $file;
222                 }
223                 push @ret, {
224                         rev => $rev,
225                         user => $user,
226                         committype => $committype,
227                         when => $when,
228                         message => [@message],
229                         pages => [@pages],
230                 } if @pages;
231                 return @ret if @ret >= $num;
232         }
233
234         return @ret;
235 } #}}}
236
237 sub rcs_diff ($) { #{{{
238         my $rev=possibly_foolish_untaint(int(shift));
239         return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
240 } #}}}
241
242 sub rcs_getctime ($) { #{{{
243         my $file=shift;
244
245         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
246                 
247         my $child = open(SVNLOG, "-|");
248         if (! $child) {
249                 exec("svn", "log", $file) || error("svn log $file failed to run");
250         }
251
252         my $date;
253         while (<SVNLOG>) {
254                 if (/$svn_log_infoline/) {
255                         $date=$1;
256                 }
257         }
258         close SVNLOG || warn "svn log $file exited $?";
259
260         if (! defined $date) {
261                 warn "failed to parse svn log for $file\n";
262                 return 0;
263         }
264                 
265         eval q{use Date::Parse};
266         error($@) if $@;
267         $date=str2time($date);
268         debug("found ctime ".localtime($date)." for $file");
269         return $date;
270 } #}}}
271
272 1