1 # Support for the darcs rcs, <URL:http://darcs.net/>.
2 # Copyright (C) 2006 Thomas Schwinge <tschwinge@gnu.org>
3 # 2007 Benjamin A'Lee <bma@bmalee.eu>
4 # Tuomo Valkonen <tuomov@iki.fi>
5 # 2008 Simon Michael <simon@joyful.com>
6 # Petr Ročkai <me@mornfall.net>
7 # Sven M. Hallberg <pesco@khjk.org>
9 # This program is free software; you can redistribute it and/or modify it
10 # under the terms of the GNU General Public License as published by the
11 # Free Software Foundation; either version 2 of the License, or (at your
12 # option) any later version.
14 # This program is distributed in the hope that it will be useful, but
15 # WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 # General Public License for more details.
19 # You should have received a copy of the GNU General Public License along
20 # with this program; if not, write to the Free Software Foundation, Inc.,
21 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 # History (see http://ikiwiki.info/todo/darcs/):
25 # * Thomas Schwinge wrote the original file, implementing only rcs_commit.
26 # * Benjamin A'Lee contributed an alternative implementation.
27 # * Tuomo Valkonen contributed rcs_getctime and stub rcs_recentchanges.
28 # * Simon Michael contributed multiple changes.
29 # * Petr Ročkai fixed rcs_recentchanges and added caching to rcs_getctime.
30 # * Sven M. Hallberg merged the above and added missing features.
33 # We're guaranteed to be the only instance of ikiwiki running at a given
34 # time. It is essential that only ikiwiki is working on a particular
35 # repository. That means one instance of ikiwiki and it also means that
36 # you must not 'darcs push' into this repository, as this might create
37 # race conditions, as I understand it.
40 package IkiWiki::Plugin::darcs;
48 hook(type => "checkconfig", id => "darcs", call => \&checkconfig);
49 hook(type => "getsetup", id => "darcs", call => \&getsetup);
50 hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
51 hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
52 hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
53 hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
54 hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
55 hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
56 hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
57 hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
58 hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
59 hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
65 sub silentsystem (@) {
66 open(SAVED_STDOUT, ">&STDOUT");
67 open(STDOUT, ">/dev/null");
69 open(STDOUT, ">&SAVED_STDOUT");
73 sub darcs_info ($$$) {
76 my $file = shift; # Relative to the repodir.
78 my $child = open(DARCS_CHANGES, "-|");
80 exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
81 error("failed to run 'darcs changes'");
84 # Brute force for now. :-/
85 while (<DARCS_CHANGES>) {
86 last if /^<\/created_as>$/;
88 ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
89 $field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.
100 my $child = open(DARCS_MANIFEST, "-|");
102 exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
103 error("failed to run 'darcs query manifest'");
106 while (<DARCS_MANIFEST>) {
107 $found = 1, last if /^(\.\/)?$file$/;
109 close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
115 my $file = shift; # Relative to the repodir.
116 my $repodir = $config{srcdir};
118 return "" if (! file_in_vc($repodir, $file));
119 my $hash = darcs_info('hash', $repodir, $file);
120 return defined $hash ? $hash : "";
124 # Exported functions.
127 if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
128 push @{$config{wrappers}}, {
129 wrapper => $config{darcs_wrapper},
130 wrappermode => (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
138 safe => 0, # rcs plugin
143 example => "/darcs/repo/_darcs/ikiwiki-wrapper",
144 description => "wrapper to generate (set as master repo apply hook)",
148 darcs_wrappermode => {
151 description => "mode for darcs_wrapper (can safely be made suid)",
157 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
158 description => "darcsweb url to show file history ([[file]] substituted)",
164 example => "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
165 description => "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
172 silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
175 sub rcs_prepedit ($) {
176 # Prepares to edit a file under revision control. Returns a token that
177 # must be passed to rcs_commit() when the file is to be commited. For us,
178 # this token the hash value of the latest patch that modifies the file,
179 # i.e. something like its current revision. If the file is not yet added
180 # to the repository, we return TODO: the empty string.
182 my $file = shift; # Relative to the repodir.
183 my $rev = darcs_rev($file);
187 sub rcs_commit ($$$;$$) {
188 # Commit the page. Returns 'undef' on success and a version of the page
189 # with conflict markers on failure.
191 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
193 # Compute if the "revision" of $file changed.
194 my $changed = darcs_rev($file) ne $rcstoken;
196 # Yes, the following is a bit convoluted.
198 # TODO. Invent a better, non-conflicting name.
199 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
200 error("failed to rename $file to $file.save: $!");
202 # Roll the repository back to $rcstoken.
204 # TODO. Can we be sure that no changes are lost? I think that
205 # we can, if we make sure that the 'darcs push' below will always
208 # We need to revert everything as 'darcs obliterate' might choke
210 # TODO: 'yes | ...' needed? Doesn't seem so.
211 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") and
212 error("'darcs revert' failed");
213 # Remove all patches starting at $rcstoken.
214 my $child = open(DARCS_OBLITERATE, "|-");
216 open(STDOUT, ">/dev/null");
217 exec('darcs', "obliterate", "--repodir", $config{srcdir},
218 "--match", "hash " . $rcstoken) and
219 error("'darcs obliterate' failed");
221 while (print DARCS_OBLITERATE "y") {
224 close(DARCS_OBLITERATE);
225 # Restore the $rcstoken one.
226 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
227 "--match", "hash " . $rcstoken, "--all") and
228 error("'darcs pull' failed");
230 # We're back at $rcstoken. Re-install the modified file.
231 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
232 error("failed to rename $file.save to $file: $!");
235 # Record the changes.
238 $author = "$user\@web";
239 } elsif (defined $ipaddr) {
240 $author = "$ipaddr\@web";
242 $author = "anon\@web";
244 if (!defined $message || !length($message)) {
245 $message = "empty message";
247 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
248 '-m', $message, '--author', $author, $file) and
249 error("'darcs record' failed");
251 # Update the repository by pulling from the default repository, which is
253 silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
254 "--all") and error("'darcs pull' failed");
256 # If this updating yields any conflicts, we'll record them now to resolve
257 # them. If nothing is recorded, there are no conflicts.
258 $rcstoken = darcs_rev($file);
259 # TODO: Use only the first line here, i.e. only the patch name?
260 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
261 silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
262 '-m', 'resolve conflicts: ' . $message, '--author', $author, $file) and
263 error("'darcs record' failed");
264 my $conflicts = darcs_rev($file) ne $rcstoken;
265 unlink("$config{srcdir}/$file.log") or
266 error("failed to remove '$file.log'");
268 # Push the changes to the main repository.
269 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
270 and error("'darcs push' failed");
274 my $document = readfile("$config{srcdir}/$file");
275 # Try to leave everything in a consistent state.
276 # TODO: 'yes | ...' needed? Doesn't seem so.
277 silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") and
278 warn("'darcs revert' failed");
285 sub rcs_commit_staged($$$) {
286 my ($message, $user, $ipaddr) = @_;
290 $author = "$user\@web";
291 } elsif (defined $ipaddr) {
292 $author = "$ipaddr\@web";
294 $author = "anon\@web";
296 if (!defined $message || !length($message)) {
297 $message = "empty message";
300 silentsystem('darcs', "record", "--repodir", $config{srcdir}, "-a", "-A", $author,
301 "-m", $message) and error("'darcs record' failed");
303 # Push the changes to the main repository.
304 silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all')
305 and error("'darcs push' failed");
312 my $file = shift; # Relative to the repodir.
314 if(! file_in_vc($config{srcdir}, $file)) {
315 # Intermediate directories will be added automagically.
316 system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
317 '--boring', $file) and error("'darcs add' failed");
322 my $file = shift; # Relative to the repodir.
324 unlink($config{srcdir}.'/'.$file);
327 sub rcs_rename ($$) {
328 my $a = shift; # Relative to the repodir.
329 my $b = shift; # Relative to the repodir.
331 system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b)
332 and error("'darcs mv' failed");
335 sub rcs_recentchanges ($) {
339 eval q{use Date::Parse};
340 eval q{use XML::Simple};
342 my $repodir=$config{srcdir};
344 debug("darcs recent changes: $num");
346 my $child = open(LOG, "-|");
348 $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
349 exec("darcs", "changes", "--xml",
351 "--repodir", "$repodir",
353 || error("'darcs changes' failed to run");
356 $data .= $_ while(<LOG>);
359 my $log = XMLin($data, ForceArray => 1);
361 debug("parsing recent changes...");
362 foreach my $patch (@{$log->{patch}}) {
363 my $date=$patch->{local_date};
364 my $hash=$patch->{hash};
365 my $when=str2time($date);
366 my (@pages, @files, @pg);
367 push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
368 push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
369 push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
372 $f = $_->{content} if (ref $_);
373 $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
377 for (@{$patch->{summary}->[0]->{move}}) {
379 push @files, $p->{from};
384 my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
385 $d =~ s/\[\[file\]\]/$f/go;
386 $d =~ s/\[\[hash\]\]/$hash/go;
389 debug("diffurl: $d");
391 page => pagename($f),
395 next unless (scalar @pg > 0);
396 debug("recent change: " . $patch->{name}[0] . " ("
397 . scalar @pg . " changes)");
400 push @message, { line => $_ } for (@{$patch->{name}});
403 if ($patch->{author} =~ /\@web$/) {
406 $committype = "darcs";
410 rev => $patch->{hash},
411 user => $patch->{author},
412 committype => $committype,
414 message => [@message],
425 foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
426 if (@lines || $line=~/^diff/) {
427 push @lines, $line."\n";
434 return join("", @lines);
438 sub rcs_getctime ($) {
441 eval q{use Date::Parse};
442 eval q{use XML::Simple};
445 # Sigh... doing things the hard way again
446 my $repodir=$config{srcdir};
448 my $filer=substr($file, length($repodir));
451 my $child = open(LOG, "-|");
453 exec("darcs", "changes", "--xml", "--reverse",
454 "--repodir", "$repodir", "$filer")
455 || error("'darcs changes $filer' failed to run");
459 $data .= $_ while(<LOG>);
462 my $log = XMLin($data, ForceArray => 1);
464 my $datestr=$log->{patch}[0]->{local_date};
466 if (! defined $datestr) {
467 warn "failed to get ctime for $filer";
471 my $date=str2time($datestr);
473 #debug("found ctime ".localtime($date)." for $filer");
480 # vim: ts=4 sw=4 noet