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";
125 eval q{use Date::Parse};
126 eval q{use Time::Duration};
127 eval q{use XML::SAX};
128 eval q{use XML::Simple};
130 # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
131 my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
133 $XML::Simple::PREFERRED_PARSER = pop @parsers;
134 } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
136 # --limit is only supported on Subversion 1.2.0+
137 my $svn_version=`svn --version -q`;
139 $svn_limit="--limit $num"
140 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
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' },
148 foreach my $logentry (@{$xml->{logentry}}) {
149 my (@pages, @message);
151 my $rev = $logentry->{revision};
152 my $user = $logentry->{author};
154 my $when=time - str2time($logentry->{date}, 'UTC');
156 foreach my $msgline (split(/\n/, $logentry->{msg})) {
157 push @message, { line => $msgline };
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;
170 foreach (keys %{$logentry->{paths}}) {
171 next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
173 my $diffurl=$config{diffurl};
174 $diffurl=~s/\[\[file\]\]/$file/g;
175 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
176 $diffurl=~s/\[\[r2\]\]/$rev/g;
178 page => pagename($file),
182 push @ret, { rev => $rev,
184 committype => $committype,
186 message => [@message],
189 return @ret if @ret >= $num;
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");
199 my $rev=int(possibly_foolish_untaint($ENV{REV}));
201 my $user=`svnlook author $config{svnrepo} -r $rev`;
203 my $message=`svnlook log $config{svnrepo} -r $rev`;
204 if ($message=~/$svn_webcommit/) {
205 $user=defined $2 ? "$2" : "$3";
210 foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
212 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
213 push @changed_pages, $1;
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`;
226 my $subject="$config{wikiname} update of ";
227 if (@changed_pages > 2) {
228 $subject.="$changed_pages[0] $changed_pages[1] etc";
231 $subject.=join(" ", @changed_pages);
233 $subject.=" by $user";
235 my $template=template("notifymail.tmpl");
237 wikiname => $config{wikiname},
243 eval q{use Mail::Sendmail};
244 foreach my $email (@email_recipients) {
247 From => "$config{wikiname} <$config{adminemail}>",
249 Message => $template->output,
250 ) or error("Failed to send update notification mail");
255 sub rcs_getctime ($) { #{{{
258 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
260 my $child = open(SVNLOG, "-|");
262 exec("svn", "log", $file) || error("svn log $file failed to run");
267 if (/$svn_log_infoline/) {
271 close SVNLOG || warn "svn log $file exited $?";
273 if (! defined $date) {
274 warn "failed to parse svn log for $file\n";
278 eval q{use Date::Parse};
279 $date=str2time($date);
280 debug("found ctime ".localtime($date)." for $file");