2 package IkiWiki::Plugin::mercurial;
8 use open qw{:utf8 :std};
11 hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
12 hook(type => "getsetup", id => "mercurial", call => \&getsetup);
13 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
14 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
15 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
16 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
17 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
18 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
19 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
20 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
21 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
22 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
23 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
27 if (exists $config{mercurial_wrapper} && length $config{mercurial_wrapper}) {
28 push @{$config{wrappers}}, {
29 wrapper => $config{mercurial_wrapper},
30 wrappermode => (defined $config{mercurial_wrappermode} ? $config{mercurial_wrappermode} : "06755"),
38 safe => 0, # rcs plugin
42 mercurial_wrapper => {
44 #example => # FIXME add example
45 description => "mercurial post-commit hook to generate",
49 mercurial_wrappermode => {
52 description => "mode for mercurial_wrapper (can safely be made suid)",
58 example => "http://example.com:8000/log/tip/[[file]]",
59 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
65 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
66 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
72 sub mercurial_log ($) {
80 if (/^description:/) {
84 # slurp everything as the description text
85 # until the next changeset
97 $infos[$#infos]{$key} = $value;
101 ($key, $value) = split /: +/, $line, 2;
103 if ($key eq "changeset") {
106 # remove the revision index, which is strictly
107 # local to the repository
111 $infos[$#infos]{$key} = $value;
119 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
120 if (system(@cmdline) != 0) {
121 warn "'@cmdline' failed: $!";
125 sub rcs_prepedit ($) {
132 my $user="Anonymous";
133 if (defined $params{session}) {
134 if (defined $params{session}->param("name")) {
135 $user = $params{session}->param("name");
137 elsif (defined $params{session}->remote_addr()) {
138 $user = "Anonymous from ".$params{session}->remote_addr();
142 if (! length $params{message}) {
143 $params{message} = "no message given";
146 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
147 "-m", IkiWiki::possibly_foolish_untaint($params{message}),
148 "-u", IkiWiki::possibly_foolish_untaint($user));
149 if (system(@cmdline) != 0) {
150 warn "'@cmdline' failed: $!";
153 return undef; # success
156 sub rcs_commit_staged (@) {
157 # Commits all staged changes. Changes can be staged using rcs_add,
158 # rcs_remove, and rcs_rename.
161 error("rcs_commit_staged not implemented for mercurial"); # TODO
167 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
168 if (system(@cmdline) != 0) {
169 warn "'@cmdline' failed: $!";
176 error("rcs_remove not implemented for mercurial"); # TODO
179 sub rcs_rename ($$) {
180 my ($src, $dest) = @_;
182 error("rcs_rename not implemented for mercurial"); # TODO
185 sub rcs_recentchanges ($) {
188 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
189 "--style", "default");
190 open (my $out, "@cmdline |");
192 eval q{use Date::Parse};
196 foreach my $info (mercurial_log($out)) {
200 foreach my $msgline (split(/\n/, $info->{description})) {
201 push @message, { line => $msgline };
204 foreach my $file (split / /,$info->{files}) {
205 my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
206 $diffurl =~ s/\[\[file\]\]/$file/go;
207 $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
210 page => pagename($file),
215 my $user = $info->{"user"};
216 $user =~ s/\s*<.*>\s*$//;
220 rev => $info->{"changeset"},
223 when => str2time($info->{"date"}),
224 message => [@message],
236 sub rcs_getctime ($) {
239 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
240 "--style", "default", "$config{srcdir}/$file");
241 open (my $out, "-|", @cmdline);
243 my @log = (mercurial_log($out));
249 eval q{use Date::Parse};
252 my $ctime = str2time($log[$#log]->{"date"});
256 sub rcs_getmtime ($) {
257 error "rcs_getmtime is not implemented for mercurial\n"; # TODO