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