clear sandbox/test commit
[ikiwiki.git] / IkiWiki / Plugin / mercurial.pm
blobd7399eaf0be208730651cf55e9ba01b29979aede
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::mercurial;
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use Encode;
8 use open qw{:utf8 :std};
10 sub import {
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);
26 sub checkconfig () {
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"),
35 sub getsetup () {
36 return
37 plugin => {
38 safe => 0, # rcs plugin
39 rebuild => undef,
40 section => "rcs",
42 mercurial_wrapper => {
43 type => "string",
44 #example => # FIXME add example
45 description => "mercurial post-commit hook to generate",
46 safe => 0, # file
47 rebuild => 0,
49 mercurial_wrappermode => {
50 type => "string",
51 example => '06755',
52 description => "mode for mercurial_wrapper (can safely be made suid)",
53 safe => 0,
54 rebuild => 0,
56 historyurl => {
57 type => "string",
58 example => "http://example.com:8000/log/tip/[[file]]",
59 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
60 safe => 1,
61 rebuild => 1,
63 diffurl => {
64 type => "string",
65 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
66 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
67 safe => 1,
68 rebuild => 1,
72 sub mercurial_log ($) {
73 my $out = shift;
74 my @infos;
76 while (<$out>) {
77 my $line = $_;
78 my ($key, $value);
80 if (/^description:/) {
81 $key = "description";
82 $value = "";
84 # slurp everything as the description text
85 # until the next changeset
86 while (<$out>) {
87 if (/^changeset: /) {
88 $line = $_;
89 last;
92 $value .= $_;
95 local $/ = "";
96 chomp $value;
97 $infos[$#infos]{$key} = $value;
100 chomp $line;
101 ($key, $value) = split /: +/, $line, 2;
103 if ($key eq "changeset") {
104 push @infos, {};
106 # remove the revision index, which is strictly
107 # local to the repository
108 $value =~ s/^\d+://;
111 $infos[$#infos]{$key} = $value;
113 close $out;
115 return @infos;
118 sub rcs_update () {
119 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
120 if (system(@cmdline) != 0) {
121 warn "'@cmdline' failed: $!";
125 sub rcs_prepedit ($) {
126 return "";
129 sub rcs_commit (@) {
130 my %params=@_;
132 my $user="Anonymous";
133 if (defined $params{session}) {
134 if (defined $params{session}->param("name")) {
135 $user = $params{session}->param("name");
137 elsif (defined $params{session}->remote_addr()) {
138 $user = "Anonymous from ".$params{session}->remote_addr();
142 if (! length $params{message}) {
143 $params{message} = "no message given";
146 my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
147 "-m", IkiWiki::possibly_foolish_untaint($params{message}),
148 "-u", IkiWiki::possibly_foolish_untaint($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 %params=@_;
161 error("rcs_commit_staged not implemented for mercurial"); # TODO
164 sub rcs_add ($) {
165 my ($file) = @_;
167 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
168 if (system(@cmdline) != 0) {
169 warn "'@cmdline' failed: $!";
173 sub rcs_remove ($) {
174 my ($file) = @_;
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 ($) {
186 my ($num) = @_;
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};
193 error($@) if $@;
195 my @ret;
196 foreach my $info (mercurial_log($out)) {
197 my @pages = ();
198 my @message = ();
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;
209 push @pages, {
210 page => pagename($file),
211 diffurl => $diffurl,
215 my $user = $info->{"user"};
216 $user =~ s/\s*<.*>\s*$//;
217 $user =~ s/^\s*//;
219 push @ret, {
220 rev => $info->{"changeset"},
221 user => $user,
222 committype => "hg",
223 when => str2time($info->{"date"}),
224 message => [@message],
225 pages => [@pages],
229 return @ret;
232 sub rcs_diff ($;$) {
233 # TODO
236 sub rcs_getctime ($) {
237 my ($file) = @_;
239 my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
240 "--style", "default", "$config{srcdir}/$file");
241 open (my $out, "-|", @cmdline);
243 my @log = (mercurial_log($out));
245 if (@log < 1) {
246 return 0;
249 eval q{use Date::Parse};
250 error($@) if $@;
252 my $ctime = str2time($log[$#log]->{"date"});
253 return $ctime;
256 sub rcs_getmtime ($) {
257 error "rcs_getmtime is not implemented for mercurial\n"; # TODO