2 package IkiWiki::Plugin::bzr;
8 use open qw{:utf8 :std};
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);
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"),
38 safe => 0, # rcs plugin
44 #example => "", # FIXME add example
45 description => "bzr post-commit hook to generate",
52 description => "mode for bzr_wrapper (can safely be made suid)",
58 #example => "", # FIXME add example
59 description => "url to show file history, using loggerhead ([[file]] substituted)",
65 example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
66 description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
81 if ($line =~ /^message:/) {
85 elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
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;
97 elsif ($line =~ /: /) {
99 if ($line =~ /^revno: (\d+)/) {
104 ($key, $value) = split /: +/, $line, 2;
106 $info{$key} = $value;
110 push @infos, {%info} if keys %info;
116 my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
117 if (system(@cmdline) != 0) {
118 warn "'@cmdline' failed: $!";
122 sub rcs_prepedit ($) {
126 sub bzr_author ($$) {
127 my ($user, $ipaddr) = @_;
130 return IkiWiki::possibly_foolish_untaint($user);
132 elsif (defined $ipaddr) {
133 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
140 sub rcs_commit ($$$;$$) {
141 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
143 $user = bzr_author($user, $ipaddr);
145 $message = IkiWiki::possibly_foolish_untaint($message);
146 if (! length $message) {
147 $message = "no message given";
150 my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
151 $config{srcdir}."/".$file);
152 if (system(@cmdline) != 0) {
153 warn "'@cmdline' failed: $!";
156 return undef; # success
159 sub rcs_commit_staged ($$$) {
160 # Commits all staged changes. Changes can be staged using rcs_add,
161 # rcs_remove, and rcs_rename.
162 my ($message, $user, $ipaddr)=@_;
164 $user = bzr_author($user, $ipaddr);
166 $message = IkiWiki::possibly_foolish_untaint($message);
167 if (! length $message) {
168 $message = "no message given";
171 my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
173 if (system(@cmdline) != 0) {
174 warn "'@cmdline' failed: $!";
177 return undef; # success
183 my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
184 if (system(@cmdline) != 0) {
185 warn "'@cmdline' failed: $!";
192 my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
193 if (system(@cmdline) != 0) {
194 warn "'@cmdline' failed: $!";
198 sub rcs_rename ($$) {
199 my ($src, $dest) = @_;
201 my $parent = IkiWiki::dirname($dest);
202 if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
203 warn("bzr add $parent failed\n");
206 my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
207 if (system(@cmdline) != 0) {
208 warn "'@cmdline' failed: $!";
212 sub rcs_recentchanges ($) {
215 my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
217 open (my $out, "@cmdline |");
219 eval q{use Date::Parse};
223 foreach my $info (bzr_log($out)) {
227 foreach my $msgline (split(/\n/, $info->{message})) {
228 push @message, { line => $msgline };
231 foreach my $file (split(/\n/, $info->{files})) {
232 my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
235 next if ($filename =~ /\/$/);
237 # Skip source name in renames
238 $filename =~ s/^.* => //;
240 my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
241 $diffurl =~ s/\[\[file\]\]/$filename/go;
242 $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
243 $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
246 page => pagename($filename),
251 my $user = $info->{"committer"};
252 if (defined($info->{"author"})) { $user = $info->{"author"}; }
253 $user =~ s/\s*<.*>\s*$//;
257 rev => $info->{"revno"},
260 when => str2time($info->{"timestamp"}),
261 message => [@message],
270 my $taintedrev=shift;
271 my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
273 my $prevspec = "before:" . $rev;
274 my $revspec = "revno:" . $rev;
275 my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
276 "--new", $config{srcdir},
277 "-r", $prevspec . ".." . $revspec);
278 open (my $out, "@cmdline |");
285 return join("", @lines);
289 sub rcs_getctime ($) {
292 # XXX filename passes through the shell here, should try to avoid
294 my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
295 open (my $out, "@cmdline |");
297 my @log = bzr_log($out);
299 if (length @log < 1) {
303 eval q{use Date::Parse};
306 my $ctime = str2time($log[0]->{"timestamp"});
310 sub rcs_getmtime ($) {
311 error "rcs_getmtime is not implemented for bzr\n"; # TODO