summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/bzr.pm
blob: 6e45c39e44ccefaa37851ea6a0b604362167c437 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::bzr;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. use open qw{:utf8 :std};
  8. sub import {
  9. hook(type => "checkconfig", id => "bzr", call => \&checkconfig);
  10. hook(type => "getsetup", id => "bzr", call => \&getsetup);
  11. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  12. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  13. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  14. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  15. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  16. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  17. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  18. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  19. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  20. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  21. }
  22. sub checkconfig () {
  23. if (defined $config{bzr_wrapper} && length $config{bzr_wrapper}) {
  24. push @{$config{wrappers}}, {
  25. wrapper => $config{bzr_wrapper},
  26. wrappermode => (defined $config{bzr_wrappermode} ? $config{bzr_wrappermode} : "06755"),
  27. };
  28. }
  29. }
  30. sub getsetup () {
  31. return
  32. plugin => {
  33. safe => 0, # rcs plugin
  34. rebuild => undef,
  35. section => "rcs",
  36. },
  37. bzr_wrapper => {
  38. type => "string",
  39. #example => "", # FIXME add example
  40. description => "bzr post-commit hook to generate",
  41. safe => 0, # file
  42. rebuild => 0,
  43. },
  44. bzr_wrappermode => {
  45. type => "string",
  46. example => '06755',
  47. description => "mode for bzr_wrapper (can safely be made suid)",
  48. safe => 0,
  49. rebuild => 0,
  50. },
  51. historyurl => {
  52. type => "string",
  53. #example => "", # FIXME add example
  54. description => "url to show file history, using loggerhead ([[file]] substituted)",
  55. safe => 1,
  56. rebuild => 1,
  57. },
  58. diffurl => {
  59. type => "string",
  60. example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
  61. description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
  62. safe => 1,
  63. rebuild => 1,
  64. },
  65. }
  66. sub bzr_log ($) {
  67. my $out = shift;
  68. my @infos = ();
  69. my $key = undef;
  70. my $hash = {};
  71. while (<$out>) {
  72. my $line = $_;
  73. my ($value);
  74. if ($line =~ /^message:/) {
  75. $key = "message";
  76. $$hash{$key} = "";
  77. }
  78. elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
  79. $key = "files";
  80. unless (defined($$hash{$key})) { $$hash{$key} = ""; }
  81. }
  82. elsif (defined($key) and $line =~ /^ (.*)/) {
  83. $$hash{$key} .= "$1\n";
  84. }
  85. elsif ($line eq "------------------------------------------------------------\n") {
  86. if (keys %$hash) {
  87. push (@infos, $hash);
  88. }
  89. $hash = {};
  90. $key = undef;
  91. }
  92. elsif ($line =~ /: /) {
  93. chomp $line;
  94. if ($line =~ /^revno: (\d+)/) {
  95. $key = "revno";
  96. $value = $1;
  97. }
  98. else {
  99. ($key, $value) = split /: +/, $line, 2;
  100. }
  101. $$hash{$key} = $value;
  102. }
  103. }
  104. close $out;
  105. return @infos;
  106. }
  107. sub rcs_update () {
  108. my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
  109. if (system(@cmdline) != 0) {
  110. warn "'@cmdline' failed: $!";
  111. }
  112. }
  113. sub rcs_prepedit ($) {
  114. return "";
  115. }
  116. sub bzr_author ($$) {
  117. my ($user, $ipaddr) = @_;
  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. sub rcs_commit ($$$;$$) {
  129. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  130. $user = bzr_author($user, $ipaddr);
  131. $message = IkiWiki::possibly_foolish_untaint($message);
  132. if (! length $message) {
  133. $message = "no message given";
  134. }
  135. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  136. $config{srcdir}."/".$file);
  137. if (system(@cmdline) != 0) {
  138. warn "'@cmdline' failed: $!";
  139. }
  140. return undef; # success
  141. }
  142. sub rcs_commit_staged ($$$) {
  143. # Commits all staged changes. Changes can be staged using rcs_add,
  144. # rcs_remove, and rcs_rename.
  145. my ($message, $user, $ipaddr)=@_;
  146. $user = bzr_author($user, $ipaddr);
  147. $message = IkiWiki::possibly_foolish_untaint($message);
  148. if (! length $message) {
  149. $message = "no message given";
  150. }
  151. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  152. $config{srcdir});
  153. if (system(@cmdline) != 0) {
  154. warn "'@cmdline' failed: $!";
  155. }
  156. return undef; # success
  157. }
  158. sub rcs_add ($) {
  159. my ($file) = @_;
  160. my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
  161. if (system(@cmdline) != 0) {
  162. warn "'@cmdline' failed: $!";
  163. }
  164. }
  165. sub rcs_remove ($) {
  166. my ($file) = @_;
  167. my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
  168. if (system(@cmdline) != 0) {
  169. warn "'@cmdline' failed: $!";
  170. }
  171. }
  172. sub rcs_rename ($$) {
  173. my ($src, $dest) = @_;
  174. my $parent = IkiWiki::dirname($dest);
  175. if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
  176. warn("bzr add $parent failed\n");
  177. }
  178. my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
  179. if (system(@cmdline) != 0) {
  180. warn "'@cmdline' failed: $!";
  181. }
  182. }
  183. sub rcs_recentchanges ($) {
  184. my ($num) = @_;
  185. my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
  186. $config{srcdir});
  187. open (my $out, "@cmdline |");
  188. eval q{use Date::Parse};
  189. error($@) if $@;
  190. my @ret;
  191. foreach my $info (bzr_log($out)) {
  192. my @pages = ();
  193. my @message = ();
  194. foreach my $msgline (split(/\n/, $info->{message})) {
  195. push @message, { line => $msgline };
  196. }
  197. foreach my $file (split(/\n/, $info->{files})) {
  198. my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
  199. # Skip directories
  200. next if ($filename =~ /\/$/);
  201. # Skip source name in renames
  202. $filename =~ s/^.* => //;
  203. my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
  204. $diffurl =~ s/\[\[file\]\]/$filename/go;
  205. $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
  206. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  207. push @pages, {
  208. page => pagename($filename),
  209. diffurl => $diffurl,
  210. };
  211. }
  212. my $user = $info->{"committer"};
  213. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  214. $user =~ s/\s*<.*>\s*$//;
  215. $user =~ s/^\s*//;
  216. push @ret, {
  217. rev => $info->{"revno"},
  218. user => $user,
  219. committype => "bzr",
  220. when => str2time($info->{"timestamp"}),
  221. message => [@message],
  222. pages => [@pages],
  223. };
  224. }
  225. return @ret;
  226. }
  227. sub rcs_diff ($) {
  228. my $taintedrev=shift;
  229. my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
  230. my $prevspec = "before:" . $rev;
  231. my $revspec = "revno:" . $rev;
  232. my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
  233. "--new", $config{srcdir},
  234. "-r", $prevspec . ".." . $revspec);
  235. open (my $out, "@cmdline |");
  236. my @lines = <$out>;
  237. if (wantarray) {
  238. return @lines;
  239. }
  240. else {
  241. return join("", @lines);
  242. }
  243. }
  244. sub rcs_getctime ($) {
  245. my ($file) = @_;
  246. # XXX filename passes through the shell here, should try to avoid
  247. # that just in case
  248. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  249. open (my $out, "@cmdline |");
  250. my @log = bzr_log($out);
  251. if (length @log < 1) {
  252. return 0;
  253. }
  254. eval q{use Date::Parse};
  255. error($@) if $@;
  256. my $ctime = str2time($log[0]->{"timestamp"});
  257. return $ctime;
  258. }
  259. 1