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