]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/svn.pm
italisise urls
[ikiwiki.git] / IkiWiki / Rcs / svn.pm
1 #!/usr/bin/perl
2
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use POSIX qw(setlocale LC_CTYPE);
7
8 package IkiWiki;
9                 
10 # svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
11 sub find_lc_ctype() {
12         my $current = setlocale(LC_CTYPE());
13         return $current if $current =~ m/UTF-?8$/i;
14
15         # Make some obvious attempts to avoid calling `locale -a`
16         foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
17                 return $locale if setlocale(LC_CTYPE(), $locale);
18         }
19
20         # Try to get all available locales and pick the first UTF-8 one found.
21         if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
22                 chomp @locale;
23                 return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
24         }
25
26         # fallback to the current locale
27         return $current;
28 } # }}}
29 $ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
30
31 sub svn_info ($$) { #{{{
32         my $field=shift;
33         my $file=shift;
34
35         my $info=`LANG=C svn info $file`;
36         my ($ret)=$info=~/^$field: (.*)$/m;
37         return $ret;
38 } #}}}
39
40 sub rcs_update () { #{{{
41         if (-d "$config{srcdir}/.svn") {
42                 if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
43                         warn("svn update failed\n");
44                 }
45         }
46 } #}}}
47
48 sub rcs_prepedit ($) { #{{{
49         # Prepares to edit a file under revision control. Returns a token
50         # that must be passed into rcs_commit when the file is ready
51         # for committing.
52         # The file is relative to the srcdir.
53         my $file=shift;
54         
55         if (-d "$config{srcdir}/.svn") {
56                 # For subversion, return the revision of the file when
57                 # editing begins.
58                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
59                 return defined $rev ? $rev : "";
60         }
61 } #}}}
62
63 sub rcs_commit ($$$;$$) { #{{{
64         # Tries to commit the page; returns undef on _success_ and
65         # a version of the page with the rcs's conflict markers on failure.
66         # The file is relative to the srcdir.
67         my $file=shift;
68         my $message=shift;
69         my $rcstoken=shift;
70         my $user=shift;
71         my $ipaddr=shift;
72
73         if (defined $user) {
74                 $message="web commit by $user".(length $message ? ": $message" : "");
75         }
76         elsif (defined $ipaddr) {
77                 $message="web commit from $ipaddr".(length $message ? ": $message" : "");
78         }
79
80         if (-d "$config{srcdir}/.svn") {
81                 # Check to see if the page has been changed by someone
82                 # else since rcs_prepedit was called.
83                 my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
84                 my $rev=svn_info("Revision", "$config{srcdir}/$file");
85                 if (defined $rev && defined $oldrev && $rev != $oldrev) {
86                         # Merge their changes into the file that we've
87                         # changed.
88                         if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
89                                    "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
90                                 warn("svn merge -r$oldrev:$rev failed\n");
91                         }
92                 }
93
94                 if (system("svn", "commit", "--quiet", 
95                            "--encoding", "UTF-8", "-m",
96                            possibly_foolish_untaint($message),
97                            $config{srcdir}) != 0) {
98                         my $conflict=readfile("$config{srcdir}/$file");
99                         if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
100                                 warn("svn revert failed\n");
101                         }
102                         return $conflict;
103                 }
104         }
105         return undef # success
106 } #}}}
107
108 sub rcs_add ($) { #{{{
109         # filename is relative to the root of the srcdir
110         my $file=shift;
111
112         if (-d "$config{srcdir}/.svn") {
113                 my $parent=dirname($file);
114                 while (! -d "$config{srcdir}/$parent/.svn") {
115                         $file=$parent;
116                         $parent=dirname($file);
117                 }
118                 
119                 if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
120                         warn("svn add failed\n");
121                 }
122         }
123 } #}}}
124
125 sub rcs_recentchanges ($) { #{{{
126         my $num=shift;
127         my @ret;
128         
129         return unless -d "$config{srcdir}/.svn";
130
131         eval q{
132                 use Date::Parse;
133                 use XML::SAX;
134                 use XML::Simple;
135         };
136         error($@) if $@;
137
138         # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
139         my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
140         do {
141                 $XML::Simple::PREFERRED_PARSER = pop @parsers;
142         } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
143
144         # --limit is only supported on Subversion 1.2.0+
145         my $svn_version=`svn --version -q`;
146         my $svn_limit='';
147         $svn_limit="--limit $num"
148                 if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
149
150         my $svn_url=svn_info("URL", $config{srcdir});
151         my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
152                 ForceArray => [ 'logentry', 'path' ],
153                 GroupTags => { paths => 'path' },
154                 KeyAttr => { path => 'content' },
155         );
156         foreach my $logentry (@{$xml->{logentry}}) {
157                 my (@pages, @message);
158
159                 my $rev = $logentry->{revision};
160                 my $user = $logentry->{author};
161
162                 my $when=time - str2time($logentry->{date}, 'UTC');
163
164                 foreach my $msgline (split(/\n/, $logentry->{msg})) {
165                         push @message, { line => $msgline };
166                 }
167
168                 my $committype="web";
169                 if (defined $message[0] &&
170                     $message[0]->{line}=~/$config{web_commit_regexp}/) {
171                         $user=defined $2 ? "$2" : "$3";
172                         $message[0]->{line}=$4;
173                 }
174                 else {
175                         $committype="svn";
176                 }
177
178                 foreach (keys %{$logentry->{paths}}) {
179                         next unless /^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
180                         my $file=$1;
181                         my $diffurl=$config{diffurl};
182                         $diffurl=~s/\[\[file\]\]/$file/g;
183                         $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
184                         $diffurl=~s/\[\[r2\]\]/$rev/g;
185                         push @pages, {
186                                 page => pagename($file),
187                                 diffurl => $diffurl,
188                         } if length $file;
189                 }
190                 push @ret, { rev => $rev,
191                         user => $user,
192                         committype => $committype,
193                         when => $when,
194                         message => [@message],
195                         pages => [@pages],
196                 } if @pages;
197                 return @ret if @ret >= $num;
198         }
199
200         return @ret;
201 } #}}}
202
203 sub rcs_notify () { #{{{
204         if (! exists $ENV{REV}) {
205                 error(gettext("REV is not set, not running from svn post-commit hook, cannot send notifications"));
206         }
207         my $rev=int(possibly_foolish_untaint($ENV{REV}));
208         
209         my $user=`svnlook author $config{svnrepo} -r $rev`;
210         chomp $user;
211         
212         my $message=`svnlook log $config{svnrepo} -r $rev`;
213         if ($message=~/$config{web_commit_regexp}/) {
214                 $user=defined $2 ? "$2" : "$3";
215                 $message=$4;
216         }
217
218         my @changed_pages;
219         foreach my $change (`svnlook changed $config{svnrepo} -r $rev`) {
220                 chomp $change;
221                 if ($change =~ /^[A-Z]+\s+\Q$config{svnpath}\E\/(.*)/) {
222                         push @changed_pages, $1;
223                 }
224         }
225         
226         require IkiWiki::UserInfo;
227         send_commit_mails(
228                 sub {
229                         return $message;
230                 },
231                 sub {
232                         `svnlook diff $config{svnrepo} -r $rev --no-diff-deleted`;
233                 }, $user, @changed_pages);
234 } #}}}
235
236 sub rcs_getctime ($) { #{{{
237         my $file=shift;
238
239         my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
240                 
241         my $child = open(SVNLOG, "-|");
242         if (! $child) {
243                 exec("svn", "log", $file) || error("svn log $file failed to run");
244         }
245
246         my $date;
247         while (<SVNLOG>) {
248                 if (/$svn_log_infoline/) {
249                         $date=$1;
250                 }
251         }
252         close SVNLOG || warn "svn log $file exited $?";
253
254         if (! defined $date) {
255                 warn "failed to parse svn log for $file\n";
256                 return 0;
257         }
258                 
259         eval q{use Date::Parse};
260         error($@) if $@;
261         $date=str2time($date);
262         debug("found ctime ".localtime($date)." for $file");
263         return $date;
264 } #}}}
265
266 1