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);
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"),
37 safe => 0, # rcs plugin
43 #example => "", # FIXME add example
44 description => "bzr post-commit hook to generate",
51 description => "mode for bzr_wrapper (can safely be made suid)",
57 #example => "", # FIXME add example
58 description => "url to show file history, using loggerhead ([[file]] substituted)",
64 example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
65 description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
79 if ($line =~ /^message:/) {
81 $infos[$#infos]{$key} = "";
83 elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
85 unless (defined($infos[$#infos]{$key})) { $infos[$#infos]{$key} = ""; }
87 elsif (defined($key) and $line =~ /^ (.*)/) {
88 $infos[$#infos]{$key} .= "$1\n";
90 elsif ($line eq "------------------------------------------------------------\n") {
96 ($key, $value) = split /: +/, $line, 2;
97 $infos[$#infos]{$key} = $value;
106 my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
107 if (system(@cmdline) != 0) {
108 warn "'@cmdline' failed: $!";
112 sub rcs_prepedit ($) {
116 sub bzr_author ($$) {
117 my ($user, $ipaddr) = @_;
120 return IkiWiki::possibly_foolish_untaint($user);
122 elsif (defined $ipaddr) {
123 return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
130 sub rcs_commit ($$$;$$) {
131 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
133 $user = bzr_author($user, $ipaddr);
135 $message = IkiWiki::possibly_foolish_untaint($message);
136 if (! length $message) {
137 $message = "no message given";
140 my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
141 $config{srcdir}."/".$file);
142 if (system(@cmdline) != 0) {
143 warn "'@cmdline' failed: $!";
146 return undef; # success
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)=@_;
154 $user = bzr_author($user, $ipaddr);
156 $message = IkiWiki::possibly_foolish_untaint($message);
157 if (! length $message) {
158 $message = "no message given";
161 my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
163 if (system(@cmdline) != 0) {
164 warn "'@cmdline' failed: $!";
167 return undef; # success
173 my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
174 if (system(@cmdline) != 0) {
175 warn "'@cmdline' failed: $!";
182 my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
183 if (system(@cmdline) != 0) {
184 warn "'@cmdline' failed: $!";
188 sub rcs_rename ($$) {
189 my ($src, $dest) = @_;
191 my $parent = IkiWiki::dirname($dest);
192 if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
193 warn("bzr add $parent failed\n");
196 my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
197 if (system(@cmdline) != 0) {
198 warn "'@cmdline' failed: $!";
202 sub rcs_recentchanges ($) {
205 my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
207 open (my $out, "@cmdline |");
209 eval q{use Date::Parse};
213 foreach my $info (bzr_log($out)) {
217 foreach my $msgline (split(/\n/, $info->{message})) {
218 push @message, { line => $msgline };
221 foreach my $file (split(/\n/, $info->{files})) {
222 my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
225 next if ($filename =~ /\/$/);
227 # Skip source name in renames
228 $filename =~ s/^.* => //;
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;
236 page => pagename($filename),
241 my $user = $info->{"committer"};
242 if (defined($info->{"author"})) { $user = $info->{"author"}; }
243 $user =~ s/\s*<.*>\s*$//;
247 rev => $info->{"revno"},
250 when => str2time($info->{"timestamp"}),
251 message => [@message],
260 my $taintedrev=shift;
261 my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
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 |");
275 return join("", @lines);
279 sub rcs_getctime ($) {
282 # XXX filename passes through the shell here, should try to avoid
284 my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
285 open (my $out, "@cmdline |");
287 my @log = bzr_log($out);
289 if (length @log < 1) {
293 eval q{use Date::Parse};
296 my $ctime = str2time($log[0]->{"timestamp"});