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