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
41 mercurial_wrapper => {
43 #example => # FIXME add example
44 description => "mercurial post-commit hook to generate",
48 mercurial_wrappermode => {
51 description => "mode for mercurial_wrapper (can safely be made suid)",
57 example => "http://example.com:8000/log/tip/[[file]]",
58 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
64 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
65 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
71 sub mercurial_log ($) {
79 if (/^description:/) {
83 # slurp everything as the description text
84 # until the next changeset
96 $infos[$#infos]{$key} = $value;
100 ($key, $value) = split /: +/, $line, 2;
102 if ($key eq "changeset") {
105 # remove the revision index, which is strictly
106 # local to the repository
110 $infos[$#infos]{$key} = $value;
118 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
119 if (system(@cmdline) != 0) {
120 warn "'@cmdline' failed: $!";
124 sub rcs_prepedit ($) {
128 sub rcs_commit ($$$;$$) {
129 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
132 $user = IkiWiki::possibly_foolish_untaint($user);
134 elsif (defined $ipaddr) {
135 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
141 $message = IkiWiki::possibly_foolish_untaint($message);
142 if (! length $message) {
143 $message = "no message given";
146 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
147 "-m", $message, "-u", $user);
148 if (system(@cmdline) != 0) {
149 warn "'@cmdline' failed: $!";
152 return undef; # success
155 sub rcs_commit_staged ($$$) {
156 # Commits all staged changes. Changes can be staged using rcs_add,
157 # rcs_remove, and rcs_rename.
158 my ($message, $user, $ipaddr)=@_;
160 error("rcs_commit_staged not implemented for mercurial"); # TODO
166 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
167 if (system(@cmdline) != 0) {
168 warn "'@cmdline' failed: $!";
175 error("rcs_remove not implemented for mercurial"); # TODO
178 sub rcs_rename ($$) {
179 my ($src, $dest) = @_;
181 error("rcs_rename not implemented for mercurial"); # TODO
184 sub rcs_recentchanges ($) {
187 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
188 "--style", "default");
189 open (my $out, "@cmdline |");
191 eval q{use Date::Parse};
195 foreach my $info (mercurial_log($out)) {
199 foreach my $msgline (split(/\n/, $info->{description})) {
200 push @message, { line => $msgline };
203 foreach my $file (split / /,$info->{files}) {
204 my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
205 $diffurl =~ s/\[\[file\]\]/$file/go;
206 $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
209 page => pagename($file),
214 my $user = $info->{"user"};
215 $user =~ s/\s*<.*>\s*$//;
219 rev => $info->{"changeset"},
222 when => str2time($info->{"date"}),
223 message => [@message],
235 sub rcs_getctime ($) {
238 # XXX filename passes through the shell here, should try to avoid
240 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
241 "--style", "default", "$config{srcdir}/$file");
242 open (my $out, "@cmdline |");
244 my @log = mercurial_log($out);
246 if (length @log < 1) {
250 eval q{use Date::Parse};
253 my $ctime = str2time($log[$#log]->{"date"});