]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
560eb38a326941a642a40bf1019cce10aa469823
[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", 
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");
74                         }
75                         return $conflict;
76                 }
77         }
78         return undef # success
79 } #}}}
80
81 sub rcs_add ($) { #{{{
82         # filename is relative to the root of the srcdir
83         my $file=shift;
84
85         if (-d "$config{srcdir}/.svn") {
86                 my $parent=dirname($file);
87                 while (! -d "$config{srcdir}/$parent/.svn") {
88                         $file=$parent;
89                         $parent=dirname($file);
90                 }
91                 
92                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
93                         warn("svn add failed\n");
94                 }
95         }
96 } #}}}
97
98 sub rcs_recentchanges ($) { #{{{
99         my $num=shift;
100         my @ret;
101         
102         return unless -d "$config{srcdir}/.svn";
103
104         eval q{use CGI 'escapeHTML'};
105         eval q{use Date::Parse};
106         eval q{use Time::Duration};
107         eval q{use XML::SAX};
108         eval q{use XML::Simple};
109
110         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
111         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
112         do {
113                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
114         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
115
116         # --limit is only supported on Subversion 1.2.0+
117         my $svn_version=`svn --version -q`;
118         my $svn_limit='';
119         $svn_limit="--limit $num"
120                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
121
122         my $svn_url=svn_info("URL", $config{srcdir});
123         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
124                 ForceArray => [ 'logentry', 'path' ],
125                 GroupTags => { paths => 'path' },
126                 KeyAttr => { path => 'content' },
127         );
128         foreach my $logentry (@{$xml->{logentry}}) {
129                 my (@pages, @message);
130
131                 my $rev = $logentry->{revision};
132                 my $user = $logentry->{author};
133
134                 my $date = $logentry->{date};
135                 $date =~ s/T/ /;
136                 $date =~ s/\.\d+Z$//;
137                 my $when=concise(ago(time - str2time($date, 'UTC')));
138
139                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
140                         push @message, { line => escapeHTML($msgline) };
141                 }
142
143                 my $committype="web";
144                 if (defined $message[0] &&
145                     $message[0]->{line}=~/$svn_webcommit/) {
146                         $user="$1";
147                         $message[0]->{line}=$2;
148                 }
149                 else {
150                         $committype="svn";
151                 }
152
153                 foreach (keys %{$logentry->{paths}}) {
154                         next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
155                         my $file=$1;
156                         my $diffurl=$config{diffurl};
157                         $diffurl=~s/\[\[file\]\]/$file/g;
158                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
159                         $diffurl=~s/\[\[r2\]\]/$rev/g;
160                         push @pages, {
161                                 link => htmllink("", "", pagename($file), 1),
162                                 diffurl => $diffurl,
163                         } if length $file;
164                 }
165                 push @ret, { rev => $rev,
166                         user => htmllink("", "", $user, 1),
167                         committype => $committype,
168                         when => $when,
169                         message => [@message],
170                         pages => [@pages],
171                 } if @pages;
172                 return @ret if @ret >= $num;
173         }
174
175         return @ret;
176 } #}}}
177
178 sub rcs_notify () { #{{{
179         if (! exists $ENV{REV}) {
180                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
181         }
182         my $rev=int(possibly_foolish_untaint($ENV{REV}));
183         
184         my $user=`svnlook author $config{svnrepo} -r $rev`;
185         chomp $user;
186         my $message=`svnlook log $config{svnrepo} -r $rev`;
187         if ($message=~/$svn_webcommit/) {
188                 $user="$1";
189                 $message=$2;
190         }
191
192         my @changed_pages;
193         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
194                 chomp $change;
195                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
196                         push @changed_pages, $1;
197                 }
198         }
199                 
200         require IkiWiki::UserInfo;
201         my @email_recipients=commit_notify_list($user, @changed_pages);
202         if (@email_recipients) {
203                 # TODO: if a commit spans multiple pages, this will send
204                 # subscribers a diff that might contain pages they did not
205                 # sign up for. Should separate the diff per page and
206                 # reassemble into one mail with just the pages subscribed to.
207                 my $diff=`svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
208
209                 my $subject="$config{wikiname} update of ";
210                 if (@changed_pages > 2) {
211                         $subject.="$changed_pages[0] $changed_pages[1] etc";
212                 }
213                 else {
214                         $subject.=join(" ", @changed_pages);
215                 }
216                 $subject.=" by $user";
217
218                 my $template=template("notifymail.tmpl");
219                 $template->param(
220                         wikiname => $config{wikiname},
221                         diff => $diff,
222                         user => $user,
223                         message => $message,
224                 );
225                 
226                 eval q{use Mail::Sendmail};
227                 foreach my $email (@email_recipients) {
228                         sendmail(
229                                 To => $email,
230                                 From => "$config{wikiname} <$config{adminemail}>",
231                                 Subject => $subject,
232                                 Message => $template->output,
233                         ) or error("Failed to send update notification mail");
234                 }
235         }
236 } #}}}
237
238 sub rcs_getctime ($) { #{{{
239         my $file=shift;
240         eval q{use Date::Parse};
241
242         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
243                 
244         my $child = open(SVNLOG, "-|");
245         if (! $child) {
246                 exec("svn", "log", $file) || error("svn log $file failed to run");
247         }
248
249         my $date;
250         while (<SVNLOG>) {
251                 if (/$svn_log_infoline/) {
252                         $date=$1;
253                 }
254         }
255         close SVNLOG || warn "svn log $file exited $?";
256
257         if (! defined $date) {
258                 warn "failed to parse svn log for $file\n";
259                 return 0;
260         }
261                 
262         $date=str2time($date);
263         debug("found ctime ".localtime($date)." for $file");
264         return $date;
265 } #}}}
266
267 1