Merge branch 'master' of ssh://git.ikiwiki.info/srv/git/ikiwiki.info
[ikiwiki.git] / IkiWiki / Plugin / darcs.pm
blob1313041e78fe9c61588c04a53bae58e4e22d33d5
1 #!/usr/bin/perl
2 package IkiWiki::Plugin::darcs;
4 use warnings;
5 use strict;
6 use IkiWiki;
8 sub import {
9 hook(type => "checkconfig", id => "darcs", call => \&checkconfig);
10 hook(type => "getsetup", id => "darcs", call => \&getsetup);
11 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
12 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
13 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
14 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
15 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
16 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
17 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
18 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
19 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
20 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
21 hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
24 sub silentsystem (@) {
25 open(SAVED_STDOUT, ">&STDOUT");
26 open(STDOUT, ">/dev/null");
27 my $ret = system @_;
28 open(STDOUT, ">&SAVED_STDOUT");
29 return $ret;
32 sub darcs_info ($$$) {
33 my $field = shift;
34 my $repodir = shift;
35 my $file = shift; # Relative to the repodir.
37 my $child = open(DARCS_CHANGES, "-|");
38 if (! $child) {
39 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
40 error("failed to run 'darcs changes'");
43 # Brute force for now. :-/
44 while (<DARCS_CHANGES>) {
45 last if /^<\/created_as>$/;
47 ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
48 $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
50 close(DARCS_CHANGES);
52 return $_;
55 sub file_in_vc ($$) {
56 my $repodir = shift;
57 my $file = shift;
59 my $child = open(DARCS_MANIFEST, "-|");
60 if (! $child) {
61 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
62 error("failed to run 'darcs query manifest'");
64 my $found=0;
65 while (<DARCS_MANIFEST>) {
66 $found = 1 if /^(\.\/)?$file$/;
68 close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
70 return $found;
73 sub darcs_rev ($) {
74 my $file = shift; # Relative to the repodir.
75 my $repodir = $config{srcdir};
77 return "" unless file_in_vc($repodir, $file);
78 my $hash = darcs_info('hash', $repodir, $file);
79 return defined $hash ? $hash : "";
82 sub checkconfig () {
83 if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
84 push @{$config{wrappers}}, {
85 wrapper => $config{darcs_wrapper},
86 wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
91 sub getsetup () {
92 return
93 plugin => {
94 safe => 0, # rcs plugin
95 rebuild => undef,
96 section => "rcs",
98 darcs_wrapper => {
99 type => "string",
100 example => "/darcs/repo/_darcs/ikiwiki-wrapper",
101 description => "wrapper to generate (set as master repo apply hook)",
102 safe => 0, # file
103 rebuild => 0,
105 darcs_wrappermode => {
106 type => "string",
107 example => '06755',
108 description => "mode for darcs_wrapper (can safely be made suid)",
109 safe => 0,
110 rebuild => 0,
112 historyurl => {
113 type => "string",
114 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
115 description => "darcsweb url to show file history ([[file]] substituted)",
116 safe => 1,
117 rebuild => 1,
119 diffurl => {
120 type => "string",
121 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
122 description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
123 safe => 1,
124 rebuild => 1,
128 sub rcs_update () {
129 silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
132 sub rcs_prepedit ($) {
133 # Prepares to edit a file under revision control. Returns a token that
134 # must be passed to rcs_commit() when the file is to be commited. For us,
135 # this token the hash value of the latest patch that modifies the file,
136 # i.e. something like its current revision.
138 my $file = shift; # Relative to the repodir.
139 my $rev = darcs_rev($file);
140 return $rev;
143 sub commitauthor (@) {
144 my %params=@_;
146 my $author="anon\@web";
147 if (defined $params{session}) {
148 if (defined $params{session}->param("name")) {
149 return $params{session}->param("name").'@web';
151 elsif (defined $params{session}->remote_addr()) {
152 return $params{session}->remote_addr().'@web';
155 return 'anon@web';
158 sub rcs_commit (@) {
159 # Commit the page. Returns 'undef' on success and a version of the page
160 # with conflict markers on failure.
161 my %params=@_;
163 my ($file, $message, $token) =
164 ($params{file}, $params{message}, $params{token});
166 # Compute if the "revision" of $file changed.
167 my $changed = darcs_rev($file) ne $token;
169 # Yes, the following is a bit convoluted.
170 if ($changed) {
171 # TODO. Invent a better, non-conflicting name.
172 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
173 error("failed to rename $file to $file.save: $!");
175 # Roll the repository back to $token.
177 # TODO. Can we be sure that no changes are lost? I think that
178 # we can, if we make sure that the 'darcs push' below will always
179 # succeed.
181 # We need to revert everything as 'darcs obliterate' might choke
182 # otherwise.
183 # TODO: 'yes | ...' needed? Doesn't seem so.
184 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
185 error("'darcs revert' failed");
186 # Remove all patches starting at $token.
187 my $child = open(DARCS_OBLITERATE, "|-");
188 if (! $child) {
189 open(STDOUT, ">/dev/null");
190 exec('darcs', "obliterate", "--repodir", $config{srcdir},
191 "--match", "hash " . $token) and
192 error("'darcs obliterate' failed");
194 1 while print DARCS_OBLITERATE "y";
195 close(DARCS_OBLITERATE);
196 # Restore the $token one.
197 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
198 "--match", "hash " . $token, "--all") == 0 ||
199 error("'darcs pull' failed");
201 # We're back at $token. Re-install the modified file.
202 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
203 error("failed to rename $file.save to $file: $!");
206 # Record the changes.
207 my $author=commitauthor(%params);
208 if (!defined $message || !length($message)) {
209 $message = "empty message";
211 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
212 '-m', $message, '--author', $author, $file) == 0 ||
213 error("'darcs record' failed");
215 # Update the repository by pulling from the default repository, which is
216 # master repository.
217 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
218 "--all") == 0 || error("'darcs pull' failed");
220 # If this updating yields any conflicts, we'll record them now to resolve
221 # them. If nothing is recorded, there are no conflicts.
222 $token = darcs_rev($file);
223 # TODO: Use only the first line here, i.e. only the patch name?
224 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
225 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
226 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
227 error("'darcs record' failed");
228 my $conflicts = darcs_rev($file) ne $token;
229 unlink("$config{srcdir}/$file.log") or
230 error("failed to remove '$file.log'");
232 # Push the changes to the main repository.
233 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
234 error("'darcs push' failed");
235 # TODO: darcs send?
237 if ($conflicts) {
238 my $document = readfile("$config{srcdir}/$file");
239 # Try to leave everything in a consistent state.
240 # TODO: 'yes | ...' needed? Doesn't seem so.
241 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
242 warn("'darcs revert' failed");
243 return $document;
245 else {
246 return undef;
250 sub rcs_commit_staged (@) {
251 my %params=@_;
253 my $author=commitauthor(%params);
254 if (!defined $params{message} || !length($params{message})) {
255 $params{message} = "empty message";
258 silentsystem('darcs', "record", "--repodir", $config{srcdir},
259 "-a", "-A", $author,
260 "-m", $params{message},
261 ) == 0 || error("'darcs record' failed");
263 # Push the changes to the main repository.
264 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
265 error("'darcs push' failed");
266 # TODO: darcs send?
268 return undef;
271 sub rcs_add ($) {
272 my $file = shift; # Relative to the repodir.
274 if(! file_in_vc($config{srcdir}, $file)) {
275 # Intermediate directories will be added automagically.
276 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
277 '--boring', $file) == 0 || error("'darcs add' failed");
281 sub rcs_remove ($) {
282 my $file = shift; # Relative to the repodir.
284 unlink($config{srcdir}.'/'.$file);
287 sub rcs_rename ($$) {
288 my $a = shift; # Relative to the repodir.
289 my $b = shift; # Relative to the repodir.
291 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
292 error("'darcs mv' failed");
295 sub rcs_recentchanges ($) {
296 my $num=shift;
297 my @ret;
299 eval q{use Date::Parse};
300 eval q{use XML::Simple};
302 my $repodir=$config{srcdir};
304 my $child = open(LOG, "-|");
305 if (! $child) {
306 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
307 exec("darcs", "changes", "--xml",
308 "--summary",
309 "--repodir", "$repodir",
310 "--last", "$num")
311 || error("'darcs changes' failed to run");
313 my $data;
314 $data .= $_ while(<LOG>);
315 close LOG;
317 my $log = XMLin($data, ForceArray => 1);
319 foreach my $patch (@{$log->{patch}}) {
320 my $date=$patch->{local_date};
321 my $hash=$patch->{hash};
322 my $when=str2time($date);
323 my (@pages, @files, @pg);
324 push @pages, $_ foreach (@{$patch->{summary}->[0]->{modify_file}});
325 push @pages, $_ foreach (@{$patch->{summary}->[0]->{add_file}});
326 push @pages, $_ foreach (@{$patch->{summary}->[0]->{remove_file}});
327 foreach my $f (@pages) {
328 $f = $f->{content} if ref $f;
329 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
331 push @files, $f;
333 foreach my $p (@{$patch->{summary}->[0]->{move}}) {
334 push @files, $p->{from};
337 foreach my $f (@files) {
338 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
339 $d =~ s/\[\[file\]\]/$f/go;
340 $d =~ s/\[\[hash\]\]/$hash/go;
342 push @pg, {
343 page => pagename($f),
344 diffurl => $d,
347 next unless (scalar @pg > 0);
349 my @message;
350 push @message, { line => $_ } foreach (@{$patch->{name}});
352 my $committype;
353 my $author;
354 if ($patch->{author} =~ /(.*)\@web$/) {
355 $author = $1;
356 $committype = "web";
358 else {
359 $author=$patch->{author};
360 $committype = "darcs";
363 push @ret, {
364 rev => $patch->{hash},
365 user => $author,
366 committype => $committype,
367 when => $when,
368 message => [@message],
369 pages => [@pg],
373 return @ret;
376 sub rcs_diff ($;$) {
377 my $rev=shift;
378 my $maxlines=shift;
379 my @lines;
380 my $repodir=$config{srcdir};
381 foreach my $line (`darcs diff --repodir $repodir --match 'hash $rev'`) {
382 if (@lines || $line=~/^diff/) {
383 last if defined $maxlines && @lines == $maxlines;
384 push @lines, $line."\n";
387 if (wantarray) {
388 return @lines;
390 else {
391 return join("", @lines);
395 sub rcs_getctime ($) {
396 my $file=shift;
398 eval q{use Date::Parse};
399 eval q{use XML::Simple};
400 local $/=undef;
402 my $child = open(LOG, "-|");
403 if (! $child) {
404 exec("darcs", "changes", "--xml", "--reverse",
405 "--repodir", $config{srcdir}, $file)
406 || error("'darcs changes $file' failed to run");
409 my $data;
411 local $/=undef;
412 $data = <LOG>;
414 close LOG;
416 my $log = XMLin($data, ForceArray => 1);
418 my $datestr = $log->{patch}[0]->{local_date};
420 if (! defined $datestr) {
421 warn "failed to get ctime for $file";
422 return 0;
425 my $date = str2time($datestr);
427 debug("ctime for '$file': ". localtime($date));
429 return $date;
432 sub rcs_getmtime ($) {
433 error "rcs_getmtime is not implemented for darcs\n"; # TODO