summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/bazaar.pm
blob: 80aba6a4556d952d1a018f3d68cfe1f46e82f7db (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 bazaar_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. } elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
  19. $key = "files";
  20. unless (defined($infos[$#infos]{$key})) { $infos[$#infos]{$key} = ""; }
  21. } elsif (defined($key) and $line =~ /^ (.*)/) {
  22. $infos[$#infos]{$key} .= $1;
  23. } elsif ($line eq "------------------------------------------------------------\n") {
  24. $key = undef;
  25. push (@infos, {});
  26. } else {
  27. chomp $line;
  28. ($key, $value) = split /: +/, $line, 2;
  29. $infos[$#infos]{$key} = $value;
  30. }
  31. }
  32. close $out;
  33. return @infos;
  34. }
  35. sub rcs_update () { #{{{
  36. my @cmdline = ("bzr", "$config{srcdir}", "update");
  37. if (system(@cmdline) != 0) {
  38. warn "'@cmdline' failed: $!";
  39. }
  40. } #}}}
  41. sub rcs_prepedit ($) { #{{{
  42. return "";
  43. } #}}}
  44. sub rcs_commit ($$$;$$) { #{{{
  45. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  46. if (defined $user) {
  47. $user = possibly_foolish_untaint($user);
  48. }
  49. elsif (defined $ipaddr) {
  50. $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
  51. }
  52. else {
  53. $user = "Anonymous";
  54. }
  55. $message = possibly_foolish_untaint($message);
  56. if (! length $message) {
  57. $message = "no message given";
  58. }
  59. my @cmdline = ("bzr", "commit",
  60. "-m", $message, "--author", $user, $config{srcdir});
  61. if (system(@cmdline) != 0) {
  62. warn "'@cmdline' failed: $!";
  63. }
  64. return undef; # success
  65. } #}}}
  66. sub rcs_add ($) { # {{{
  67. my ($file) = @_;
  68. my @cmdline = ("bzr", "add", "$config{srcdir}/$file");
  69. if (system(@cmdline) != 0) {
  70. warn "'@cmdline' failed: $!";
  71. }
  72. } #}}}
  73. sub rcs_recentchanges ($) { #{{{
  74. my ($num) = @_;
  75. eval q{use CGI 'escapeHTML'};
  76. error($@) if $@;
  77. my @cmdline = ("bzr", "log", "-v", "--limit", $num, $config{srcdir});
  78. open (my $out, "@cmdline |");
  79. eval q{use Date::Parse};
  80. error($@) if $@;
  81. my @ret;
  82. foreach my $info (bazaar_log($out)) {
  83. my @pages = ();
  84. my @message = ();
  85. foreach my $msgline (split(/\n/, $info->{message})) {
  86. push @message, { line => $msgline };
  87. }
  88. foreach my $file (split(/\n/, $info->{files})) {
  89. my $diffurl = $config{'diffurl'};
  90. $diffurl =~ s/\[\[file\]\]/$file/go;
  91. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  92. push @pages, {
  93. page => pagename($file),
  94. diffurl => $diffurl,
  95. };
  96. }
  97. my $user = $info->{"committer"};
  98. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  99. $user =~ s/\s*<.*>\s*$//;
  100. $user =~ s/^\s*//;
  101. push @ret, {
  102. rev => $info->{"revno"},
  103. user => $user,
  104. committype => "bazaar",
  105. when => time - str2time($info->{"timestamp"}),
  106. message => [@message],
  107. pages => [@pages],
  108. };
  109. }
  110. return @ret;
  111. } #}}}
  112. sub rcs_notify () { #{{{
  113. # TODO
  114. } #}}}
  115. sub rcs_getctime ($) { #{{{
  116. my ($file) = @_;
  117. # XXX filename passes through the shell here, should try to avoid
  118. # that just in case
  119. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  120. open (my $out, "@cmdline |");
  121. my @log = bazaar_log($out);
  122. if (length @log < 1) {
  123. return 0;
  124. }
  125. eval q{use Date::Parse};
  126. error($@) if $@;
  127. my $ctime = str2time($log[0]->{"timestamp"});
  128. return $ctime;
  129. } #}}}
  130. 1