* Patch from Thomas Schwinge to switch from --svn to --rcs=svn, etc,
[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_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+):?(.*)/;
12
13 sub svn_info ($$) { #{{{
14         my $field=shift;
15         my $file=shift;
16
17         my $info=`LANG=C svn info $file`;
18         my ($ret)=$info=~/^$field: (.*)$/m;
19         return $ret;
20 } #}}}
21
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");
26                 }
27         }
28 } #}}}
29
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
33         # for committing.
34         # The file is relative to the srcdir.
35         my $file=shift;
36         
37         if (-d "$config{srcdir}/.svn") {
38                 # For subversion, return the revision of the file when
39                 # editing begins.
40                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
41                 return defined $rev ? $rev : "";
42         }
43 } #}}}
44
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.
49         my $file=shift;
50         my $message=shift;
51         my $rcstoken=shift;
52
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
60                         # changed.
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");
65                         }
66                 }
67
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");
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         eval q{use CGI 'escapeHTML'};
103         eval q{use Date::Parse};
104         eval q{use Time::Duration};
105         
106         if (-d "$config{srcdir}/.svn") {
107                 my $svn_url=svn_info("URL", $config{srcdir});
108
109                 my $div=qr/^--------------------+$/;
110                 my $state='start';
111                 my ($rev, $user, $when, @pages, @message);
112                 foreach (`LANG=C svn log -v '$svn_url'`) {
113                         chomp;
114                         if ($state eq 'start' && /$div/) {
115                                 $state='header';
116                         }
117                         elsif ($state eq 'header' && /$svn_log_infoline/) {
118                                 $rev=$1;
119                                 $user=$2;
120                                 $when=concise(ago(time - str2time($3)));
121                         }
122                         elsif ($state eq 'header' && /^\s+[A-Z]+\s+\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/) {
123                                 my $file=$1;
124                                 my $diffurl=$config{diffurl};
125                                 $diffurl=~s/\[\[file\]\]/$file/g;
126                                 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
127                                 $diffurl=~s/\[\[r2\]\]/$rev/g;
128                                 push @pages, {
129                                         link => htmllink("", pagename($file), 1),
130                                         diffurl => $diffurl,
131                                 } if length $file;
132                         }
133                         elsif ($state eq 'header' && /^$/) {
134                                 $state='body';
135                         }
136                         elsif ($state eq 'body' && /$div/) {
137                                 my $committype="web";
138                                 if (defined $message[0] &&
139                                     $message[0]->{line}=~/$svn_webcommit/) {
140                                         $user="$1";
141                                         $message[0]->{line}=$2;
142                                 }
143                                 else {
144                                         $committype="svn";
145                                 }
146                                 
147                                 push @ret, { rev => $rev,
148                                         user => htmllink("", $user, 1),
149                                         committype => $committype,
150                                         when => $when, message => [@message],
151                                         pages => [@pages],
152                                 } if @pages;
153                                 return @ret if @ret >= $num;
154                                 
155                                 $state='header';
156                                 $rev=$user=$when=undef;
157                                 @pages=@message=();
158                         }
159                         elsif ($state eq 'body') {
160                                 push @message, {line => escapeHTML($_)},
161                         }
162                 }
163         }
164
165         return @ret;
166 } #}}}
167
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");
171         }
172         my $rev=int(possibly_foolish_untaint($ENV{REV}));
173         
174         my $user=`svnlook author $config{svnrepo} -r $rev`;
175         chomp $user;
176         my $message=`svnlook log $config{svnrepo} -r $rev`;
177         if ($message=~/$svn_webcommit/) {
178                 $user="$1";
179                 $message=$2;
180         }
181
182         my @changed_pages;
183         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
184                 chomp $change;
185                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
186                         push @changed_pages, $1;
187                 }
188         }
189                 
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`;
198
199                 my $subject="$config{wikiname} update of ";
200                 if (@changed_pages > 2) {
201                         $subject.="$changed_pages[0] $changed_pages[1] etc";
202                 }
203                 else {
204                         $subject.=join(" ", @changed_pages);
205                 }
206                 $subject.=" by $user";
207
208                 my $template=HTML::Template->new(
209                         filename => "$config{templatedir}/notifymail.tmpl"
210                 );
211                 $template->param(
212                         wikiname => $config{wikiname},
213                         diff => $diff,
214                         user => $user,
215                         message => $message,
216                 );
217                 
218                 eval q{use Mail::Sendmail};
219                 foreach my $email (@email_recipients) {
220                         sendmail(
221                                 To => $email,
222                                 From => "$config{wikiname} <$config{adminemail}>",
223                                 Subject => $subject,
224                                 Message => $template->output,
225                         ) or error("Failed to send update notification mail");
226                 }
227         }
228 } #}}}
229
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, "-|");
236                 if (! $child) {
237                         exec("svn", "log", $file) || error("svn log $file failed to run");
238                 }
239
240                 my $date;
241                 while (<SVNLOG>) {
242                         if (/$svn_log_infoline/) {
243                                 $date=$3;
244                         }
245                 }
246                 close SVNLOG || warn "svn log $file exited $?";
247
248                 if (! defined $date) {
249                         warn "failed to parse svn log for $file\n";
250                         next;
251                 }
252                 
253                 $pagectime{$page}=$date=str2time($date);
254                 debug("found ctime ".localtime($date)." for $page");
255         }
256 } #}}}
257
258 1