2 package IkiWiki::Plugin::mercurial;
8 use open qw{:utf8 :std};
11 if (exists $IkiWiki::hooks{rcs}) {
12 error(gettext("cannot use multiple rcs plugins"));
14 hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
15 hook(type => "getsetup", id => "mercurial", call => \&getsetup);
16 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
17 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
18 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
19 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
20 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
21 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
22 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
23 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
24 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
25 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
28 sub checkconfig () { #{{{
29 if (exists $config{mercurial_wrapper} && length $config{mercurial_wrapper}) {
30 push @{$config{wrappers}}, {
31 wrapper => $config{mercurial_wrapper},
32 wrappermode => (defined $config{mercurial_wrappermode} ? $config{mercurial_wrappermode} : "06755"),
37 sub getsetup () { #{{{
39 mercurial_wrapper => {
41 #example => # FIXME add example
42 description => "mercurial post-commit executable to generate",
46 mercurial_wrappermode => {
49 description => "mode for mercurial_wrapper (can safely be made suid)",
55 example => "http://example.com:8000/log/tip/[[file]]",
56 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
62 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
63 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
69 sub mercurial_log ($) { #{{{
77 if (/^description:/) {
81 # slurp everything as the description text
82 # until the next changeset
94 $infos[$#infos]{$key} = $value;
98 ($key, $value) = split /: +/, $line, 2;
100 if ($key eq "changeset") {
103 # remove the revision index, which is strictly
104 # local to the repository
108 $infos[$#infos]{$key} = $value;
115 sub rcs_update () { #{{{
116 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
117 if (system(@cmdline) != 0) {
118 warn "'@cmdline' failed: $!";
122 sub rcs_prepedit ($) { #{{{
126 sub rcs_commit ($$$;$$) { #{{{
127 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
130 $user = IkiWiki::possibly_foolish_untaint($user);
132 elsif (defined $ipaddr) {
133 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
139 $message = IkiWiki::possibly_foolish_untaint($message);
140 if (! length $message) {
141 $message = "no message given";
144 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
145 "-m", $message, "-u", $user);
146 if (system(@cmdline) != 0) {
147 warn "'@cmdline' failed: $!";
150 return undef; # success
153 sub rcs_commit_staged ($$$) {
154 # Commits all staged changes. Changes can be staged using rcs_add,
155 # rcs_remove, and rcs_rename.
156 my ($message, $user, $ipaddr)=@_;
158 error("rcs_commit_staged not implemented for mercurial"); # TODO
161 sub rcs_add ($) { # {{{
164 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
165 if (system(@cmdline) != 0) {
166 warn "'@cmdline' failed: $!";
170 sub rcs_remove ($) { # {{{
173 error("rcs_remove not implemented for mercurial"); # TODO
176 sub rcs_rename ($$) { # {{{
177 my ($src, $dest) = @_;
179 error("rcs_rename not implemented for mercurial"); # TODO
182 sub rcs_recentchanges ($) { #{{{
185 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
186 "--style", "default");
187 open (my $out, "@cmdline |");
189 eval q{use Date::Parse};
193 foreach my $info (mercurial_log($out)) {
197 foreach my $msgline (split(/\n/, $info->{description})) {
198 push @message, { line => $msgline };
201 foreach my $file (split / /,$info->{files}) {
202 my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
203 $diffurl =~ s/\[\[file\]\]/$file/go;
204 $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
207 page => pagename($file),
212 my $user = $info->{"user"};
213 $user =~ s/\s*<.*>\s*$//;
217 rev => $info->{"changeset"},
219 committype => "mercurial",
220 when => str2time($info->{"date"}),
221 message => [@message],
229 sub rcs_diff ($) { #{{{
233 sub rcs_getctime ($) { #{{{
236 # XXX filename passes through the shell here, should try to avoid
238 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1',
239 "--style", "default", "$config{srcdir}/$file");
240 open (my $out, "@cmdline |");
242 my @log = mercurial_log($out);
244 if (length @log < 1) {
248 eval q{use Date::Parse};
251 my $ctime = str2time($log[0]->{"date"});