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