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