2 # For subversion support.
10 my $svn_log_infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
11 my $svn_webcommit=qr/^web commit by (\w+):?(.*)/;
13 sub svn_info ($$) { #{{{
17 my $info=`LANG=C svn info $file`;
18 my ($ret)=$info=~/^$field: (.*)$/m;
22 sub rcs_update () { #{{{
23 if (-d "$config{srcdir}/.svn") {
24 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
25 warn("svn update failed\n");
30 sub rcs_prepedit ($) { #{{{
31 # Prepares to edit a file under revision control. Returns a token
32 # that must be passed into rcs_commit when the file is ready
34 # The file is relative to the srcdir.
37 if (-d "$config{srcdir}/.svn") {
38 # For subversion, return the revision of the file when
40 my $rev=svn_info("Revision", "$config{srcdir}/$file");
41 return defined $rev ? $rev : "";
45 sub rcs_commit ($$$) { #{{{
46 # Tries to commit the page; returns undef on _success_ and
47 # a version of the page with the rcs's conflict markers on failure.
48 # The file is relative to the srcdir.
53 if (-d "$config{srcdir}/.svn") {
54 # Check to see if the page has been changed by someone
55 # else since rcs_prepedit was called.
56 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
57 my $rev=svn_info("Revision", "$config{srcdir}/$file");
58 if (defined $rev && defined $oldrev && $rev != $oldrev) {
59 # Merge their changes into the file that we've
61 chdir($config{srcdir}); # svn merge wants to be here
62 if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
63 "$config{srcdir}/$file") != 0) {
64 warn("svn merge -r$oldrev:$rev failed\n");
68 if (system("svn", "commit", "--quiet", "-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 eval q{use CGI 'escapeHTML'};
103 eval q{use Date::Parse};
104 eval q{use Time::Duration};
106 if (-d "$config{srcdir}/.svn") {
107 my $svn_url=svn_info("URL", $config{srcdir});
109 my $div=qr/^--------------------+$/;
111 my ($rev, $user, $when, @pages, @message);
112 foreach (`LANG=C svn log -v '$svn_url'`) {
114 if ($state eq 'start' && /$div/) {
117 elsif ($state eq 'header' && /$svn_log_infoline/) {
120 $when=concise(ago(time - str2time($3)));
122 elsif ($state eq 'header' && /^\s+[A-Z]+\s+\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/) {
124 my $diffurl=$config{diffurl};
125 $diffurl=~s/\[\[file\]\]/$file/g;
126 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
127 $diffurl=~s/\[\[r2\]\]/$rev/g;
129 link => htmllink("", pagename($file), 1),
133 elsif ($state eq 'header' && /^$/) {
136 elsif ($state eq 'body' && /$div/) {
137 my $committype="web";
138 if (defined $message[0] &&
139 $message[0]->{line}=~/$svn_webcommit/) {
141 $message[0]->{line}=$2;
147 push @ret, { rev => $rev,
148 user => htmllink("", $user, 1),
149 committype => $committype,
150 when => $when, message => [@message],
153 return @ret if @ret >= $num;
156 $rev=$user=$when=undef;
159 elsif ($state eq 'body') {
160 push @message, {line => escapeHTML($_)},
168 sub rcs_notify () { #{{{
169 if (! exists $ENV{REV}) {
170 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
172 my $rev=int(possibly_foolish_untaint($ENV{REV}));
174 my $user=`svnlook author $config{svnrepo} -r $rev`;
176 my $message=`svnlook log $config{svnrepo} -r $rev`;
177 if ($message=~/$svn_webcommit/) {
183 foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
185 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
186 push @changed_pages, $1;
190 require IkiWiki::UserInfo;
191 my @email_recipients=commit_notify_list($user, @changed_pages);
192 if (@email_recipients) {
193 # TODO: if a commit spans multiple pages, this will send
194 # subscribers a diff that might contain pages they did not
195 # sign up for. Should separate the diff per page and
196 # reassemble into one mail with just the pages subscribed to.
197 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
199 my $subject="$config{wikiname} update of ";
200 if (@changed_pages > 2) {
201 $subject.="$changed_pages[0] $changed_pages[1] etc";
204 $subject.=join(" ", @changed_pages);
206 $subject.=" by $user";
208 my $template=HTML::Template->new(
209 filename => "$config{templatedir}/notifymail.tmpl"
212 wikiname => $config{wikiname},
218 eval q{use Mail::Sendmail};
219 foreach my $email (@email_recipients) {
222 From => "$config{wikiname} <$config{adminemail}>",
224 Message => $template->output,
225 ) or error("Failed to send update notification mail");
230 sub rcs_getctime () { #{{{
231 eval q{use Date::Parse};
232 foreach my $page (keys %pagectime) {
233 my $file="$config{srcdir}/$pagesources{$page}";
234 next unless -e $file;
235 my $child = open(SVNLOG, "-|");
237 exec("svn", "log", $file) || error("svn log $file failed to run");
242 if (/$svn_log_infoline/) {
246 close SVNLOG || warn "svn log $file exited $?";
248 if (! defined $date) {
249 warn "failed to parse svn log for $file\n";
253 $pagectime{$page}=$date=str2time($date);
254 debug("found ctime ".localtime($date)." for $page");