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