]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
5474e3be2537a311896e10c71613fa19da243031
[ikiwiki.git] / IkiWiki / Rcs / svn.pm
1 #!/usr/bin/perl
2 # For subversion support.
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7
8 package IkiWiki;
9                 
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         return unless -d "$config{srcdir}/.svn";
102
103         eval q{use CGI 'escapeHTML'};
104         eval q{use Date::Parse};
105         eval q{use Time::Duration};
106         eval q{use XML::Simple};
107         
108         my $svn_url=svn_info("URL", $config{srcdir});
109         my $xml = XMLin(scalar `svn --xml -v log '$svn_url'`,
110                 ForceArray => [ 'logentry', 'path' ],
111                 GroupTags => { paths => 'path' },
112                 KeyAttr => { path => 'content' },
113         );
114         foreach my $logentry (@{$xml->{logentry}}) {
115                 my (@pages, @message);
116
117                 my $rev = $logentry->{revision};
118                 my $user = $logentry->{author};
119
120                 my $date = $logentry->{date};
121                 $date =~ s/T/ /;
122                 $date =~ s/\.\d+Z$//;
123                 my $when=concise(ago(time - str2time($date, 'UTC')));
124
125                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
126                         push @message, { line => escapeHTML($msgline) };
127                 }
128
129                 my $committype="web";
130                 if (defined $message[0] &&
131                     $message[0]->{line}=~/$svn_webcommit/) {
132                         $user="$1";
133                         $message[0]->{line}=$2;
134                 }
135                 else {
136                         $committype="svn";
137                 }
138
139                 foreach (keys %{$logentry->{paths}}) {
140                         next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
141                         my $file=$1;
142                         my $diffurl=$config{diffurl};
143                         $diffurl=~s/\[\[file\]\]/$file/g;
144                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
145                         $diffurl=~s/\[\[r2\]\]/$rev/g;
146                         push @pages, {
147                                 link => htmllink("", "", pagename($file), 1),
148                                 diffurl => $diffurl,
149                         } if length $file;
150                 }
151                 push @ret, { rev => $rev,
152                         user => htmllink("", "", $user, 1),
153                         committype => $committype,
154                         when => $when,
155                         message => [@message],
156                         pages => [@pages],
157                 } if @pages;
158                 return @ret if @ret >= $num;
159         }
160
161         return @ret;
162 } #}}}
163
164 sub rcs_notify () { #{{{
165         if (! exists $ENV{REV}) {
166                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
167         }
168         my $rev=int(possibly_foolish_untaint($ENV{REV}));
169         
170         my $user=`svnlook author $config{svnrepo} -r $rev`;
171         chomp $user;
172         my $message=`svnlook log $config{svnrepo} -r $rev`;
173         if ($message=~/$svn_webcommit/) {
174                 $user="$1";
175                 $message=$2;
176         }
177
178         my @changed_pages;
179         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
180                 chomp $change;
181                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
182                         push @changed_pages, $1;
183                 }
184         }
185                 
186         require IkiWiki::UserInfo;
187         my @email_recipients=commit_notify_list($user, @changed_pages);
188         if (@email_recipients) {
189                 # TODO: if a commit spans multiple pages, this will send
190                 # subscribers a diff that might contain pages they did not
191                 # sign up for. Should separate the diff per page and
192                 # reassemble into one mail with just the pages subscribed to.
193                 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
194
195                 my $subject="$config{wikiname} update of ";
196                 if (@changed_pages > 2) {
197                         $subject.="$changed_pages[0] $changed_pages[1] etc";
198                 }
199                 else {
200                         $subject.=join(" ", @changed_pages);
201                 }
202                 $subject.=" by $user";
203
204                 my $template=HTML::Template->new(
205                         filename => "$config{templatedir}/notifymail.tmpl"
206                 );
207                 $template->param(
208                         wikiname => $config{wikiname},
209                         diff => $diff,
210                         user => $user,
211                         message => $message,
212                 );
213                 
214                 eval q{use Mail::Sendmail};
215                 foreach my $email (@email_recipients) {
216                         sendmail(
217                                 To => $email,
218                                 From => "$config{wikiname} <$config{adminemail}>",
219                                 Subject => $subject,
220                                 Message => $template->output,
221                         ) or error("Failed to send update notification mail");
222                 }
223         }
224 } #}}}
225
226 sub rcs_getctime ($) { #{{{
227         my $file=shift;
228         eval q{use Date::Parse};
229
230         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
231                 
232         my $child = open(SVNLOG, "-|");
233         if (! $child) {
234                 exec("svn", "log", $file) || error("svn log $file failed to run");
235         }
236
237         my $date;
238         while (<SVNLOG>) {
239                 if (/$svn_log_infoline/) {
240                         $date=$1;
241                 }
242         }
243         close SVNLOG || warn "svn log $file exited $?";
244
245         if (! defined $date) {
246                 warn "failed to parse svn log for $file\n";
247                 return 0;
248         }
249                 
250         $date=str2time($date);
251         debug("found ctime ".localtime($date)." for $file");
252         return $date;
253 } #}}}
254
255 1