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