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 ($) {
129 sub rcs_commit ($$$;$$) {
130 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
133 $user = IkiWiki::possibly_foolish_untaint($user);
135 elsif (defined $ipaddr) {
136 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
142 $message = IkiWiki::possibly_foolish_untaint($message);
143 if (! length $message) {
144 $message = "no message given";
147 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
148 "-m", $message, "-u", $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.
159 my ($message, $user, $ipaddr)=@_;
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 # XXX filename passes through the shell here, should try to avoid
241 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
242 "--style", "default", "$config{srcdir}/$file");
243 open (my $out, "@cmdline |");
245 my @log = mercurial_log($out);
247 if (length @log < 1) {
251 eval q{use Date::Parse};
254 my $ctime = str2time($log[$#log]->{"date"});
258 sub rcs_getmtime ($) {
259 error "rcs_getmtime is not implemented for mercurial\n"; # TODO