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