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