2 package IkiWiki::Plugin::bzr;
8 use URI::Escape q{uri_escape_utf8};
9 use open qw{:utf8 :std};
12 hook(type => "checkconfig", id => "bzr", call => \&checkconfig);
13 hook(type => "getsetup", id => "bzr", call => \&getsetup);
14 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
15 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
16 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
17 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
18 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
19 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
20 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
21 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
22 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
23 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
24 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
28 if (defined $config{bzr_wrapper} && length $config{bzr_wrapper}) {
29 push @{$config{wrappers}}, {
30 wrapper => $config{bzr_wrapper},
31 wrappermode => (defined $config{bzr_wrappermode} ? $config{bzr_wrappermode} : "06755"),
39 safe => 0, # rcs plugin
45 #example => "", # FIXME add example
46 description => "bzr post-commit hook to generate",
53 description => "mode for bzr_wrapper (can safely be made suid)",
59 #example => "", # FIXME add example
60 description => "url to show file history, using loggerhead ([[file]] substituted)",
66 example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
67 description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
82 if ($line =~ /^message:/) {
86 elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
88 $info{$key} = "" unless defined $info{$key};
90 elsif (defined($key) and $line =~ /^ (.*)/) {
91 $info{$key} .= "$1\n";
93 elsif ($line eq "------------------------------------------------------------\n") {
94 push @infos, {%info} if keys %info;
98 elsif ($line =~ /: /) {
100 if ($line =~ /^revno: (\d+)/) {
105 ($key, $value) = split /: +/, $line, 2;
107 $info{$key} = $value;
111 push @infos, {%info} if keys %info;
117 my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
118 if (system(@cmdline) != 0) {
119 warn "'@cmdline' failed: $!";
123 sub rcs_prepedit ($) {
130 return unless defined $session;
132 my $user=$session->param("name");
133 my $ipaddr=$session->remote_addr();
136 return IkiWiki::possibly_foolish_untaint($user);
138 elsif (defined $ipaddr) {
139 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
149 my $user=bzr_author($params{session});
151 $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
152 if (! length $params{message}) {
153 $params{message} = "no message given";
156 my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
157 (defined $user ? ("--author", $user) : ()),
158 $config{srcdir}."/".$params{file});
159 if (system(@cmdline) != 0) {
160 warn "'@cmdline' failed: $!";
163 return undef; # success
166 sub rcs_commit_staged (@) {
169 my $user=bzr_author($params{session});
171 $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
172 if (! length $params{message}) {
173 $params{message} = "no message given";
176 my @cmdline = ("bzr", "commit", "--quiet", "-m", $params{message},
177 (defined $user ? ("--author", $user) : ()),
179 if (system(@cmdline) != 0) {
180 warn "'@cmdline' failed: $!";
183 return undef; # success
189 my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
190 if (system(@cmdline) != 0) {
191 warn "'@cmdline' failed: $!";
198 my @cmdline = ("bzr", "rm", "--quiet", "$config{srcdir}/$file");
199 if (system(@cmdline) != 0) {
200 warn "'@cmdline' failed: $!";
204 sub rcs_rename ($$) {
205 my ($src, $dest) = @_;
207 my $parent = IkiWiki::dirname($dest);
208 if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
209 warn("bzr add $parent failed\n");
212 my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
213 if (system(@cmdline) != 0) {
214 warn "'@cmdline' failed: $!";
218 sub rcs_recentchanges ($) {
221 my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
223 open (my $out, "@cmdline |");
225 eval q{use Date::Parse};
229 foreach my $info (bzr_log($out)) {
233 foreach my $msgline (split(/\n/, $info->{message})) {
234 push @message, { line => $msgline };
237 foreach my $file (split(/\n/, $info->{files})) {
238 my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
241 next if ($filename =~ /\/$/);
243 # Skip source name in renames
244 $filename =~ s/^.* => //;
246 my $efilename = uri_escape_utf8($filename);
248 my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
249 $diffurl =~ s/\[\[file\]\]/$efilename/go;
250 $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
251 $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
254 page => pagename($filename),
259 my $user = $info->{"committer"};
260 if (defined($info->{"author"})) { $user = $info->{"author"}; }
261 $user =~ s/\s*<.*>\s*$//;
265 rev => $info->{"revno"},
268 when => str2time($info->{"timestamp"}),
269 message => [@message],
278 my $taintedrev=shift;
280 my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
282 my $prevspec = "before:" . $rev;
283 my $revspec = "revno:" . $rev;
284 my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
285 "--new", $config{srcdir},
286 "-r", $prevspec . ".." . $revspec);
287 open (my $out, "@cmdline |");
289 while (my $line=<$out>) {
290 last if defined $maxlines && @lines == $maxlines;
297 return join("", @lines);
301 sub extract_timestamp (@) {
302 open (my $out, "-|", @_);
303 my @log = bzr_log($out);
305 if (length @log < 1) {
309 eval q{use Date::Parse};
312 my $time = str2time($log[0]->{"timestamp"});
316 sub rcs_getctime ($) {
319 my @cmdline = ("bzr", "log", "--forward", "--limit", '1', "$config{srcdir}/$file");
320 return extract_timestamp(@cmdline);
323 sub rcs_getmtime ($) {
326 my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
327 return extract_timestamp(@cmdline);