monotone.pm: while we're still only checking the major and minor
[ikiwiki] / IkiWiki / Plugin / mercurial.pm
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::mercurial;
3
4 use warnings;
5 use strict;
6 use IkiWiki;
7 use Encode;
8 use open qw{:utf8 :std};
9
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);
24 }
25
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"),
31                 };
32         }
33 }
34
35 sub getsetup () {
36         return
37                 plugin => {
38                         safe => 0, # rcs plugin
39                         rebuild => undef,
40                         section => "rcs",
41                 },
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,
48                 },
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,
55                 },
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,
62                 },
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,
69                 },
70 }
71
72 sub mercurial_log ($) {
73         my $out = shift;
74         my @infos;
75
76         while (<$out>) {
77                 my $line = $_;
78                 my ($key, $value);
79
80                 if (/^description:/) {
81                         $key = "description";
82                         $value = "";
83
84                         # slurp everything as the description text 
85                         # until the next changeset
86                         while (<$out>) {
87                                 if (/^changeset: /) {
88                                         $line = $_;
89                                         last;
90                                 }
91
92                                 $value .= $_;
93                         }
94
95                         local $/ = "";
96                         chomp $value;
97                         $infos[$#infos]{$key} = $value;
98                 }
99
100                 chomp $line;
101                 ($key, $value) = split /: +/, $line, 2;
102
103                 if ($key eq "changeset") {
104                         push @infos, {};
105
106                         # remove the revision index, which is strictly 
107                         # local to the repository
108                         $value =~ s/^\d+://;
109                 }
110
111                 $infos[$#infos]{$key} = $value;
112         }
113         close $out;
114
115         return @infos;
116 }
117
118 sub rcs_update () {
119         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
120         if (system(@cmdline) != 0) {
121                 warn "'@cmdline' failed: $!";
122         }
123 }
124
125 sub rcs_prepedit ($) {
126         return "";
127 }
128
129 sub rcs_commit (@) {
130         my %params=@_;
131
132         my $user="Anonymous";
133         if (defined $params{session}) {
134                 if (defined $params{session}->param("name")) {
135                         $user = $params{session}->param("name");
136                 }
137                 elsif (defined $params{session}->remote_addr()) {
138                         $user = "Anonymous from ".$params{session}->remote_addr();
139                 }
140         }
141
142         if (! length $params{message}) {
143                 $params{message} = "no message given";
144         }
145
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: $!";
151         }
152
153         return undef; # success
154 }
155
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=@_;
160         
161         error("rcs_commit_staged not implemented for mercurial"); # TODO
162 }
163
164 sub rcs_add ($) {
165         my ($file) = @_;
166
167         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
168         if (system(@cmdline) != 0) {
169                 warn "'@cmdline' failed: $!";
170         }
171 }
172
173 sub rcs_remove ($) {
174         my ($file) = @_;
175
176         error("rcs_remove not implemented for mercurial"); # TODO
177 }
178
179 sub rcs_rename ($$) {
180         my ($src, $dest) = @_;
181
182         error("rcs_rename not implemented for mercurial"); # TODO
183 }
184
185 sub rcs_recentchanges ($) {
186         my ($num) = @_;
187
188         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
189                 "--style", "default");
190         open (my $out, "@cmdline |");
191
192         eval q{use Date::Parse};
193         error($@) if $@;
194
195         my @ret;
196         foreach my $info (mercurial_log($out)) {
197                 my @pages = ();
198                 my @message = ();
199         
200                 foreach my $msgline (split(/\n/, $info->{description})) {
201                         push @message, { line => $msgline };
202                 }
203
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;
208
209                         push @pages, {
210                                 page => pagename($file),
211                                 diffurl => $diffurl,
212                         };
213                 }
214
215                 my $user = $info->{"user"};
216                 $user =~ s/\s*<.*>\s*$//;
217                 $user =~ s/^\s*//;
218
219                 push @ret, {
220                         rev        => $info->{"changeset"},
221                         user       => $user,
222                         committype => "hg",
223                         when       => str2time($info->{"date"}),
224                         message    => [@message],
225                         pages      => [@pages],
226                 };
227         }
228
229         return @ret;
230 }
231
232 sub rcs_diff ($) {
233         # TODO
234 }
235
236 sub rcs_getctime ($) {
237         my ($file) = @_;
238
239         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
240                 "--style", "default", "$config{srcdir}/$file");
241         open (my $out, "-|", @cmdline);
242
243         my @log = (mercurial_log($out));
244
245         if (@log < 1) {
246                 return 0;
247         }
248
249         eval q{use Date::Parse};
250         error($@) if $@;
251         
252         my $ctime = str2time($log[$#log]->{"date"});
253         return $ctime;
254 }
255
256 sub rcs_getmtime ($) {
257         error "rcs_getmtime is not implemented for mercurial\n"; # TODO
258 }
259
260 1