summaryrefslogtreecommitdiff
path: root/IkiWiki/Rcs/mercurial.pm
blob: bfe6ba49c13edc088b270faee364d94a2dbd7903 (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 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. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
  84. "--style", "default");
  85. open (my $out, "@cmdline |");
  86. eval q{use Date::Parse};
  87. error($@) if $@;
  88. my @ret;
  89. foreach my $info (mercurial_log($out)) {
  90. my @pages = ();
  91. my @message = ();
  92. foreach my $msgline (split(/\n/, $info->{description})) {
  93. push @message, { line => $msgline };
  94. }
  95. foreach my $file (split / /,$info->{files}) {
  96. my $diffurl = $config{'diffurl'};
  97. $diffurl =~ s/\[\[file\]\]/$file/go;
  98. $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
  99. push @pages, {
  100. page => pagename($file),
  101. diffurl => $diffurl,
  102. };
  103. }
  104. my $user = $info->{"user"};
  105. $user =~ s/\s*<.*>\s*$//;
  106. $user =~ s/^\s*//;
  107. push @ret, {
  108. rev => $info->{"changeset"},
  109. user => $user,
  110. committype => "mercurial",
  111. when => str2time($info->{"date"}),
  112. message => [@message],
  113. pages => [@pages],
  114. };
  115. }
  116. return @ret;
  117. } #}}}
  118. sub rcs_diff ($) { #{{{
  119. # TODO
  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 = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1',
  126. "--style", "default", "$config{srcdir}/$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