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);
25 sub checkconfig () { #{{{
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"),
34 sub getsetup () { #{{{
36 mercurial_wrapper => {
38 #example => # FIXME add example
39 description => "mercurial post-commit executable to generate",
43 mercurial_wrappermode => {
46 description => "mode for mercurial_wrapper (can safely be made suid)",
52 example => "http://example.com:8000/log/tip/[[file]]",
53 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
59 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
60 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
66 sub mercurial_log ($) { #{{{
74 if (/^description:/) {
78 # slurp everything as the description text
79 # until the next changeset
91 $infos[$#infos]{$key} = $value;
95 ($key, $value) = split /: +/, $line, 2;
97 if ($key eq "changeset") {
100 # remove the revision index, which is strictly
101 # local to the repository
105 $infos[$#infos]{$key} = $value;
112 sub rcs_update () { #{{{
113 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
114 if (system(@cmdline) != 0) {
115 warn "'@cmdline' failed: $!";
119 sub rcs_prepedit ($) { #{{{
123 sub rcs_commit ($$$;$$) { #{{{
124 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
127 $user = IkiWiki::possibly_foolish_untaint($user);
129 elsif (defined $ipaddr) {
130 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
136 $message = IkiWiki::possibly_foolish_untaint($message);
137 if (! length $message) {
138 $message = "no message given";
141 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
142 "-m", $message, "-u", $user);
143 if (system(@cmdline) != 0) {
144 warn "'@cmdline' failed: $!";
147 return undef; # success
150 sub rcs_commit_staged ($$$) {
151 # Commits all staged changes. Changes can be staged using rcs_add,
152 # rcs_remove, and rcs_rename.
153 my ($message, $user, $ipaddr)=@_;
155 error("rcs_commit_staged not implemented for mercurial"); # TODO
158 sub rcs_add ($) { # {{{
161 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
162 if (system(@cmdline) != 0) {
163 warn "'@cmdline' failed: $!";
167 sub rcs_remove ($) { # {{{
170 error("rcs_remove not implemented for mercurial"); # TODO
173 sub rcs_rename ($$) { # {{{
174 my ($src, $dest) = @_;
176 error("rcs_rename not implemented for mercurial"); # TODO
179 sub rcs_recentchanges ($) { #{{{
182 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
183 "--style", "default");
184 open (my $out, "@cmdline |");
186 eval q{use Date::Parse};
190 foreach my $info (mercurial_log($out)) {
194 foreach my $msgline (split(/\n/, $info->{description})) {
195 push @message, { line => $msgline };
198 foreach my $file (split / /,$info->{files}) {
199 my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
200 $diffurl =~ s/\[\[file\]\]/$file/go;
201 $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
204 page => pagename($file),
209 my $user = $info->{"user"};
210 $user =~ s/\s*<.*>\s*$//;
214 rev => $info->{"changeset"},
216 committype => "mercurial",
217 when => str2time($info->{"date"}),
218 message => [@message],
226 sub rcs_diff ($) { #{{{
230 sub rcs_getctime ($) { #{{{
233 # XXX filename passes through the shell here, should try to avoid
235 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1',
236 "--style", "default", "$config{srcdir}/$file");
237 open (my $out, "@cmdline |");
239 my @log = mercurial_log($out);
241 if (length @log < 1) {
245 eval q{use Date::Parse};
248 my $ctime = str2time($log[0]->{"date"});