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);
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"),
37 safe => 0, # rcs plugin
40 mercurial_wrapper => {
42 #example => # FIXME add example
43 description => "mercurial post-commit hook to generate",
47 mercurial_wrappermode => {
50 description => "mode for mercurial_wrapper (can safely be made suid)",
56 example => "http://example.com:8000/log/tip/[[file]]",
57 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
63 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
64 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
70 sub mercurial_log ($) {
78 if (/^description:/) {
82 # slurp everything as the description text
83 # until the next changeset
95 $infos[$#infos]{$key} = $value;
99 ($key, $value) = split /: +/, $line, 2;
101 if ($key eq "changeset") {
104 # remove the revision index, which is strictly
105 # local to the repository
109 $infos[$#infos]{$key} = $value;
117 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
118 if (system(@cmdline) != 0) {
119 warn "'@cmdline' failed: $!";
123 sub rcs_prepedit ($) {
127 sub rcs_commit ($$$;$$) {
128 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
131 $user = IkiWiki::possibly_foolish_untaint($user);
133 elsif (defined $ipaddr) {
134 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
140 $message = IkiWiki::possibly_foolish_untaint($message);
141 if (! length $message) {
142 $message = "no message given";
145 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
146 "-m", $message, "-u", $user);
147 if (system(@cmdline) != 0) {
148 warn "'@cmdline' failed: $!";
151 return undef; # success
154 sub rcs_commit_staged ($$$) {
155 # Commits all staged changes. Changes can be staged using rcs_add,
156 # rcs_remove, and rcs_rename.
157 my ($message, $user, $ipaddr)=@_;
159 error("rcs_commit_staged not implemented for mercurial"); # TODO
165 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
166 if (system(@cmdline) != 0) {
167 warn "'@cmdline' failed: $!";
174 error("rcs_remove not implemented for mercurial"); # TODO
177 sub rcs_rename ($$) {
178 my ($src, $dest) = @_;
180 error("rcs_rename not implemented for mercurial"); # TODO
183 sub rcs_recentchanges ($) {
186 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
187 "--style", "default");
188 open (my $out, "@cmdline |");
190 eval q{use Date::Parse};
194 foreach my $info (mercurial_log($out)) {
198 foreach my $msgline (split(/\n/, $info->{description})) {
199 push @message, { line => $msgline };
202 foreach my $file (split / /,$info->{files}) {
203 my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
204 $diffurl =~ s/\[\[file\]\]/$file/go;
205 $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
208 page => pagename($file),
213 my $user = $info->{"user"};
214 $user =~ s/\s*<.*>\s*$//;
218 rev => $info->{"changeset"},
220 committype => "mercurial",
221 when => str2time($info->{"date"}),
222 message => [@message],
234 sub rcs_getctime ($) {
237 # XXX filename passes through the shell here, should try to avoid
239 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1',
240 "--style", "default", "$config{srcdir}/$file");
241 open (my $out, "@cmdline |");
243 my @log = mercurial_log($out);
245 if (length @log < 1) {
249 eval q{use Date::Parse};
252 my $ctime = str2time($log[0]->{"date"});