]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/bzr.pm
7eb5cfe93dd9987d63634c05ae51193c97f08915
[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 }
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                         unless (defined($info{$key})) { $info{$key} = ""; }
87                 }
88                 elsif (defined($key) and $line =~ /^  (.*)/) {
89                         $info{$key} .= "$1\n";
90                 }
91                 elsif ($line eq "------------------------------------------------------------\n") {
92                         if (keys %info) {
93                                 push (@infos, {%info});
94                         }
95                         %info = ();
96                         $key = undef;
97                 }
98                 elsif ($line =~ /: /) {
99                         chomp $line;
100                         if ($line =~ /^revno: (\d+)/) {
101                             $key = "revno";
102                             $value = $1;
103                         }
104                         else {
105                                 ($key, $value) = split /: +/, $line, 2;
106                         }
107                         $info{$key} = $value;
108                 }
109         }
110         close $out;
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 rcs_getctime ($) {
290         my ($file) = @_;
291
292         # XXX filename passes through the shell here, should try to avoid
293         # that just in case
294         my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
295         open (my $out, "@cmdline |");
296
297         my @log = bzr_log($out);
298
299         if (length @log < 1) {
300                 return 0;
301         }
302
303         eval q{use Date::Parse};
304         error($@) if $@;
305         
306         my $ctime = str2time($log[0]->{"timestamp"});
307         return $ctime;
308 }
309
310 1