summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/mercurial.pm
blob: e4cc6deb376ead514c0f3f3b7be7ddb1057d6c5f (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 $ipaddr";
  59. }
  60. else {
  61. $user = "Anonymous";
  62. }
  63. $message = possibly_foolish_untaint($message);
  64. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "commit",
  65. "-m", "$message", "-u", "$user");
  66. if (system(@cmdline) != 0) {
  67. warn "'@cmdline' failed: $!";
  68. }
  69. return undef; # success
  70. } #}}}
  71. sub rcs_add ($) { # {{{
  72. my ($file) = @_;
  73. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$file");
  74. if (system(@cmdline) != 0) {
  75. warn "'@cmdline' failed: $!";
  76. }
  77. } #}}}
  78. sub rcs_recentchanges ($) { #{{{
  79. my ($num) = @_;
  80. eval q{use CGI 'escapeHTML'};
  81. error($@) if $@;
  82. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num);
  83. open (my $out, "@cmdline |");
  84. eval q{use Date::Parse};
  85. error($@) if $@;
  86. my @ret;
  87. foreach my $info (mercurial_log($out)) {
  88. my @pages = ();
  89. my @message = ();
  90. foreach my $msgline (split(/\n/, $info->{description})) {
  91. push @message, { line => $msgline };
  92. }
  93. foreach my $file (split / /,$info->{files}) {
  94. my $diffurl = $config{'diffurl'};
  95. $diffurl =~ s/\[\[file\]\]/$file/go;
  96. $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
  97. push @pages, {
  98. page => pagename($file),
  99. diffurl => $diffurl,
  100. };
  101. }
  102. my $user = $info->{"user"};
  103. $user =~ s/\s*<.*>\s*$//;
  104. $user =~ s/^\s*//;
  105. push @ret, {
  106. rev => $info->{"changeset"},
  107. user => $user,
  108. committype => "mercurial",
  109. when => time - str2time($info->{"date"}),
  110. message => [@message],
  111. pages => [@pages],
  112. };
  113. }
  114. return @ret;
  115. } #}}}
  116. sub rcs_notify () { #{{{
  117. # TODO
  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 = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1', $file);
  124. open (my $out, "@cmdline |");
  125. my @log = mercurial_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]->{"date"});
  132. return $ctime;
  133. } #}}}
  134. 1