]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
update
[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;
9                 
10 my $svn_webcommit=qr/^web commit (by (\w+)|from (\d+\.\d+\.\d+\.\d+)):?(.*)/;
11
12 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
13 sub find_lc_ctype() {
14         my $current = setlocale(LC_CTYPE());
15         return $current if $current =~ m/UTF-?8$/i;
16
17         # Make some obvious attempts to avoid calling `locale -a`
18         foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
19                 return $locale if setlocale(LC_CTYPE(), $locale);
20         }
21
22         # Try to get all available locales and pick the first UTF-8 one found.
23         if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
24                 chomp @locale;
25                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
26         }
27
28         # fallback to the current locale
29         return $current;
30 } # }}}
31 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
32
33 sub svn_info ($$) { #{{{
34         my $field=shift;
35         my $file=shift;
36
37         my $info=`LANG=C svn info $file`;
38         my ($ret)=$info=~/^$field: (.*)$/m;
39         return $ret;
40 } #}}}
41
42 sub rcs_update () { #{{{
43         if (-d "$config{srcdir}/.svn") {
44                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
45                         warn("svn update failed\n");
46                 }
47         }
48 } #}}}
49
50 sub rcs_prepedit ($) { #{{{
51         # Prepares to edit a file under revision control. Returns a token
52         # that must be passed into rcs_commit when the file is ready
53         # for committing.
54         # The file is relative to the srcdir.
55         my $file=shift;
56         
57         if (-d "$config{srcdir}/.svn") {
58                 # For subversion, return the revision of the file when
59                 # editing begins.
60                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
61                 return defined $rev ? $rev : "";
62         }
63 } #}}}
64
65 sub rcs_commit ($$$) { #{{{
66         # Tries to commit the page; returns undef on _success_ and
67         # a version of the page with the rcs's conflict markers on failure.
68         # The file is relative to the srcdir.
69         my $file=shift;
70         my $message=shift;
71         my $rcstoken=shift;
72
73         if (-d "$config{srcdir}/.svn") {
74                 # Check to see if the page has been changed by someone
75                 # else since rcs_prepedit was called.
76                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
77                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
78                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
79                         # Merge their changes into the file that we've
80                         # changed.
81                         chdir($config{srcdir}); # svn merge wants to be here
82                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
83                                    "$config{srcdir}/$file") != 0) {
84                                 warn("svn merge -r$oldrev:$rev failed\n");
85                         }
86                 }
87
88                 if (system("svn", "commit", "--quiet", 
89                            "--encoding", "UTF-8", "-m",
90                            possibly_foolish_untaint($message),
91                            "$config{srcdir}") != 0) {
92                         my $conflict=readfile("$config{srcdir}/$file");
93                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
94                                 warn("svn revert failed\n");
95                         }
96                         return $conflict;
97                 }
98         }
99         return undef # success
100 } #}}}
101
102 sub rcs_add ($) { #{{{
103         # filename is relative to the root of the srcdir
104         my $file=shift;
105
106         if (-d "$config{srcdir}/.svn") {
107                 my $parent=dirname($file);
108                 while (! -d "$config{srcdir}/$parent/.svn") {
109                         $file=$parent;
110                         $parent=dirname($file);
111                 }
112                 
113                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
114                         warn("svn add failed\n");
115                 }
116         }
117 } #}}}
118
119 sub rcs_recentchanges ($) { #{{{
120         my $num=shift;
121         my @ret;
122         
123         return unless -d "$config{srcdir}/.svn";
124
125         eval q{use Date::Parse};
126         eval q{use Time::Duration};
127         eval q{use XML::SAX};
128         eval q{use XML::Simple};
129
130         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
131         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
132         do {
133                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
134         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
135
136         # --limit is only supported on Subversion 1.2.0+
137         my $svn_version=`svn --version -q`;
138         my $svn_limit='';
139         $svn_limit="--limit $num"
140                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
141
142         my $svn_url=svn_info("URL", $config{srcdir});
143         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
144                 ForceArray => [ 'logentry', 'path' ],
145                 GroupTags => { paths => 'path' },
146                 KeyAttr => { path => 'content' },
147         );
148         foreach my $logentry (@{$xml->{logentry}}) {
149                 my (@pages, @message);
150
151                 my $rev = $logentry->{revision};
152                 my $user = $logentry->{author};
153
154                 my $when=time - str2time($logentry->{date}, 'UTC');
155
156                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
157                         push @message, { line => $msgline };
158                 }
159
160                 my $committype="web";
161                 if (defined $message[0] &&
162                     $message[0]->{line}=~/$svn_webcommit/) {
163                         $user=defined $2 ? "$2" : "$3";
164                         $message[0]->{line}=$4;
165                 }
166                 else {
167                         $committype="svn";
168                 }
169
170                 foreach (keys %{$logentry->{paths}}) {
171                         next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
172                         my $file=$1;
173                         my $diffurl=$config{diffurl};
174                         $diffurl=~s/\[\[file\]\]/$file/g;
175                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
176                         $diffurl=~s/\[\[r2\]\]/$rev/g;
177                         push @pages, {
178                                 page => pagename($file),
179                                 diffurl => $diffurl,
180                         } if length $file;
181                 }
182                 push @ret, { rev => $rev,
183                         user => $user,
184                         committype => $committype,
185                         when => $when,
186                         message => [@message],
187                         pages => [@pages],
188                 } if @pages;
189                 return @ret if @ret >= $num;
190         }
191
192         return @ret;
193 } #}}}
194
195 sub rcs_notify () { #{{{
196         if (! exists $ENV{REV}) {
197                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
198         }
199         my $rev=int(possibly_foolish_untaint($ENV{REV}));
200         
201         my $user=`svnlook author $config{svnrepo} -r $rev`;
202         chomp $user;
203         my $message=`svnlook log $config{svnrepo} -r $rev`;
204         if ($message=~/$svn_webcommit/) {
205                 $user=defined $2 ? "$2" : "$3";
206                 $message=$4;
207         }
208
209         my @changed_pages;
210         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
211                 chomp $change;
212                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
213                         push @changed_pages, $1;
214                 }
215         }
216                 
217         require IkiWiki::UserInfo;
218         my @email_recipients=commit_notify_list($user, @changed_pages);
219         if (@email_recipients) {
220                 # TODO: if a commit spans multiple pages, this will send
221                 # subscribers a diff that might contain pages they did not
222                 # sign up for. Should separate the diff per page and
223                 # reassemble into one mail with just the pages subscribed to.
224                 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
225
226                 my $subject="$config{wikiname} update of ";
227                 if (@changed_pages > 2) {
228                         $subject.="$changed_pages[0] $changed_pages[1] etc";
229                 }
230                 else {
231                         $subject.=join(" ", @changed_pages);
232                 }
233                 $subject.=" by $user";
234
235                 my $template=template("notifymail.tmpl");
236                 $template->param(
237                         wikiname => $config{wikiname},
238                         diff => $diff,
239                         user => $user,
240                         message => $message,
241                 );
242                 
243                 eval q{use Mail::Sendmail};
244                 foreach my $email (@email_recipients) {
245                         sendmail(
246                                 To => $email,
247                                 From => "$config{wikiname} <$config{adminemail}>",
248                                 Subject => $subject,
249                                 Message => $template->output,
250                         ) or error("Failed to send update notification mail");
251                 }
252         }
253 } #}}}
254
255 sub rcs_getctime ($) { #{{{
256         my $file=shift;
257
258         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
259                 
260         my $child = open(SVNLOG, "-|");
261         if (! $child) {
262                 exec("svn", "log", $file) || error("svn log $file failed to run");
263         }
264
265         my $date;
266         while (<SVNLOG>) {
267                 if (/$svn_log_infoline/) {
268                         $date=$1;
269                 }
270         }
271         close SVNLOG || warn "svn log $file exited $?";
272
273         if (! defined $date) {
274                 warn "failed to parse svn log for $file\n";
275                 return 0;
276         }
277                 
278         eval q{use Date::Parse};
279         $date=str2time($date);
280         debug("found ctime ".localtime($date)." for $file");
281         return $date;
282 } #}}}
283
284 1