summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/bzr.pm
blob: 43822fe8ffad4cc7fe2f73689d0d97b3c8f757fa (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", "--limit", $num, $config{srcdir});
  82. open (my $out, "@cmdline |");
  83. eval q{use Date::Parse};
  84. error($@) if $@;
  85. my @ret;
  86. foreach my $info (bzr_log($out)) {
  87. my @pages = ();
  88. my @message = ();
  89. foreach my $msgline (split(/\n/, $info->{message})) {
  90. push @message, { line => $msgline };
  91. }
  92. foreach my $file (split(/\n/, $info->{files})) {
  93. my $diffurl = $config{'diffurl'};
  94. $diffurl =~ s/\[\[file\]\]/$file/go;
  95. $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
  96. push @pages, {
  97. page => pagename($file),
  98. diffurl => $diffurl,
  99. };
  100. }
  101. my $user = $info->{"committer"};
  102. if (defined($info->{"author"})) { $user = $info->{"author"}; }
  103. $user =~ s/\s*<.*>\s*$//;
  104. $user =~ s/^\s*//;
  105. push @ret, {
  106. rev => $info->{"revno"},
  107. user => $user,
  108. committype => "bzr",
  109. when => time - str2time($info->{"timestamp"}),
  110. message => [@message],
  111. pages => [@pages],
  112. };
  113. }
  114. return @ret;
  115. } #}}}
  116. sub rcs_getctime ($) { #{{{
  117. my ($file) = @_;
  118. # XXX filename passes through the shell here, should try to avoid
  119. # that just in case
  120. my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
  121. open (my $out, "@cmdline |");
  122. my @log = bzr_log($out);
  123. if (length @log < 1) {
  124. return 0;
  125. }
  126. eval q{use Date::Parse};
  127. error($@) if $@;
  128. my $ctime = str2time($log[0]->{"timestamp"});
  129. return $ctime;
  130. } #}}}
  131. 1