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