]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/mercurial.pm
po: fix interdiction to create pages of type po
[ikiwiki.git] / 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 }
24
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"),
30                 };
31         }
32 }
33
34 sub getsetup () {
35         return
36                 plugin => {
37                         safe => 0, # rcs plugin
38                         rebuild => undef,
39                 },
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,
46                 },
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,
53                 },
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,
60                 },
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,
67                 },
68 }
69
70 sub mercurial_log ($) {
71         my $out = shift;
72         my @infos;
73
74         while (<$out>) {
75                 my $line = $_;
76                 my ($key, $value);
77
78                 if (/^description:/) {
79                         $key = "description";
80                         $value = "";
81
82                         # slurp everything as the description text 
83                         # until the next changeset
84                         while (<$out>) {
85                                 if (/^changeset: /) {
86                                         $line = $_;
87                                         last;
88                                 }
89
90                                 $value .= $_;
91                         }
92
93                         local $/ = "";
94                         chomp $value;
95                         $infos[$#infos]{$key} = $value;
96                 }
97
98                 chomp $line;
99                 ($key, $value) = split /: +/, $line, 2;
100
101                 if ($key eq "changeset") {
102                         push @infos, {};
103
104                         # remove the revision index, which is strictly 
105                         # local to the repository
106                         $value =~ s/^\d+://;
107                 }
108
109                 $infos[$#infos]{$key} = $value;
110         }
111         close $out;
112
113         return @infos;
114 }
115
116 sub rcs_update () {
117         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
118         if (system(@cmdline) != 0) {
119                 warn "'@cmdline' failed: $!";
120         }
121 }
122
123 sub rcs_prepedit ($) {
124         return "";
125 }
126
127 sub rcs_commit ($$$;$$) {
128         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
129
130         if (defined $user) {
131                 $user = IkiWiki::possibly_foolish_untaint($user);
132         }
133         elsif (defined $ipaddr) {
134                 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
135         }
136         else {
137                 $user = "Anonymous";
138         }
139
140         $message = IkiWiki::possibly_foolish_untaint($message);
141         if (! length $message) {
142                 $message = "no message given";
143         }
144
145         my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit", 
146                        "-m", $message, "-u", $user);
147         if (system(@cmdline) != 0) {
148                 warn "'@cmdline' failed: $!";
149         }
150
151         return undef; # success
152 }
153
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)=@_;
158         
159         error("rcs_commit_staged not implemented for mercurial"); # TODO
160 }
161
162 sub rcs_add ($) {
163         my ($file) = @_;
164
165         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
166         if (system(@cmdline) != 0) {
167                 warn "'@cmdline' failed: $!";
168         }
169 }
170
171 sub rcs_remove ($) {
172         my ($file) = @_;
173
174         error("rcs_remove not implemented for mercurial"); # TODO
175 }
176
177 sub rcs_rename ($$) {
178         my ($src, $dest) = @_;
179
180         error("rcs_rename not implemented for mercurial"); # TODO
181 }
182
183 sub rcs_recentchanges ($) {
184         my ($num) = @_;
185
186         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
187                 "--style", "default");
188         open (my $out, "@cmdline |");
189
190         eval q{use Date::Parse};
191         error($@) if $@;
192
193         my @ret;
194         foreach my $info (mercurial_log($out)) {
195                 my @pages = ();
196                 my @message = ();
197         
198                 foreach my $msgline (split(/\n/, $info->{description})) {
199                         push @message, { line => $msgline };
200                 }
201
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;
206
207                         push @pages, {
208                                 page => pagename($file),
209                                 diffurl => $diffurl,
210                         };
211                 }
212
213                 my $user = $info->{"user"};
214                 $user =~ s/\s*<.*>\s*$//;
215                 $user =~ s/^\s*//;
216
217                 push @ret, {
218                         rev        => $info->{"changeset"},
219                         user       => $user,
220                         committype => "hg",
221                         when       => str2time($info->{"date"}),
222                         message    => [@message],
223                         pages      => [@pages],
224                 };
225         }
226
227         return @ret;
228 }
229
230 sub rcs_diff ($) {
231         # TODO
232 }
233
234 sub rcs_getctime ($) {
235         my ($file) = @_;
236
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 |");
242
243         my @log = mercurial_log($out);
244
245         if (length @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 1