tidy up new page_types code
[ikiwiki.git] / IkiWiki / Plugin / mercurial.pm
blob11fdec529373c6b430a8e77bebb2c0a3aa6972b7
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);
25 sub checkconfig () {
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"),
34 sub getsetup () {
35 return
36 plugin => {
37 safe => 0, # rcs plugin
38 rebuild => undef,
40 mercurial_wrapper => {
41 type => "string",
42 #example => # FIXME add example
43 description => "mercurial post-commit hook to generate",
44 safe => 0, # file
45 rebuild => 0,
47 mercurial_wrappermode => {
48 type => "string",
49 example => '06755',
50 description => "mode for mercurial_wrapper (can safely be made suid)",
51 safe => 0,
52 rebuild => 0,
54 historyurl => {
55 type => "string",
56 example => "http://example.com:8000/log/tip/[[file]]",
57 description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
58 safe => 1,
59 rebuild => 1,
61 diffurl => {
62 type => "string",
63 example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
64 description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
65 safe => 1,
66 rebuild => 1,
70 sub mercurial_log ($) {
71 my $out = shift;
72 my @infos;
74 while (<$out>) {
75 my $line = $_;
76 my ($key, $value);
78 if (/^description:/) {
79 $key = "description";
80 $value = "";
82 # slurp everything as the description text
83 # until the next changeset
84 while (<$out>) {
85 if (/^changeset: /) {
86 $line = $_;
87 last;
90 $value .= $_;
93 local $/ = "";
94 chomp $value;
95 $infos[$#infos]{$key} = $value;
98 chomp $line;
99 ($key, $value) = split /: +/, $line, 2;
101 if ($key eq "changeset") {
102 push @infos, {};
104 # remove the revision index, which is strictly
105 # local to the repository
106 $value =~ s/^\d+://;
109 $infos[$#infos]{$key} = $value;
111 close $out;
113 return @infos;
116 sub rcs_update () {
117 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
118 if (system(@cmdline) != 0) {
119 warn "'@cmdline' failed: $!";
123 sub rcs_prepedit ($) {
124 return "";
127 sub rcs_commit ($$$;$$) {
128 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
130 if (defined $user) {
131 $user = IkiWiki::possibly_foolish_untaint($user);
133 elsif (defined $ipaddr) {
134 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
136 else {
137 $user = "Anonymous";
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
162 sub rcs_add ($) {
163 my ($file) = @_;
165 my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
166 if (system(@cmdline) != 0) {
167 warn "'@cmdline' failed: $!";
171 sub rcs_remove ($) {
172 my ($file) = @_;
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 ($) {
184 my ($num) = @_;
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};
191 error($@) if $@;
193 my @ret;
194 foreach my $info (mercurial_log($out)) {
195 my @pages = ();
196 my @message = ();
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;
207 push @pages, {
208 page => pagename($file),
209 diffurl => $diffurl,
213 my $user = $info->{"user"};
214 $user =~ s/\s*<.*>\s*$//;
215 $user =~ s/^\s*//;
217 push @ret, {
218 rev => $info->{"changeset"},
219 user => $user,
220 committype => "hg",
221 when => str2time($info->{"date"}),
222 message => [@message],
223 pages => [@pages],
227 return @ret;
230 sub rcs_diff ($) {
231 # TODO
234 sub rcs_getctime ($) {
235 my ($file) = @_;
237 # XXX filename passes through the shell here, should try to avoid
238 # that just in case
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 (length @log < 1) {
246 return 0;
249 eval q{use Date::Parse};
250 error($@) if $@;
252 my $ctime = str2time($log[$#log]->{"date"});
253 return $ctime;