]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/bzr.pm
Revert "comment: Don't show comments of subpages on parent pages. (Fixes bug introduc...
[ikiwiki.git] / IkiWiki / Plugin / bzr.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::bzr;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use Encode;
8 use open qw{:utf8 :std};
9
10 sub import {
11         hook(type => "checkconfig", id => "bzr", call => \&checkconfig);
12         hook(type => "getsetup", id => "bzr", call => \&getsetup);
13         hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
14         hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
15         hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
16         hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
17         hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
18         hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
19         hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
20         hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
21         hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
22         hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
23         hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
24 }
25
26 sub checkconfig () {
27         if (defined $config{bzr_wrapper} && length $config{bzr_wrapper}) {
28                 push @{$config{wrappers}}, {
29                         wrapper => $config{bzr_wrapper},
30                         wrappermode => (defined $config{bzr_wrappermode} ? $config{bzr_wrappermode} : "06755"),
31                 };
32         }
33 }
34
35 sub getsetup () {
36         return
37                 plugin => {
38                         safe => 0, # rcs plugin
39                         rebuild => undef,
40                         section => "rcs",
41                 },
42                 bzr_wrapper => {
43                         type => "string",
44                         #example => "", # FIXME add example
45                         description => "bzr post-commit hook to generate",
46                         safe => 0, # file
47                         rebuild => 0,
48                 },
49                 bzr_wrappermode => {
50                         type => "string",
51                         example => '06755',
52                         description => "mode for bzr_wrapper (can safely be made suid)",
53                         safe => 0,
54                         rebuild => 0,
55                 },
56                 historyurl => {
57                         type => "string",
58                         #example => "", # FIXME add example
59                         description => "url to show file history, using loggerhead ([[file]] substituted)",
60                         safe => 1,
61                         rebuild => 1,
62                 },
63                 diffurl => {
64                         type => "string",
65                         example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
66                         description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
67                         safe => 1,
68                         rebuild => 1,
69                 },
70 }
71
72 sub bzr_log ($) {
73         my $out = shift;
74         my @infos = ();
75         my $key = undef;
76
77         my %info;
78         while (<$out>) {
79                 my $line = $_;
80                 my ($value);
81                 if ($line =~ /^message:/) {
82                         $key = "message";
83                         $info{$key} = "";
84                 }
85                 elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
86                         $key = "files";
87                         $info{$key} = "" unless defined $info{$key};
88                 }
89                 elsif (defined($key) and $line =~ /^  (.*)/) {
90                         $info{$key} .= "$1\n";
91                 }
92                 elsif ($line eq "------------------------------------------------------------\n") {
93                         push @infos, {%info} if keys %info;
94                         %info = ();
95                         $key = undef;
96                 }
97                 elsif ($line =~ /: /) {
98                         chomp $line;
99                         if ($line =~ /^revno: (\d+)/) {
100                             $key = "revno";
101                             $value = $1;
102                         }
103                         else {
104                                 ($key, $value) = split /: +/, $line, 2;
105                         }
106                         $info{$key} = $value;
107                 }
108         }
109         close $out;
110         push @infos, {%info} if keys %info;
111
112         return @infos;
113 }
114
115 sub rcs_update () {
116         my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
117         if (system(@cmdline) != 0) {
118                 warn "'@cmdline' failed: $!";
119         }
120 }
121
122 sub rcs_prepedit ($) {
123         return "";
124 }
125
126 sub bzr_author ($) {
127         my $session=shift;
128
129         return unless defined $session;
130
131         my $user=$session->param("name");
132         my $ipaddr=$session->remote_addr();
133
134         if (defined $user) {
135                 return IkiWiki::possibly_foolish_untaint($user);
136         }
137         elsif (defined $ipaddr) {
138                 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
139         }
140         else {
141                 return "Anonymous";
142         }
143 }
144
145 sub rcs_commit (@) {
146         my %params=@_;
147
148         my $user=bzr_author($params{session});
149
150         $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
151         if (! length $params{message}) {
152                 $params{message} = "no message given";
153         }
154
155         my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
156                        (defined $user ? ("--author", $user) : ()),
157                        $config{srcdir}."/".$params{file});
158         if (system(@cmdline) != 0) {
159                 warn "'@cmdline' failed: $!";
160         }
161
162         return undef; # success
163 }
164
165 sub rcs_commit_staged (@) {
166         my %params=@_;
167
168         my $user=bzr_author($params{session});
169
170         $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
171         if (! length $params{message}) {
172                 $params{message} = "no message given";
173         }
174
175         my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
176                        (defined $user ? ("--author", $user) : ()),
177                        $config{srcdir});
178         if (system(@cmdline) != 0) {
179                 warn "'@cmdline' failed: $!";
180         }
181
182         return undef; # success
183 }
184
185 sub rcs_add ($) {
186         my ($file) = @_;
187
188         my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
189         if (system(@cmdline) != 0) {
190                 warn "'@cmdline' failed: $!";
191         }
192 }
193
194 sub rcs_remove ($) {
195         my ($file) = @_;
196
197         my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
198         if (system(@cmdline) != 0) {
199                 warn "'@cmdline' failed: $!";
200         }
201 }
202
203 sub rcs_rename ($$) {
204         my ($src, $dest) = @_;
205
206         my $parent = IkiWiki::dirname($dest);
207         if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
208                 warn("bzr add $parent failed\n");
209         }
210
211         my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
212         if (system(@cmdline) != 0) {
213                 warn "'@cmdline' failed: $!";
214         }
215 }
216
217 sub rcs_recentchanges ($) {
218         my ($num) = @_;
219
220         my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num, 
221                            $config{srcdir});
222         open (my $out, "@cmdline |");
223
224         eval q{use Date::Parse};
225         error($@) if $@;
226
227         my @ret;
228         foreach my $info (bzr_log($out)) {
229                 my @pages = ();
230                 my @message = ();
231
232                 foreach my $msgline (split(/\n/, $info->{message})) {
233                         push @message, { line => $msgline };
234                 }
235
236                 foreach my $file (split(/\n/, $info->{files})) {
237                         my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
238
239                         # Skip directories
240                         next if ($filename =~ /\/$/);
241
242                         # Skip source name in renames
243                         $filename =~ s/^.* => //;
244
245                         my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
246                         $diffurl =~ s/\[\[file\]\]/$filename/go;
247                         $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
248                         $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
249
250                         push @pages, {
251                                 page => pagename($filename),
252                                 diffurl => $diffurl,
253                         };
254                 }
255
256                 my $user = $info->{"committer"};
257                 if (defined($info->{"author"})) { $user = $info->{"author"}; }
258                 $user =~ s/\s*<.*>\s*$//;
259                 $user =~ s/^\s*//;
260
261                 push @ret, {
262                         rev        => $info->{"revno"},
263                         user       => $user,
264                         committype => "bzr",
265                         when       => str2time($info->{"timestamp"}),
266                         message    => [@message],
267                         pages      => [@pages],
268                 };
269         }
270
271         return @ret;
272 }
273
274 sub rcs_diff ($;$) {
275         my $taintedrev=shift;
276         my $maxlines=shift;
277         my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
278
279         my $prevspec = "before:" . $rev;
280         my $revspec = "revno:" . $rev;
281         my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
282                 "--new", $config{srcdir},
283                 "-r", $prevspec . ".." . $revspec);
284         open (my $out, "@cmdline |");
285         my @lines;
286         while (my $line=<$out>) {
287                 last if defined $maxlines && @lines == $maxlines;
288                 push @lines, $line;
289         }
290         if (wantarray) {
291                 return @lines;
292         }
293         else {
294                 return join("", @lines);
295         }
296 }
297
298 sub extract_timestamp (@) {
299         open (my $out, "-|", @_);
300         my @log = bzr_log($out);
301
302         if (length @log < 1) {
303                 return 0;
304         }
305
306         eval q{use Date::Parse};
307         error($@) if $@;
308         
309         my $time = str2time($log[0]->{"timestamp"});
310         return $time;
311 }
312
313 sub rcs_getctime ($) {
314         my ($file) = @_;
315
316         my @cmdline = ("bzr", "log", "--forward", "--limit", '1', "$config{srcdir}/$file");
317         return extract_timestamp(@cmdline);
318 }
319
320 sub rcs_getmtime ($) {
321         my ($file) = @_;
322
323         my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
324         return extract_timestamp(@cmdline);
325 }
326
327 1