summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/bzr.pm
blob: 0b50592924915b0040fa530095fd575985da08ea (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. bzr_wrapper => {
  33. type => "string",
  34. #example => "", # FIXME add example
  35. description => "bzr post-commit hook to generate",
  36. safe => 0, # file
  37. rebuild => 0,
  38. },
  39. bzr_wrappermode => {
  40. type => "string",
  41. example => '06755',
  42. description => "mode for bzr_wrapper (can safely be made suid)",
  43. safe => 0,
  44. rebuild => 0,
  45. },
  46. historyurl => {
  47. type => "string",
  48. #example => "", # FIXME add example
  49. description => "url to show file history, using loggerhead ([[file]] substituted)",
  50. safe => 1,
  51. rebuild => 1,
  52. },
  53. diffurl => {
  54. type => "string",
  55. example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
  56. description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
  57. safe => 1,
  58. rebuild => 1,
  59. },
  60. } #}}}
  61. sub bzr_log ($) { #{{{
  62. my $out = shift;
  63. my @infos = ();
  64. my $key = undef;
  65. while (<$out>) {
  66. my $line = $_;
  67. my ($value);
  68. if ($line =~ /^message:/) {
  69. $key = "message";
  70. $infos[$#infos]{$key} = "";
  71. }
  72. elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
  73. $key = "files";
  74. unless (defined($infos[$#infos]{$key})) { $infos[$#infos]{$key} = ""; }
  75. }
  76. elsif (defined($key) and $line =~ /^ (.*)/) {
  77. $infos[$#infos]{$key} .= "$1\n";
  78. }
  79. elsif ($line eq "------------------------------------------------------------\n") {
  80. $key = undef;
  81. push (@infos, {});
  82. }
  83. else {
  84. chomp $line;
  85. ($key, $value) = split /: +/, $line, 2;
  86. $infos[$#infos]{$key} = $value;
  87. }
  88. }
  89. close $out;
  90. return @infos;
  91. } #}}}
  92. sub rcs_update () { #{{{
  93. my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
  94. if (system(@cmdline) != 0) {
  95. warn "'@cmdline' failed: $!";
  96. }
  97. } #}}}
  98. sub rcs_prepedit ($) { #{{{
  99. return "";
  100. } #}}}
  101. sub bzr_author ($$) { #{{{
  102. my ($user, $ipaddr) = @_;
  103. if (defined $user) {
  104. return IkiWiki::possibly_foolish_untaint($user);
  105. }
  106. elsif (defined $ipaddr) {
  107. return "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
  108. }
  109. else {
  110. return "Anonymous";
  111. }
  112. } #}}}
  113. sub rcs_commit ($$$;$$) { #{{{
  114. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  115. $user = bzr_author($user, $ipaddr);
  116. $message = IkiWiki::possibly_foolish_untaint($message);
  117. if (! length $message) {
  118. $message = "no message given";
  119. }
  120. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  121. $config{srcdir}."/".$file);
  122. if (system(@cmdline) != 0) {
  123. warn "'@cmdline' failed: $!";
  124. }
  125. return undef; # success
  126. } #}}}
  127. sub rcs_commit_staged ($$$) {
  128. # Commits all staged changes. Changes can be staged using rcs_add,
  129. # rcs_remove, and rcs_rename.
  130. my ($message, $user, $ipaddr)=@_;
  131. $user = bzr_author($user, $ipaddr);
  132. $message = IkiWiki::possibly_foolish_untaint($message);
  133. if (! length $message) {
  134. $message = "no message given";
  135. }
  136. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  137. $config{srcdir});
  138. if (system(@cmdline) != 0) {
  139. warn "'@cmdline' failed: $!";
  140. }
  141. return undef; # success
  142. } #}}}
  143. sub rcs_add ($) { # {{{
  144. my ($file) = @_;
  145. my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
  146. if (system(@cmdline) != 0) {
  147. warn "'@cmdline' failed: $!";
  148. }
  149. } #}}}
  150. sub rcs_remove ($) { # {{{
  151. my ($file) = @_;
  152. my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
  153. if (system(@cmdline) != 0) {
  154. warn "'@cmdline' failed: $!";
  155. }
  156. } #}}}
  157. sub rcs_rename ($$) { # {{{
  158. my ($src, $dest) = @_;
  159. my $parent = IkiWiki::dirname($dest);
  160. if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
  161. warn("bzr add $parent failed\n");
  162. }
  163. my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
  164. if (system(@cmdline) != 0) {
  165. warn "'@cmdline' failed: $!";
  166. }
  167. } #}}}
  168. sub rcs_recentchanges ($) { #{{{
  169. my ($num) = @_;
  170. my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
  171. $config{srcdir});
  172. open (my $out, "@cmdline |");
  173. eval q{use Date::Parse};
  174. error($@) if $@;
  175. my @ret;
  176. foreach my $info (bzr_log($out)) {
  177. my @pages = ();
  178. my @message = ();
  179. foreach my $msgline (split(/\n/, $info->{message})) {
  180. push @message, { line => $msgline };
  181. }
  182. foreach my $file (split(/\n/, $info->{files})) {
  183. my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
  184. # Skip directories
  185. next if ($filename =~ /\/$/);
  186. # Skip source name in renames
  187. $filename =~ s/^.* => //;
  188. my $diffurl = defined $config{'diffurl'} ? $config{'diffurl'} : "";
  189. $diffurl =~ s/\[\[file\]\]/$filename/go;
  190. $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
  191. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  192. push @pages, {
  193. page => pagename($filename),
  194. diffurl => $diffurl,
  195. };
  196. }
  197. my $user = $info->{"committer"};
  198. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  199. $user =~ s/\s*<.*>\s*$//;
  200. $user =~ s/^\s*//;
  201. push @ret, {
  202. rev => $info->{"revno"},
  203. user => $user,
  204. committype => "bzr",
  205. when => time - str2time($info->{"timestamp"}),
  206. message => [@message],
  207. pages => [@pages],
  208. };
  209. }
  210. return @ret;
  211. } #}}}
  212. sub rcs_getctime ($) { #{{{
  213. my ($file) = @_;
  214. # XXX filename passes through the shell here, should try to avoid
  215. # that just in case
  216. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  217. open (my $out, "@cmdline |");
  218. my @log = bzr_log($out);
  219. if (length @log < 1) {
  220. return 0;
  221. }
  222. eval q{use Date::Parse};
  223. error($@) if $@;
  224. my $ctime = str2time($log[0]->{"timestamp"});
  225. return $ctime;
  226. } #}}}
  227. 1