]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/SVN.pm
* Added plugin system, currently only supporting for PreProcessorDirectives.
[ikiwiki.git] / IkiWiki / Rcs / SVN.pm
1 #!/usr/bin/perl
2 # For subversion support.
3
4 use warnings;
5 use strict;
6
7 package IkiWiki;
8                 
9 my $svn_log_infoline=qr/^r(\d+)\s+\|\s+([^\s]+)\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
10 my $svn_webcommit=qr/^web commit by (\w+):?(.*)/;
11
12 sub svn_info ($$) { #{{{
13         my $field=shift;
14         my $file=shift;
15
16         my $info=`LANG=C svn info $file`;
17         my ($ret)=$info=~/^$field: (.*)$/m;
18         return $ret;
19 } #}}}
20
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");
25                 }
26         }
27 } #}}}
28
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
32         # for committing.
33         # The file is relative to the srcdir.
34         my $file=shift;
35         
36         if (-d "$config{srcdir}/.svn") {
37                 # For subversion, return the revision of the file when
38                 # editing begins.
39                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
40                 return defined $rev ? $rev : "";
41         }
42 } #}}}
43
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.
48         my $file=shift;
49         my $message=shift;
50         my $rcstoken=shift;
51
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
59                         # changed.
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");
64                         }
65                 }
66
67                 if (system("svn", "commit", "--quiet", "-m",
68                            possibly_foolish_untaint($message),
69                            "$config{srcdir}") != 0) {
70                         my $conflict=readfile("$config{srcdir}/$file");
71                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
72                                 warn("svn revert failed\n");
73                         }
74                         return $conflict;
75                 }
76         }
77         return undef # success
78 } #}}}
79
80 sub rcs_add ($) { #{{{
81         # filename is relative to the root of the srcdir
82         my $file=shift;
83
84         if (-d "$config{srcdir}/.svn") {
85                 my $parent=dirname($file);
86                 while (! -d "$config{srcdir}/$parent/.svn") {
87                         $file=$parent;
88                         $parent=dirname($file);
89                 }
90                 
91                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
92                         warn("svn add failed\n");
93                 }
94         }
95 } #}}}
96
97 sub rcs_recentchanges ($) { #{{{
98         my $num=shift;
99         my @ret;
100         
101         eval q{use CGI 'escapeHTML'};
102         eval q{use Date::Parse};
103         eval q{use Time::Duration};
104         
105         if (-d "$config{srcdir}/.svn") {
106                 my $svn_url=svn_info("URL", $config{srcdir});
107
108                 my $div=qr/^--------------------+$/;
109                 my $state='start';
110                 my ($rev, $user, $when, @pages, @message);
111                 foreach (`LANG=C svn log -v '$svn_url'`) {
112                         chomp;
113                         if ($state eq 'start' && /$div/) {
114                                 $state='header';
115                         }
116                         elsif ($state eq 'header' && /$svn_log_infoline/) {
117                                 $rev=$1;
118                                 $user=$2;
119                                 $when=concise(ago(time - str2time($3)));
120                         }
121                         elsif ($state eq 'header' && /^\s+[A-Z]+\s+\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/) {
122                                 my $file=$1;
123                                 my $diffurl=$config{diffurl};
124                                 $diffurl=~s/\[\[file\]\]/$file/g;
125                                 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
126                                 $diffurl=~s/\[\[r2\]\]/$rev/g;
127                                 push @pages, {
128                                         link => htmllink("", pagename($file), 1),
129                                         diffurl => $diffurl,
130                                 } if length $file;
131                         }
132                         elsif ($state eq 'header' && /^$/) {
133                                 $state='body';
134                         }
135                         elsif ($state eq 'body' && /$div/) {
136                                 my $committype="web";
137                                 if (defined $message[0] &&
138                                     $message[0]->{line}=~/$svn_webcommit/) {
139                                         $user="$1";
140                                         $message[0]->{line}=$2;
141                                 }
142                                 else {
143                                         $committype="svn";
144                                 }
145                                 
146                                 push @ret, { rev => $rev,
147                                         user => htmllink("", $user, 1),
148                                         committype => $committype,
149                                         when => $when, message => [@message],
150                                         pages => [@pages],
151                                 } if @pages;
152                                 return @ret if @ret >= $num;
153                                 
154                                 $state='header';
155                                 $rev=$user=$when=undef;
156                                 @pages=@message=();
157                         }
158                         elsif ($state eq 'body') {
159                                 push @message, {line => escapeHTML($_)},
160                         }
161                 }
162         }
163
164         return @ret;
165 } #}}}
166
167 sub rcs_notify () { #{{{
168         if (! exists $ENV{REV}) {
169                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
170         }
171         my $rev=int(possibly_foolish_untaint($ENV{REV}));
172         
173         my $user=`svnlook author $config{svnrepo} -r $rev`;
174         chomp $user;
175         my $message=`svnlook log $config{svnrepo} -r $rev`;
176         if ($message=~/$svn_webcommit/) {
177                 $user="$1";
178                 $message=$2;
179         }
180
181         my @changed_pages;
182         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
183                 chomp $change;
184                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
185                         push @changed_pages, $1;
186                 }
187         }
188                 
189         require IkiWiki::UserInfo;
190         my @email_recipients=commit_notify_list($user, @changed_pages);
191         if (@email_recipients) {
192                 # TODO: if a commit spans multiple pages, this will send
193                 # subscribers a diff that might contain pages they did not
194                 # sign up for. Should separate the diff per page and
195                 # reassemble into one mail with just the pages subscribed to.
196                 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
197
198                 my $subject="$config{wikiname} update of ";
199                 if (@changed_pages > 2) {
200                         $subject.="$changed_pages[0] $changed_pages[1] etc";
201                 }
202                 else {
203                         $subject.=join(" ", @changed_pages);
204                 }
205                 $subject.=" by $user";
206
207                 my $template=HTML::Template->new(
208                         filename => "$config{templatedir}/notifymail.tmpl"
209                 );
210                 $template->param(
211                         wikiname => $config{wikiname},
212                         diff => $diff,
213                         user => $user,
214                         message => $message,
215                 );
216                 
217                 eval q{use Mail::Sendmail};
218                 foreach my $email (@email_recipients) {
219                         sendmail(
220                                 To => $email,
221                                 From => "$config{wikiname} <$config{adminemail}>",
222                                 Subject => $subject,
223                                 Message => $template->output,
224                         ) or error("Failed to send update notification mail");
225                 }
226         }
227 } #}}}
228
229 sub rcs_getctime () { #{{{
230         eval q{use Date::Parse};
231         foreach my $page (keys %pagectime) {
232                 my $file="$config{srcdir}/$pagesources{$page}";
233                 next unless -e $file;
234                 my $child = open(SVNLOG, "-|");
235                 if (! $child) {
236                         exec("svn", "log", $file) || error("svn log $file failed to run");
237                 }
238
239                 my $date;
240                 while (<SVNLOG>) {
241                         if (/$svn_log_infoline/) {
242                                 $date=$3;
243                         }
244                 }
245                 close SVNLOG || warn "svn log $file exited $?";
246
247                 if (! defined $date) {
248                         warn "failed to parse svn log for $file\n";
249                         next;
250                 }
251                 
252                 $pagectime{$page}=$date=str2time($date);
253                 debug("found ctime ".localtime($date)." for $page");
254         }
255 } #}}}
256
257 1