]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/bzr.pm
bugfixes
[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 ($user, $ipaddr) = @_;
128
129         if (defined $user) {
130                 return IkiWiki::possibly_foolish_untaint($user);
131         }
132         elsif (defined $ipaddr) {
133                 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
134         }
135         else {
136                 return "Anonymous";
137         }
138 }
139
140 sub rcs_commit ($$$;$$) {
141         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
142
143         $user = bzr_author($user, $ipaddr);
144
145         $message = IkiWiki::possibly_foolish_untaint($message);
146         if (! length $message) {
147                 $message = "no message given";
148         }
149
150         my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
151                        $config{srcdir}."/".$file);
152         if (system(@cmdline) != 0) {
153                 warn "'@cmdline' failed: $!";
154         }
155
156         return undef; # success
157 }
158
159 sub rcs_commit_staged ($$$) {
160         # Commits all staged changes. Changes can be staged using rcs_add,
161         # rcs_remove, and rcs_rename.
162         my ($message, $user, $ipaddr)=@_;
163
164         $user = bzr_author($user, $ipaddr);
165
166         $message = IkiWiki::possibly_foolish_untaint($message);
167         if (! length $message) {
168                 $message = "no message given";
169         }
170
171         my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
172                        $config{srcdir});
173         if (system(@cmdline) != 0) {
174                 warn "'@cmdline' failed: $!";
175         }
176
177         return undef; # success
178 }
179
180 sub rcs_add ($) {
181         my ($file) = @_;
182
183         my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
184         if (system(@cmdline) != 0) {
185                 warn "'@cmdline' failed: $!";
186         }
187 }
188
189 sub rcs_remove ($) {
190         my ($file) = @_;
191
192         my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
193         if (system(@cmdline) != 0) {
194                 warn "'@cmdline' failed: $!";
195         }
196 }
197
198 sub rcs_rename ($$) {
199         my ($src, $dest) = @_;
200
201         my $parent = IkiWiki::dirname($dest);
202         if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
203                 warn("bzr add $parent failed\n");
204         }
205
206         my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
207         if (system(@cmdline) != 0) {
208                 warn "'@cmdline' failed: $!";
209         }
210 }
211
212 sub rcs_recentchanges ($) {
213         my ($num) = @_;
214
215         my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num, 
216                            $config{srcdir});
217         open (my $out, "@cmdline |");
218
219         eval q{use Date::Parse};
220         error($@) if $@;
221
222         my @ret;
223         foreach my $info (bzr_log($out)) {
224                 my @pages = ();
225                 my @message = ();
226
227                 foreach my $msgline (split(/\n/, $info->{message})) {
228                         push @message, { line => $msgline };
229                 }
230
231                 foreach my $file (split(/\n/, $info->{files})) {
232                         my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
233
234                         # Skip directories
235                         next if ($filename =~ /\/$/);
236
237                         # Skip source name in renames
238                         $filename =~ s/^.* => //;
239
240                         my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
241                         $diffurl =~ s/\[\[file\]\]/$filename/go;
242                         $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
243                         $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
244
245                         push @pages, {
246                                 page => pagename($filename),
247                                 diffurl => $diffurl,
248                         };
249                 }
250
251                 my $user = $info->{"committer"};
252                 if (defined($info->{"author"})) { $user = $info->{"author"}; }
253                 $user =~ s/\s*<.*>\s*$//;
254                 $user =~ s/^\s*//;
255
256                 push @ret, {
257                         rev        => $info->{"revno"},
258                         user       => $user,
259                         committype => "bzr",
260                         when       => str2time($info->{"timestamp"}),
261                         message    => [@message],
262                         pages      => [@pages],
263                 };
264         }
265
266         return @ret;
267 }
268
269 sub rcs_diff ($) {
270         my $taintedrev=shift;
271         my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
272
273         my $prevspec = "before:" . $rev;
274         my $revspec = "revno:" . $rev;
275         my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
276                 "--new", $config{srcdir},
277                 "-r", $prevspec . ".." . $revspec);
278         open (my $out, "@cmdline |");
279
280         my @lines = <$out>;
281         if (wantarray) {
282                 return @lines;
283         }
284         else {
285                 return join("", @lines);
286         }
287 }
288
289 sub extract_timestamp (@) {
290         # XXX filename passes through the shell here, should try to avoid
291         # that just in case
292         open (my $out, "@_ |");
293         my @log = bzr_log($out);
294
295         if (length @log < 1) {
296                 return 0;
297         }
298
299         eval q{use Date::Parse};
300         error($@) if $@;
301         
302         my $time = str2time($log[0]->{"timestamp"});
303         return $time;
304 }
305
306 sub rcs_getctime ($) {
307         my ($file) = @_;
308
309         my @cmdline = ("bzr", "log", "--forward", "--limit", '1', "$config{srcdir}/$file");
310         return extract_timestamp(@cmdline);
311 }
312
313 sub rcs_getmtime ($) {
314         my ($file) = @_;
315
316         my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
317         return extract_timestamp(@cmdline);
318 }
319
320 1