summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/bzr.pm
blob: a04bfe1cbe39a41d48937b4006215b1222ff2183 (plain)
  1. #!/usr/bin/perl
  2. use warnings;
  3. use strict;
  4. use IkiWiki;
  5. use Encode;
  6. use open qw{:utf8 :std};
  7. package IkiWiki;
  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", $config{srcdir}, "update");
  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", "-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_add ($) { # {{{
  71. my ($file) = @_;
  72. my @cmdline = ("bzr", "add", "$config{srcdir}/$file");
  73. if (system(@cmdline) != 0) {
  74. warn "'@cmdline' failed: $!";
  75. }
  76. } #}}}
  77. sub rcs_recentchanges ($) { #{{{
  78. my ($num) = @_;
  79. eval q{use CGI 'escapeHTML'};
  80. error($@) if $@;
  81. my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
  82. $config{srcdir});
  83. open (my $out, "@cmdline |");
  84. eval q{use Date::Parse};
  85. error($@) if $@;
  86. my @ret;
  87. foreach my $info (bzr_log($out)) {
  88. my @pages = ();
  89. my @message = ();
  90. foreach my $msgline (split(/\n/, $info->{message})) {
  91. push @message, { line => $msgline };
  92. }
  93. foreach my $file (split(/\n/, $info->{files})) {
  94. my ($filename, $fileid) = split(/[ \t]+/, $file);
  95. my $diffurl = $config{'diffurl'};
  96. $diffurl =~ s/\[\[file\]\]/$filename/go;
  97. $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
  98. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  99. push @pages, {
  100. page => pagename($filename),
  101. diffurl => $diffurl,
  102. };
  103. }
  104. my $user = $info->{"committer"};
  105. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  106. $user =~ s/\s*<.*>\s*$//;
  107. $user =~ s/^\s*//;
  108. push @ret, {
  109. rev => $info->{"revno"},
  110. user => $user,
  111. committype => "bzr",
  112. when => time - str2time($info->{"timestamp"}),
  113. message => [@message],
  114. pages => [@pages],
  115. };
  116. }
  117. return @ret;
  118. } #}}}
  119. sub rcs_getctime ($) { #{{{
  120. my ($file) = @_;
  121. # XXX filename passes through the shell here, should try to avoid
  122. # that just in case
  123. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  124. open (my $out, "@cmdline |");
  125. my @log = bzr_log($out);
  126. if (length @log < 1) {
  127. return 0;
  128. }
  129. eval q{use Date::Parse};
  130. error($@) if $@;
  131. my $ctime = str2time($log[0]->{"timestamp"});
  132. return $ctime;
  133. } #}}}
  134. 1