Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / bzr.pm
blob3bc4ea8dd69b1d67329ec1db81bdd2e4ac9d19cf
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::bzr;
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use Encode;
8 use open qw{:utf8 :std};
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 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
26 sub checkconfig () {
27 if (defined $config{bzr_wrapper} && length $config{bzr_wrapper}) {
28 push @{$config{wrappers}}, {
29 wrapper => $config{bzr_wrapper},
30 wrappermode => (defined $config{bzr_wrappermode} ? $config{bzr_wrappermode} : "06755"),
35 sub getsetup () {
36 return
37 plugin => {
38 safe => 0, # rcs plugin
39 rebuild => undef,
40 section => "rcs",
42 bzr_wrapper => {
43 type => "string",
44 #example => "", # FIXME add example
45 description => "bzr post-commit hook to generate",
46 safe => 0, # file
47 rebuild => 0,
49 bzr_wrappermode => {
50 type => "string",
51 example => '06755',
52 description => "mode for bzr_wrapper (can safely be made suid)",
53 safe => 0,
54 rebuild => 0,
56 historyurl => {
57 type => "string",
58 #example => "", # FIXME add example
59 description => "url to show file history, using loggerhead ([[file]] substituted)",
60 safe => 1,
61 rebuild => 1,
63 diffurl => {
64 type => "string",
65 example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
66 description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
67 safe => 1,
68 rebuild => 1,
72 sub bzr_log ($) {
73 my $out = shift;
74 my @infos = ();
75 my $key = undef;
77 my %info;
78 while (<$out>) {
79 my $line = $_;
80 my ($value);
81 if ($line =~ /^message:/) {
82 $key = "message";
83 $info{$key} = "";
85 elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
86 $key = "files";
87 $info{$key} = "" unless defined $info{$key};
89 elsif (defined($key) and $line =~ /^ (.*)/) {
90 $info{$key} .= "$1\n";
92 elsif ($line eq "------------------------------------------------------------\n") {
93 push @infos, {%info} if keys %info;
94 %info = ();
95 $key = undef;
97 elsif ($line =~ /: /) {
98 chomp $line;
99 if ($line =~ /^revno: (\d+)/) {
100 $key = "revno";
101 $value = $1;
103 else {
104 ($key, $value) = split /: +/, $line, 2;
106 $info{$key} = $value;
109 close $out;
110 push @infos, {%info} if keys %info;
112 return @infos;
115 sub rcs_update () {
116 my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
117 if (system(@cmdline) != 0) {
118 warn "'@cmdline' failed: $!";
122 sub rcs_prepedit ($) {
123 return "";
126 sub bzr_author ($) {
127 my $session=shift;
129 return unless defined $session;
131 my $user=$session->param("name");
132 my $ipaddr=$session->remote_addr();
134 if (defined $user) {
135 return IkiWiki::possibly_foolish_untaint($user);
137 elsif (defined $ipaddr) {
138 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
140 else {
141 return "Anonymous";
145 sub rcs_commit (@) {
146 my %params=@_;
148 my $user=bzr_author($params{session});
150 $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
151 if (! length $params{message}) {
152 $params{message} = "no message given";
155 my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
156 (defined $user ? ("--author", $user) : ()),
157 $config{srcdir}."/".$params{file});
158 if (system(@cmdline) != 0) {
159 warn "'@cmdline' failed: $!";
162 return undef; # success
165 sub rcs_commit_staged (@) {
166 my %params=@_;
168 my $user=bzr_author($params{session});
170 $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
171 if (! length $params{message}) {
172 $params{message} = "no message given";
175 my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
176 (defined $user ? ("--author", $user) : ()),
177 $config{srcdir});
178 if (system(@cmdline) != 0) {
179 warn "'@cmdline' failed: $!";
182 return undef; # success
185 sub rcs_add ($) {
186 my ($file) = @_;
188 my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
189 if (system(@cmdline) != 0) {
190 warn "'@cmdline' failed: $!";
194 sub rcs_remove ($) {
195 my ($file) = @_;
197 my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
198 if (system(@cmdline) != 0) {
199 warn "'@cmdline' failed: $!";
203 sub rcs_rename ($$) {
204 my ($src, $dest) = @_;
206 my $parent = IkiWiki::dirname($dest);
207 if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
208 warn("bzr add $parent failed\n");
211 my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
212 if (system(@cmdline) != 0) {
213 warn "'@cmdline' failed: $!";
217 sub rcs_recentchanges ($) {
218 my ($num) = @_;
220 my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
221 $config{srcdir});
222 open (my $out, "@cmdline |");
224 eval q{use Date::Parse};
225 error($@) if $@;
227 my @ret;
228 foreach my $info (bzr_log($out)) {
229 my @pages = ();
230 my @message = ();
232 foreach my $msgline (split(/\n/, $info->{message})) {
233 push @message, { line => $msgline };
236 foreach my $file (split(/\n/, $info->{files})) {
237 my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
239 # Skip directories
240 next if ($filename =~ /\/$/);
242 # Skip source name in renames
243 $filename =~ s/^.* => //;
245 my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
246 $diffurl =~ s/\[\[file\]\]/$filename/go;
247 $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
248 $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
250 push @pages, {
251 page => pagename($filename),
252 diffurl => $diffurl,
256 my $user = $info->{"committer"};
257 if (defined($info->{"author"})) { $user = $info->{"author"}; }
258 $user =~ s/\s*<.*>\s*$//;
259 $user =~ s/^\s*//;
261 push @ret, {
262 rev => $info->{"revno"},
263 user => $user,
264 committype => "bzr",
265 when => str2time($info->{"timestamp"}),
266 message => [@message],
267 pages => [@pages],
271 return @ret;
274 sub rcs_diff ($;$) {
275 my $taintedrev=shift;
276 my $maxlines=shift;
277 my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
279 my $prevspec = "before:" . $rev;
280 my $revspec = "revno:" . $rev;
281 my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
282 "--new", $config{srcdir},
283 "-r", $prevspec . ".." . $revspec);
284 open (my $out, "@cmdline |");
285 my @lines;
286 while (my $line=<$out>) {
287 last if defined $maxlines && @lines == $maxlines;
288 push @lines, $line;
290 if (wantarray) {
291 return @lines;
293 else {
294 return join("", @lines);
298 sub extract_timestamp (@) {
299 open (my $out, "-|", @_);
300 my @log = bzr_log($out);
302 if (length @log < 1) {
303 return 0;
306 eval q{use Date::Parse};
307 error($@) if $@;
309 my $time = str2time($log[0]->{"timestamp"});
310 return $time;
313 sub rcs_getctime ($) {
314 my ($file) = @_;
316 my @cmdline = ("bzr", "log", "--forward", "--limit", '1', "$config{srcdir}/$file");
317 return extract_timestamp(@cmdline);
320 sub rcs_getmtime ($) {
321 my ($file) = @_;
323 my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
324 return extract_timestamp(@cmdline);