]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Rcs/SVN.pm
dd74a05773fec68d477c94aca0666892ec74615b
[git.ikiwiki.info.git] / IkiWiki / Rcs / SVN.pm
1 #!/usr/bin/perl
2 # For subversion support.
4 use warnings;
5 use strict;
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+).*/;
11 sub svn_info ($$) { #{{{
12         my $field=shift;
13         my $file=shift;
15         my $info=`LANG=C svn info $file`;
16         my ($ret)=$info=~/^$field: (.*)$/m;
17         return $ret;
18 } #}}}
20 sub rcs_update () { #{{{
21         if (-d "$config{srcdir}/.svn") {
22                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
23                         warn("svn update failed\n");
24                 }
25         }
26 } #}}}
28 sub rcs_prepedit ($) { #{{{
29         # Prepares to edit a file under revision control. Returns a token
30         # that must be passed into rcs_commit when the file is ready
31         # for committing.
32         # The file is relative to the srcdir.
33         my $file=shift;
34         
35         if (-d "$config{srcdir}/.svn") {
36                 # For subversion, return the revision of the file when
37                 # editing begins.
38                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
39                 return defined $rev ? $rev : "";
40         }
41 } #}}}
43 sub rcs_commit ($$$) { #{{{
44         # Tries to commit the page; returns undef on _success_ and
45         # a version of the page with the rcs's conflict markers on failure.
46         # The file is relative to the srcdir.
47         my $file=shift;
48         my $message=shift;
49         my $rcstoken=shift;
51         if (-d "$config{srcdir}/.svn") {
52                 # Check to see if the page has been changed by someone
53                 # else since rcs_prepedit was called.
54                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
55                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
56                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
57                         # Merge their changes into the file that we've
58                         # changed.
59                         chdir($config{srcdir}); # svn merge wants to be here
60                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
61                                    "$config{srcdir}/$file") != 0) {
62                                 warn("svn merge -r$oldrev:$rev failed\n");
63                         }
64                 }
66                 if (system("svn", "commit", "--quiet", "-m",
67                            possibly_foolish_untaint($message),
68                            "$config{srcdir}") != 0) {
69                         my $conflict=readfile("$config{srcdir}/$file");
70                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
71                                 warn("svn revert failed\n");
72                         }
73                         return $conflict;
74                 }
75         }
76         return undef # success
77 } #}}}
79 sub rcs_add ($) { #{{{
80         # filename is relative to the root of the srcdir
81         my $file=shift;
83         if (-d "$config{srcdir}/.svn") {
84                 my $parent=dirname($file);
85                 while (! -d "$config{srcdir}/$parent/.svn") {
86                         $file=$parent;
87                         $parent=dirname($file);
88                 }
89                 
90                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
91                         warn("svn add failed\n");
92                 }
93         }
94 } #}}}
96 sub rcs_recentchanges ($) { #{{{
97         my $num=shift;
98         my @ret;
99         
100         eval q{use CGI 'escapeHTML'};
101         eval q{use Date::Parse};
102         eval q{use Time::Duration};
103         
104         if (-d "$config{srcdir}/.svn") {
105                 my $svn_url=svn_info("URL", $config{srcdir});
107                 my $div=qr/^--------------------+$/;
108                 my $state='start';
109                 my ($rev, $user, $when, @pages, @message);
110                 foreach (`LANG=C svn log -v '$svn_url'`) {
111                         chomp;
112                         if ($state eq 'start' && /$div/) {
113                                 $state='header';
114                         }
115                         elsif ($state eq 'header' && /$svn_log_infoline/) {
116                                 $rev=$1;
117                                 $user=$2;
118                                 $when=concise(ago(time - str2time($3)));
119                         }
120                         elsif ($state eq 'header' && /^\s+[A-Z]+\s+\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/) {
121                                 my $file=$1;
122                                 my $diffurl=$config{diffurl};
123                                 $diffurl=~s/\[\[file\]\]/$file/g;
124                                 $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
125                                 $diffurl=~s/\[\[r2\]\]/$rev/g;
126                                 push @pages, {
127                                         link => htmllink("", pagename($file), 1),
128                                         diffurl => $diffurl,
129                                 } if length $file;
130                         }
131                         elsif ($state eq 'header' && /^$/) {
132                                 $state='body';
133                         }
134                         elsif ($state eq 'body' && /$div/) {
135                                 my $committype="web";
136                                 if (defined $message[0] &&
137                                     $message[0]->{line}=~/^web commit by (\w+):?(.*)/) {
138                                         $user="$1";
139                                         $message[0]->{line}=$2;
140                                 }
141                                 else {
142                                         $committype="svn";
143                                 }
144                                 
145                                 push @ret, { rev => $rev,
146                                         user => htmllink("", $user, 1),
147                                         committype => $committype,
148                                         when => $when, message => [@message],
149                                         pages => [@pages],
150                                 } if @pages;
151                                 return @ret if @ret >= $num;
152                                 
153                                 $state='header';
154                                 $rev=$user=$when=undef;
155                                 @pages=@message=();
156                         }
157                         elsif ($state eq 'body') {
158                                 push @message, {line => escapeHTML($_)},
159                         }
160                 }
161         }
163         return @ret;
164 } #}}}
166 sub rcs_notify () { #{{{
167         if (! exists $ENV{REV}) {
168                 error("REV is not set, not running from svn post-commit hook, cannot send notifications");
169         }
171         my @changed_pages;
172         foreach my $change (`svnlook changed $config{svnrepo} -r $ENV{REV}`) {
173                 chomp;
174                 if (/^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
175                         push @changed_pages, $1;
176                 }
177         }
178                 
179         require IkiWiki::UserInfo;
180         my @email_recipients=page_subscribers(@changed_pages);
181         if (@email_recipients) {
182                 eval q{use Mail::Sendmail};
183                 # TODO: if a commit spans multiple pages, this will send
184                 # subscribers a diff that might contain pages they did not
185                 # sign up for. Should separate the diff per page and
186                 # reassemble into one mail with just the pages subscribed to.
187                 my $body=`LANG=C svnlook diff $config{svnrepo} -r $ENV{REV} --no-diff-deleted`;
188                 foreach my $email (@email_recipients) {
189                         sendmail(
190                                 To => $email,
191                                 From => "$config{wikiname} <$config{adminemail}>",
192                                 Subject => "$config{wikiname} $ENV{REV} update notification",
193                                 Message => $body,
194                         ) or error("Failed to send update notification mail");
195                 }
196         }
197 } #}}}
199 sub rcs_getctime () { #{{{
200         eval q{use Date::Parse};
201         foreach my $page (keys %pagectime) {
202                 my $file="$config{srcdir}/$pagesources{$page}";
203                 next unless -e $file;
204                 my $child = open(SVNLOG, "-|");
205                 if (! $child) {
206                         exec("svn", "log", $file) || error("svn log $file failed to run");
207                 }
209                 my $date;
210                 while (<SVNLOG>) {
211                         if (/$svn_log_infoline/) {
212                                 $date=$3;
213                         }
214                 }
215                 close SVNLOG || warn "svn log $file exited $?";
217                 if (! defined $date) {
218                         warn "failed to parse svn log for $file\n";
219                         next;
220                 }
221                 
222                 $pagectime{$page}=$date=str2time($date);
223                 debug("found ctime ".localtime($date)." for $page");
224         }
225 } #}}}