summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/bzr.pm
blob: 1ffdc23535d81578b9fab129953659028a6ec96e (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. while (<$out>) {
  71. my $line = $_;
  72. my ($value);
  73. if ($line =~ /^message:/) {
  74. $key = "message";
  75. $infos[$#infos]{$key} = "";
  76. }
  77. elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
  78. $key = "files";
  79. unless (defined($infos[$#infos]{$key})) { $infos[$#infos]{$key} = ""; }
  80. }
  81. elsif (defined($key) and $line =~ /^ (.*)/) {
  82. $infos[$#infos]{$key} .= "$1\n";
  83. }
  84. elsif ($line eq "------------------------------------------------------------\n") {
  85. $key = undef;
  86. push (@infos, {});
  87. }
  88. else {
  89. chomp $line;
  90. ($key, $value) = split /: +/, $line, 2;
  91. $infos[$#infos]{$key} = $value;
  92. }
  93. }
  94. close $out;
  95. return @infos;
  96. }
  97. sub rcs_update () {
  98. my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
  99. if (system(@cmdline) != 0) {
  100. warn "'@cmdline' failed: $!";
  101. }
  102. }
  103. sub rcs_prepedit ($) {
  104. return "";
  105. }
  106. sub bzr_author ($$) {
  107. my ($user, $ipaddr) = @_;
  108. if (defined $user) {
  109. return IkiWiki::possibly_foolish_untaint($user);
  110. }
  111. elsif (defined $ipaddr) {
  112. return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
  113. }
  114. else {
  115. return "Anonymous";
  116. }
  117. }
  118. sub rcs_commit ($$$;$$) {
  119. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  120. $user = bzr_author($user, $ipaddr);
  121. $message = IkiWiki::possibly_foolish_untaint($message);
  122. if (! length $message) {
  123. $message = "no message given";
  124. }
  125. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  126. $config{srcdir}."/".$file);
  127. if (system(@cmdline) != 0) {
  128. warn "'@cmdline' failed: $!";
  129. }
  130. return undef; # success
  131. }
  132. sub rcs_commit_staged ($$$) {
  133. # Commits all staged changes. Changes can be staged using rcs_add,
  134. # rcs_remove, and rcs_rename.
  135. my ($message, $user, $ipaddr)=@_;
  136. $user = bzr_author($user, $ipaddr);
  137. $message = IkiWiki::possibly_foolish_untaint($message);
  138. if (! length $message) {
  139. $message = "no message given";
  140. }
  141. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  142. $config{srcdir});
  143. if (system(@cmdline) != 0) {
  144. warn "'@cmdline' failed: $!";
  145. }
  146. return undef; # success
  147. }
  148. sub rcs_add ($) {
  149. my ($file) = @_;
  150. my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
  151. if (system(@cmdline) != 0) {
  152. warn "'@cmdline' failed: $!";
  153. }
  154. }
  155. sub rcs_remove ($) {
  156. my ($file) = @_;
  157. my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
  158. if (system(@cmdline) != 0) {
  159. warn "'@cmdline' failed: $!";
  160. }
  161. }
  162. sub rcs_rename ($$) {
  163. my ($src, $dest) = @_;
  164. my $parent = IkiWiki::dirname($dest);
  165. if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
  166. warn("bzr add $parent failed\n");
  167. }
  168. my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
  169. if (system(@cmdline) != 0) {
  170. warn "'@cmdline' failed: $!";
  171. }
  172. }
  173. sub rcs_recentchanges ($) {
  174. my ($num) = @_;
  175. my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
  176. $config{srcdir});
  177. open (my $out, "@cmdline |");
  178. eval q{use Date::Parse};
  179. error($@) if $@;
  180. my @ret;
  181. foreach my $info (bzr_log($out)) {
  182. my @pages = ();
  183. my @message = ();
  184. foreach my $msgline (split(/\n/, $info->{message})) {
  185. push @message, { line => $msgline };
  186. }
  187. foreach my $file (split(/\n/, $info->{files})) {
  188. my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
  189. # Skip directories
  190. next if ($filename =~ /\/$/);
  191. # Skip source name in renames
  192. $filename =~ s/^.* => //;
  193. my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
  194. $diffurl =~ s/\[\[file\]\]/$filename/go;
  195. $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
  196. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  197. push @pages, {
  198. page => pagename($filename),
  199. diffurl => $diffurl,
  200. };
  201. }
  202. my $user = $info->{"committer"};
  203. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  204. $user =~ s/\s*<.*>\s*$//;
  205. $user =~ s/^\s*//;
  206. push @ret, {
  207. rev => $info->{"revno"},
  208. user => $user,
  209. committype => "bzr",
  210. when => str2time($info->{"timestamp"}),
  211. message => [@message],
  212. pages => [@pages],
  213. };
  214. }
  215. return @ret;
  216. }
  217. sub rcs_diff ($) {
  218. my $taintedrev=shift;
  219. my ($rev) = $taintedrev =~ /^(\d+(\.\d+)*)$/; # untaint
  220. my $prevspec = "before:" . $rev;
  221. my $revspec = "revno:" . $rev;
  222. my @cmdline = ("bzr", "diff", "--old", $config{srcdir},
  223. "--new", $config{srcdir},
  224. "-r", $prevspec . ".." . $revspec);
  225. open (my $out, "@cmdline |");
  226. my @lines = <$out>;
  227. if (wantarray) {
  228. return @lines;
  229. }
  230. else {
  231. return join("", @lines);
  232. }
  233. }
  234. sub rcs_getctime ($) {
  235. my ($file) = @_;
  236. # XXX filename passes through the shell here, should try to avoid
  237. # that just in case
  238. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  239. open (my $out, "@cmdline |");
  240. my @log = bzr_log($out);
  241. if (length @log < 1) {
  242. return 0;
  243. }
  244. eval q{use Date::Parse};
  245. error($@) if $@;
  246. my $ctime = str2time($log[0]->{"timestamp"});
  247. return $ctime;
  248. }
  249. 1