2 # For subversion support.
10 my $svn_webcommit=qr/^web commit by (\w+):?(.*)/;
12 sub svn_info ($$) { #{{{
16 my $info=`LANG=C svn info $file`;
17 my ($ret)=$info=~/^$field: (.*)$/m;
21 sub rcs_update () { #{{{
22 if (-d "$config{srcdir}/.svn") {
23 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
24 warn("svn update failed\n");
29 sub rcs_prepedit ($) { #{{{
30 # Prepares to edit a file under revision control. Returns a token
31 # that must be passed into rcs_commit when the file is ready
33 # The file is relative to the srcdir.
36 if (-d "$config{srcdir}/.svn") {
37 # For subversion, return the revision of the file when
39 my $rev=svn_info("Revision", "$config{srcdir}/$file");
40 return defined $rev ? $rev : "";
44 sub rcs_commit ($$$) { #{{{
45 # Tries to commit the page; returns undef on _success_ and
46 # a version of the page with the rcs's conflict markers on failure.
47 # The file is relative to the srcdir.
52 if (-d "$config{srcdir}/.svn") {
53 # Check to see if the page has been changed by someone
54 # else since rcs_prepedit was called.
55 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
56 my $rev=svn_info("Revision", "$config{srcdir}/$file");
57 if (defined $rev && defined $oldrev && $rev != $oldrev) {
58 # Merge their changes into the file that we've
60 chdir($config{srcdir}); # svn merge wants to be here
61 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
62 "$config{srcdir}/$file") != 0) {
63 warn("svn merge -r$oldrev:$rev failed\n");
67 if (system("svn", "commit", "--quiet",
68 "--encoding", "UTF-8", "-m",
69 possibly_foolish_untaint($message),
70 "$config{srcdir}") != 0) {
71 my $conflict=readfile("$config{srcdir}/$file");
72 if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
73 warn("svn revert failed\n");
78 return undef # success
81 sub rcs_add ($) { #{{{
82 # filename is relative to the root of the srcdir
85 if (-d "$config{srcdir}/.svn") {
86 my $parent=dirname($file);
87 while (! -d "$config{srcdir}/$parent/.svn") {
89 $parent=dirname($file);
92 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
93 warn("svn add failed\n");
98 sub rcs_recentchanges ($) { #{{{
102 return unless -d "$config{srcdir}/.svn";
104 eval q{use CGI 'escapeHTML'};
105 eval q{use Date::Parse};
106 eval q{use Time::Duration};
107 eval q{use XML::Simple};
109 # --limit is only supported on Subversion 1.2.0+
110 my $svn_version=`svn --version -q`;
112 $svn_limit="--limit $num"
113 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
115 my $svn_url=svn_info("URL", $config{srcdir});
116 my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
117 ForceArray => [ 'logentry', 'path' ],
118 GroupTags => { paths => 'path' },
119 KeyAttr => { path => 'content' },
121 foreach my $logentry (@{$xml->{logentry}}) {
122 my (@pages, @message);
124 my $rev = $logentry->{revision};
125 my $user = $logentry->{author};
127 my $date = $logentry->{date};
129 $date =~ s/\.\d+Z$//;
130 my $when=concise(ago(time - str2time($date, 'UTC')));
132 foreach my $msgline (split(/\n/, $logentry->{msg})) {
133 push @message, { line => escapeHTML($msgline) };
136 my $committype="web";
137 if (defined $message[0] &&
138 $message[0]->{line}=~/$svn_webcommit/) {
140 $message[0]->{line}=$2;
146 foreach (keys %{$logentry->{paths}}) {
147 next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
149 my $diffurl=$config{diffurl};
150 $diffurl=~s/\[\[file\]\]/$file/g;
151 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
152 $diffurl=~s/\[\[r2\]\]/$rev/g;
154 link => htmllink("", "", pagename($file), 1),
158 push @ret, { rev => $rev,
159 user => htmllink("", "", $user, 1),
160 committype => $committype,
162 message => [@message],
165 return @ret if @ret >= $num;
171 sub rcs_notify () { #{{{
172 if (! exists $ENV{REV}) {
173 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
175 my $rev=int(possibly_foolish_untaint($ENV{REV}));
177 my $user=`svnlook author $config{svnrepo} -r $rev`;
179 my $message=`svnlook log $config{svnrepo} -r $rev`;
180 if ($message=~/$svn_webcommit/) {
186 foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
188 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
189 push @changed_pages, $1;
193 require IkiWiki::UserInfo;
194 my @email_recipients=commit_notify_list($user, @changed_pages);
195 if (@email_recipients) {
196 # TODO: if a commit spans multiple pages, this will send
197 # subscribers a diff that might contain pages they did not
198 # sign up for. Should separate the diff per page and
199 # reassemble into one mail with just the pages subscribed to.
200 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
202 my $subject="$config{wikiname} update of ";
203 if (@changed_pages > 2) {
204 $subject.="$changed_pages[0] $changed_pages[1] etc";
207 $subject.=join(" ", @changed_pages);
209 $subject.=" by $user";
211 my $template=template("notifymail.tmpl");
213 wikiname => $config{wikiname},
219 eval q{use Mail::Sendmail};
220 foreach my $email (@email_recipients) {
223 From => "$config{wikiname} <$config{adminemail}>",
225 Message => $template->output,
226 ) or error("Failed to send update notification mail");
231 sub rcs_getctime ($) { #{{{
233 eval q{use Date::Parse};
235 my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
237 my $child = open(SVNLOG, "-|");
239 exec("svn", "log", $file) || error("svn log $file failed to run");
244 if (/$svn_log_infoline/) {
248 close SVNLOG || warn "svn log $file exited $?";
250 if (! defined $date) {
251 warn "failed to parse svn log for $file\n";
255 $date=str2time($date);
256 debug("found ctime ".localtime($date)." for $file");