]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Plugin/mercurial.pm
move generic comment into IkiWiki::Setup
[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                         section => "rcs",
40                 },
41                 mercurial_wrapper => {
42                         type => "string",
43                         #example => # FIXME add example
44                         description => "mercurial post-commit hook to generate",
45                         safe => 0, # file
46                         rebuild => 0,
47                 },
48                 mercurial_wrappermode => {
49                         type => "string",
50                         example => '06755',
51                         description => "mode for mercurial_wrapper (can safely be made suid)",
52                         safe => 0,
53                         rebuild => 0,
54                 },
55                 historyurl => {
56                         type => "string",
57                         example => "http://example.com:8000/log/tip/[[file]]",
58                         description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
59                         safe => 1,
60                         rebuild => 1,
61                 },
62                 diffurl => {
63                         type => "string",
64                         example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
65                         description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
66                         safe => 1,
67                         rebuild => 1,
68                 },
69 }
70
71 sub mercurial_log ($) {
72         my $out = shift;
73         my @infos;
74
75         while (<$out>) {
76                 my $line = $_;
77                 my ($key, $value);
78
79                 if (/^description:/) {
80                         $key = "description";
81                         $value = "";
82
83                         # slurp everything as the description text 
84                         # until the next changeset
85                         while (<$out>) {
86                                 if (/^changeset: /) {
87                                         $line = $_;
88                                         last;
89                                 }
90
91                                 $value .= $_;
92                         }
93
94                         local $/ = "";
95                         chomp $value;
96                         $infos[$#infos]{$key} = $value;
97                 }
98
99                 chomp $line;
100                 ($key, $value) = split /: +/, $line, 2;
101
102                 if ($key eq "changeset") {
103                         push @infos, {};
104
105                         # remove the revision index, which is strictly 
106                         # local to the repository
107                         $value =~ s/^\d+://;
108                 }
109
110                 $infos[$#infos]{$key} = $value;
111         }
112         close $out;
113
114         return @infos;
115 }
116
117 sub rcs_update () {
118         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
119         if (system(@cmdline) != 0) {
120                 warn "'@cmdline' failed: $!";
121         }
122 }
123
124 sub rcs_prepedit ($) {
125         return "";
126 }
127
128 sub rcs_commit ($$$;$$) {
129         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
130
131         if (defined $user) {
132                 $user = IkiWiki::possibly_foolish_untaint($user);
133         }
134         elsif (defined $ipaddr) {
135                 $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
136         }
137         else {
138                 $user = "Anonymous";
139         }
140
141         $message = IkiWiki::possibly_foolish_untaint($message);
142         if (! length $message) {
143                 $message = "no message given";
144         }
145
146         my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit", 
147                        "-m", $message, "-u", $user);
148         if (system(@cmdline) != 0) {
149                 warn "'@cmdline' failed: $!";
150         }
151
152         return undef; # success
153 }
154
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)=@_;
159         
160         error("rcs_commit_staged not implemented for mercurial"); # TODO
161 }
162
163 sub rcs_add ($) {
164         my ($file) = @_;
165
166         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
167         if (system(@cmdline) != 0) {
168                 warn "'@cmdline' failed: $!";
169         }
170 }
171
172 sub rcs_remove ($) {
173         my ($file) = @_;
174
175         error("rcs_remove not implemented for mercurial"); # TODO
176 }
177
178 sub rcs_rename ($$) {
179         my ($src, $dest) = @_;
180
181         error("rcs_rename not implemented for mercurial"); # TODO
182 }
183
184 sub rcs_recentchanges ($) {
185         my ($num) = @_;
186
187         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
188                 "--style", "default");
189         open (my $out, "@cmdline |");
190
191         eval q{use Date::Parse};
192         error($@) if $@;
193
194         my @ret;
195         foreach my $info (mercurial_log($out)) {
196                 my @pages = ();
197                 my @message = ();
198         
199                 foreach my $msgline (split(/\n/, $info->{description})) {
200                         push @message, { line => $msgline };
201                 }
202
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;
207
208                         push @pages, {
209                                 page => pagename($file),
210                                 diffurl => $diffurl,
211                         };
212                 }
213
214                 my $user = $info->{"user"};
215                 $user =~ s/\s*<.*>\s*$//;
216                 $user =~ s/^\s*//;
217
218                 push @ret, {
219                         rev        => $info->{"changeset"},
220                         user       => $user,
221                         committype => "hg",
222                         when       => str2time($info->{"date"}),
223                         message    => [@message],
224                         pages      => [@pages],
225                 };
226         }
227
228         return @ret;
229 }
230
231 sub rcs_diff ($) {
232         # TODO
233 }
234
235 sub rcs_getctime ($) {
236         my ($file) = @_;
237
238         # XXX filename passes through the shell here, should try to avoid
239         # that just in case
240         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
241                 "--style", "default", "$config{srcdir}/$file");
242         open (my $out, "@cmdline |");
243
244         my @log = mercurial_log($out);
245
246         if (length @log < 1) {
247                 return 0;
248         }
249
250         eval q{use Date::Parse};
251         error($@) if $@;
252         
253         my $ctime = str2time($log[$#log]->{"date"});
254         return $ctime;
255 }
256
257 1