summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/mercurial.pm
blob: db6a396ac4914630a41407d0df8748641c9aeef2 (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 mercurial_log($) {
  9. my $out = shift;
  10. my @infos;
  11. while (<$out>) {
  12. my $line = $_;
  13. my ($key, $value);
  14. if (/^description:/) {
  15. $key = "description";
  16. $value = "";
  17. # slurp everything as the description text
  18. # until the next changeset
  19. while (<$out>) {
  20. if (/^changeset: /) {
  21. $line = $_;
  22. last;
  23. }
  24. $value .= $_;
  25. }
  26. local $/ = "";
  27. chomp $value;
  28. $infos[$#infos]{$key} = $value;
  29. }
  30. chomp $line;
  31. ($key, $value) = split /: +/, $line, 2;
  32. if ($key eq "changeset") {
  33. push @infos, {};
  34. # remove the revision index, which is strictly
  35. # local to the repository
  36. $value =~ s/^\d+://;
  37. }
  38. $infos[$#infos]{$key} = $value;
  39. }
  40. close $out;
  41. return @infos;
  42. }
  43. sub rcs_update () { #{{{
  44. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
  45. if (system(@cmdline) != 0) {
  46. warn "'@cmdline' failed: $!";
  47. }
  48. } #}}}
  49. sub rcs_prepedit ($) { #{{{
  50. return "";
  51. } #}}}
  52. sub rcs_commit ($$$;$$) { #{{{
  53. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  54. if (defined $user) {
  55. $user = possibly_foolish_untaint($user);
  56. }
  57. elsif (defined $ipaddr) {
  58. $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
  59. }
  60. else {
  61. $user = "Anonymous";
  62. }
  63. $message = possibly_foolish_untaint($message);
  64. if (! length $message) {
  65. $message = "no message given";
  66. }
  67. my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
  68. "-m", $message, "-u", $user);
  69. if (system(@cmdline) != 0) {
  70. warn "'@cmdline' failed: $!";
  71. }
  72. return undef; # success
  73. } #}}}
  74. sub rcs_add ($) { # {{{
  75. my ($file) = @_;
  76. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
  77. if (system(@cmdline) != 0) {
  78. warn "'@cmdline' failed: $!";
  79. }
  80. } #}}}
  81. sub rcs_recentchanges ($) { #{{{
  82. my ($num) = @_;
  83. eval q{use CGI 'escapeHTML'};
  84. error($@) if $@;
  85. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
  86. "--style", "default");
  87. open (my $out, "@cmdline |");
  88. eval q{use Date::Parse};
  89. error($@) if $@;
  90. my @ret;
  91. foreach my $info (mercurial_log($out)) {
  92. my @pages = ();
  93. my @message = ();
  94. foreach my $msgline (split(/\n/, $info->{description})) {
  95. push @message, { line => $msgline };
  96. }
  97. foreach my $file (split / /,$info->{files}) {
  98. my $diffurl = $config{'diffurl'};
  99. $diffurl =~ s/\[\[file\]\]/$file/go;
  100. $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
  101. push @pages, {
  102. page => pagename($file),
  103. diffurl => $diffurl,
  104. };
  105. }
  106. my $user = $info->{"user"};
  107. $user =~ s/\s*<.*>\s*$//;
  108. $user =~ s/^\s*//;
  109. push @ret, {
  110. rev => $info->{"changeset"},
  111. user => $user,
  112. committype => "mercurial",
  113. when => str2time($info->{"date"}),
  114. message => [@message],
  115. pages => [@pages],
  116. };
  117. }
  118. return @ret;
  119. } #}}}
  120. sub rcs_getctime ($) { #{{{
  121. my ($file) = @_;
  122. # XXX filename passes through the shell here, should try to avoid
  123. # that just in case
  124. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1',
  125. "--style", "default", "$config{srcdir}/$file");
  126. open (my $out, "@cmdline |");
  127. my @log = mercurial_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]->{"date"});
  134. return $ctime;
  135. } #}}}
  136. 1