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