6 use POSIX qw(setlocale LC_CTYPE);
10 my $svn_webcommit=qr/^web commit (by (\w+)|from (\d+\.\d+\.\d+\.\d+)):?(.*)/;
12 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
14 my $current = setlocale(LC_CTYPE());
15 return $current if $current =~ m/UTF-?8$/i;
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);
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`)) {
25 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
28 # fallback to the current locale
31 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
33 sub svn_info ($$) { #{{{
37 my $info=`LANG=C svn info $file`;
38 my ($ret)=$info=~/^$field: (.*)$/m;
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");
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
54 # The file is relative to the srcdir.
57 if (-d "$config{srcdir}/.svn") {
58 # For subversion, return the revision of the file when
60 my $rev=svn_info("Revision", "$config{srcdir}/$file");
61 return defined $rev ? $rev : "";
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.
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
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");
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");
99 return undef # success
102 sub rcs_add ($) { #{{{
103 # filename is relative to the root of the srcdir
106 if (-d "$config{srcdir}/.svn") {
107 my $parent=dirname($file);
108 while (! -d "$config{srcdir}/$parent/.svn") {
110 $parent=dirname($file);
113 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
114 warn("svn add failed\n");
119 sub rcs_recentchanges ($) { #{{{
123 return unless -d "$config{srcdir}/.svn";
133 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
134 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
136 $XML::Simple::PREFERRED_PARSER = pop @parsers;
137 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
139 # --limit is only supported on Subversion 1.2.0+
140 my $svn_version=`svn --version -q`;
142 $svn_limit="--limit $num"
143 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
145 my $svn_url=svn_info("URL", $config{srcdir});
146 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
147 ForceArray => [ 'logentry', 'path' ],
148 GroupTags => { paths => 'path' },
149 KeyAttr => { path => 'content' },
151 foreach my $logentry (@{$xml->{logentry}}) {
152 my (@pages, @message);
154 my $rev = $logentry->{revision};
155 my $user = $logentry->{author};
157 my $when=time - str2time($logentry->{date}, 'UTC');
159 foreach my $msgline (split(/\n/, $logentry->{msg})) {
160 push @message, { line => $msgline };
163 my $committype="web";
164 if (defined $message[0] &&
165 $message[0]->{line}=~/$svn_webcommit/) {
166 $user=defined $2 ? "$2" : "$3";
167 $message[0]->{line}=$4;
173 foreach (keys %{$logentry->{paths}}) {
174 next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
176 my $diffurl=$config{diffurl};
177 $diffurl=~s/\[\[file\]\]/$file/g;
178 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
179 $diffurl=~s/\[\[r2\]\]/$rev/g;
181 page => pagename($file),
185 push @ret, { rev => $rev,
187 committype => $committype,
189 message => [@message],
192 return @ret if @ret >= $num;
198 sub rcs_notify () { #{{{
199 if (! exists $ENV{REV}) {
200 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
202 my $rev=int(possibly_foolish_untaint($ENV{REV}));
204 my $user=`svnlook author $config{svnrepo} -r $rev`;
206 my $message=`svnlook log $config{svnrepo} -r $rev`;
207 if ($message=~/$svn_webcommit/) {
208 $user=defined $2 ? "$2" : "$3";
213 foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
215 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
216 push @changed_pages, $1;
220 require IkiWiki::UserInfo;
221 my @email_recipients=commit_notify_list($user, @changed_pages);
222 if (@email_recipients) {
223 # TODO: if a commit spans multiple pages, this will send
224 # subscribers a diff that might contain pages they did not
225 # sign up for. Should separate the diff per page and
226 # reassemble into one mail with just the pages subscribed to.
227 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
229 my $subject="$config{wikiname} update of ";
230 if (@changed_pages > 2) {
231 $subject.="$changed_pages[0] $changed_pages[1] etc";
234 $subject.=join(" ", @changed_pages);
236 $subject.=" by $user";
238 my $template=template("notifymail.tmpl");
240 wikiname => $config{wikiname},
246 eval q{use Mail::Sendmail};
248 foreach my $email (@email_recipients) {
251 From => "$config{wikiname} <$config{adminemail}>",
253 Message => $template->output,
254 ) or error("Failed to send update notification mail");
259 sub rcs_getctime ($) { #{{{
262 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
264 my $child = open(SVNLOG, "-|");
266 exec("svn", "log", $file) || error("svn log $file failed to run");
271 if (/$svn_log_infoline/) {
275 close SVNLOG || warn "svn log $file exited $?";
277 if (! defined $date) {
278 warn "failed to parse svn log for $file\n";
282 eval q{use Date::Parse};
284 $date=str2time($date);
285 debug("found ctime ".localtime($date)." for $file");