summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin/mercurial.pm
blob: ea00a33648431daadb94efa398e47c3c224a6f89 (plain)
  1. #!/usr/bin/perl
  2. package IkiWiki::Plugin::mercurial;
  3. use warnings;
  4. use strict;
  5. use IkiWiki;
  6. use Encode;
  7. use open qw{:utf8 :std};
  8. sub import {
  9. hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
  10. hook(type => "getsetup", id => "mercurial", call => \&getsetup);
  11. hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
  12. hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
  13. hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
  14. hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
  15. hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
  16. hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
  17. hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
  18. hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
  19. hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
  20. hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
  21. }
  22. sub checkconfig () {
  23. if (exists $config{mercurial_wrapper} && length $config{mercurial_wrapper}) {
  24. push @{$config{wrappers}}, {
  25. wrapper => $config{mercurial_wrapper},
  26. wrappermode => (defined $config{mercurial_wrappermode} ? $config{mercurial_wrappermode} : "06755"),
  27. };
  28. }
  29. }
  30. sub getsetup () {
  31. return
  32. plugin => {
  33. safe => 0, # rcs plugin
  34. rebuild => undef,
  35. section => "rcs",
  36. },
  37. mercurial_wrapper => {
  38. type => "string",
  39. #example => # FIXME add example
  40. description => "mercurial post-commit hook to generate",
  41. safe => 0, # file
  42. rebuild => 0,
  43. },
  44. mercurial_wrappermode => {
  45. type => "string",
  46. example => '06755',
  47. description => "mode for mercurial_wrapper (can safely be made suid)",
  48. safe => 0,
  49. rebuild => 0,
  50. },
  51. historyurl => {
  52. type => "string",
  53. example => "http://example.com:8000/log/tip/[[file]]",
  54. description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
  55. safe => 1,
  56. rebuild => 1,
  57. },
  58. diffurl => {
  59. type => "string",
  60. example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
  61. description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
  62. safe => 1,
  63. rebuild => 1,
  64. },
  65. }
  66. sub mercurial_log ($) {
  67. my $out = shift;
  68. my @infos;
  69. while (<$out>) {
  70. my $line = $_;
  71. my ($key, $value);
  72. if (/^description:/) {
  73. $key = "description";
  74. $value = "";
  75. # slurp everything as the description text
  76. # until the next changeset
  77. while (<$out>) {
  78. if (/^changeset: /) {
  79. $line = $_;
  80. last;
  81. }
  82. $value .= $_;
  83. }
  84. local $/ = "";
  85. chomp $value;
  86. $infos[$#infos]{$key} = $value;
  87. }
  88. chomp $line;
  89. ($key, $value) = split /: +/, $line, 2;
  90. if ($key eq "changeset") {
  91. push @infos, {};
  92. # remove the revision index, which is strictly
  93. # local to the repository
  94. $value =~ s/^\d+://;
  95. }
  96. $infos[$#infos]{$key} = $value;
  97. }
  98. close $out;
  99. return @infos;
  100. }
  101. sub rcs_update () {
  102. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
  103. if (system(@cmdline) != 0) {
  104. warn "'@cmdline' failed: $!";
  105. }
  106. }
  107. sub rcs_prepedit ($) {
  108. return "";
  109. }
  110. sub rcs_commit ($$$;$$) {
  111. my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
  112. if (defined $user) {
  113. $user = IkiWiki::possibly_foolish_untaint($user);
  114. }
  115. elsif (defined $ipaddr) {
  116. $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
  117. }
  118. else {
  119. $user = "Anonymous";
  120. }
  121. $message = IkiWiki::possibly_foolish_untaint($message);
  122. if (! length $message) {
  123. $message = "no message given";
  124. }
  125. my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
  126. "-m", $message, "-u", $user);
  127. if (system(@cmdline) != 0) {
  128. warn "'@cmdline' failed: $!";
  129. }
  130. return undef; # success
  131. }
  132. sub rcs_commit_staged ($$$) {
  133. # Commits all staged changes. Changes can be staged using rcs_add,
  134. # rcs_remove, and rcs_rename.
  135. my ($message, $user, $ipaddr)=@_;
  136. error("rcs_commit_staged not implemented for mercurial"); # TODO
  137. }
  138. sub rcs_add ($) {
  139. my ($file) = @_;
  140. my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
  141. if (system(@cmdline) != 0) {
  142. warn "'@cmdline' failed: $!";
  143. }
  144. }
  145. sub rcs_remove ($) {
  146. my ($file) = @_;
  147. error("rcs_remove not implemented for mercurial"); # TODO
  148. }
  149. sub rcs_rename ($$) {
  150. my ($src, $dest) = @_;
  151. error("rcs_rename not implemented for mercurial"); # TODO
  152. }
  153. sub rcs_recentchanges ($) {
  154. my ($num) = @_;
  155. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
  156. "--style", "default");
  157. open (my $out, "@cmdline |");
  158. eval q{use Date::Parse};
  159. error($@) if $@;
  160. my @ret;
  161. foreach my $info (mercurial_log($out)) {
  162. my @pages = ();
  163. my @message = ();
  164. foreach my $msgline (split(/\n/, $info->{description})) {
  165. push @message, { line => $msgline };
  166. }
  167. foreach my $file (split / /,$info->{files}) {
  168. my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
  169. $diffurl =~ s/\[\[file\]\]/$file/go;
  170. $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
  171. push @pages, {
  172. page => pagename($file),
  173. diffurl => $diffurl,
  174. };
  175. }
  176. my $user = $info->{"user"};
  177. $user =~ s/\s*<.*>\s*$//;
  178. $user =~ s/^\s*//;
  179. push @ret, {
  180. rev => $info->{"changeset"},
  181. user => $user,
  182. committype => "hg",
  183. when => str2time($info->{"date"}),
  184. message => [@message],
  185. pages => [@pages],
  186. };
  187. }
  188. return @ret;
  189. }
  190. sub rcs_diff ($) {
  191. # TODO
  192. }
  193. sub rcs_getctime ($) {
  194. my ($file) = @_;
  195. # XXX filename passes through the shell here, should try to avoid
  196. # that just in case
  197. my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
  198. "--style", "default", "$config{srcdir}/$file");
  199. open (my $out, "@cmdline |");
  200. my @log = mercurial_log($out);
  201. if (length @log < 1) {
  202. return 0;
  203. }
  204. eval q{use Date::Parse};
  205. error($@) if $@;
  206. my $ctime = str2time($log[$#log]->{"date"});
  207. return $ctime;
  208. }
  209. 1