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