]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
* Don't truncate git commit messages to the first line in RecentChanges,
[ikiwiki.git] / IkiWiki / Rcs / svn.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use POSIX qw(setlocale LC_CTYPE);
7
8 package IkiWiki::Rcs::svn;
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_recentchanges ($) { #{{{
138         my $num=shift;
139         my @ret;
140         
141         return unless -d "$config{srcdir}/.svn";
142
143         eval q{
144                 use Date::Parse;
145                 use XML::SAX;
146                 use XML::Simple;
147         };
148         error($@) if $@;
149
150         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
151         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
152         do {
153                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
154         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
155
156         # --limit is only supported on Subversion 1.2.0+
157         my $svn_version=`svn --version -q`;
158         my $svn_limit='';
159         $svn_limit="--limit $num"
160                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
161
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' },
167         );
168         foreach my $logentry (@{$xml->{logentry}}) {
169                 my (@pages, @message);
170
171                 my $rev = $logentry->{revision};
172                 my $user = $logentry->{author};
173
174                 my $when=time - str2time($logentry->{date}, 'UTC');
175
176                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
177                         push @message, { line => $msgline };
178                 }
179
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;
185                 }
186                 else {
187                         $committype="svn";
188                 }
189
190                 foreach my $file (keys %{$logentry->{paths}}) {
191                         if (length $config{svnpath}) {
192                                 next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
193                                 $file=$1;
194                         }
195
196                         my $diffurl=$config{diffurl};
197                         $diffurl=~s/\[\[file\]\]/$file/g;
198                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
199                         $diffurl=~s/\[\[r2\]\]/$rev/g;
200
201                         push @pages, {
202                                 page => pagename($file),
203                                 diffurl => $diffurl,
204                         } if length $file;
205                 }
206                 push @ret, { rev => $rev,
207                         user => $user,
208                         committype => $committype,
209                         when => $when,
210                         message => [@message],
211                         pages => [@pages],
212                 } if @pages;
213                 return @ret if @ret >= $num;
214         }
215
216         return @ret;
217 } #}}}
218
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"));
222         }
223         my $rev=int(possibly_foolish_untaint($ENV{REV}));
224         
225         my $user=`svnlook author $config{svnrepo} -r $rev`;
226         chomp $user;
227         
228         my $message=`svnlook log $config{svnrepo} -r $rev`;
229         if ($message=~/$config{web_commit_regexp}/) {
230                 $user=defined $2 ? "$2" : "$3";
231                 $message=$4;
232         }
233
234         my @changed_pages;
235         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
236                 chomp $change;
237                 if (length $config{svnpath}) {
238                         if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
239                                 push @changed_pages, $1;
240                         }
241                 }
242                 else {
243                         push @changed_pages, $change;
244                 }
245         }
246         
247         require IkiWiki::UserInfo;
248         send_commit_mails(
249                 sub {
250                         return $message;
251                 },
252                 sub {
253                         `svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
254                 }, $user, @changed_pages);
255 } #}}}
256
257 sub rcs_getctime ($) { #{{{
258         my $file=shift;
259
260         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
261                 
262         my $child = open(SVNLOG, "-|");
263         if (! $child) {
264                 exec("svn", "log", $file) || error("svn log $file failed to run");
265         }
266
267         my $date;
268         while (<SVNLOG>) {
269                 if (/$svn_log_infoline/) {
270                         $date=$1;
271                 }
272         }
273         close SVNLOG || warn "svn log $file exited $?";
274
275         if (! defined $date) {
276                 warn "failed to parse svn log for $file\n";
277                 return 0;
278         }
279                 
280         eval q{use Date::Parse};
281         error($@) if $@;
282         $date=str2time($date);
283         debug("found ctime ".localtime($date)." for $file");
284         return $date;
285 } #}}}
286
287 1