summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/bzr.pm
blob: e414e85d2da2136475527a6b5a20f4f1c45f6703 (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;
  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 rcs_commit ($$$;$$) { #{{{
  49. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  50. if (defined $user) {
  51. $user = possibly_foolish_untaint($user);
  52. }
  53. elsif (defined $ipaddr) {
  54. $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
  55. }
  56. else {
  57. $user = "Anonymous";
  58. }
  59. $message = possibly_foolish_untaint($message);
  60. if (! length $message) {
  61. $message = "no message given";
  62. }
  63. my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
  64. $config{srcdir}."/".$file);
  65. if (system(@cmdline) != 0) {
  66. warn "'@cmdline' failed: $!";
  67. }
  68. return undef; # success
  69. } #}}}
  70. sub rcs_commit_staged ($$$) {
  71. # Commits all staged changes. Changes can be staged using rcs_add,
  72. # rcs_remove, and rcs_rename.
  73. my ($message, $user, $ipaddr)=@_;
  74. error("rcs_commit_staged not implemented for bzr"); # TODO
  75. }
  76. sub rcs_add ($) { # {{{
  77. my ($file) = @_;
  78. my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
  79. if (system(@cmdline) != 0) {
  80. warn "'@cmdline' failed: $!";
  81. }
  82. } #}}}
  83. sub rcs_remove ($) { # {{{
  84. my ($file) = @_;
  85. error("rcs_remove not implemented for bzr"); # TODO
  86. } #}}}
  87. sub rcs_rename ($$) { # {{{
  88. my ($src, $dest) = @_;
  89. error("rcs_rename not implemented for bzr"); # TODO
  90. } #}}}
  91. sub rcs_recentchanges ($) { #{{{
  92. my ($num) = @_;
  93. my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
  94. $config{srcdir});
  95. open (my $out, "@cmdline |");
  96. eval q{use Date::Parse};
  97. error($@) if $@;
  98. my @ret;
  99. foreach my $info (bzr_log($out)) {
  100. my @pages = ();
  101. my @message = ();
  102. foreach my $msgline (split(/\n/, $info->{message})) {
  103. push @message, { line => $msgline };
  104. }
  105. foreach my $file (split(/\n/, $info->{files})) {
  106. my ($filename, $fileid) = split(/[ \t]+/, $file);
  107. my $diffurl = $config{'diffurl'};
  108. $diffurl =~ s/\[\[file\]\]/$filename/go;
  109. $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
  110. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  111. push @pages, {
  112. page => pagename($filename),
  113. diffurl => $diffurl,
  114. };
  115. }
  116. my $user = $info->{"committer"};
  117. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  118. $user =~ s/\s*<.*>\s*$//;
  119. $user =~ s/^\s*//;
  120. push @ret, {
  121. rev => $info->{"revno"},
  122. user => $user,
  123. committype => "bzr",
  124. when => time - str2time($info->{"timestamp"}),
  125. message => [@message],
  126. pages => [@pages],
  127. };
  128. }
  129. return @ret;
  130. } #}}}
  131. sub rcs_getctime ($) { #{{{
  132. my ($file) = @_;
  133. # XXX filename passes through the shell here, should try to avoid
  134. # that just in case
  135. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  136. open (my $out, "@cmdline |");
  137. my @log = bzr_log($out);
  138. if (length @log < 1) {
  139. return 0;
  140. }
  141. eval q{use Date::Parse};
  142. error($@) if $@;
  143. my $ctime = str2time($log[0]->{"timestamp"});
  144. return $ctime;
  145. } #}}}
  146. 1