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