1 Here's Thomas Schwinge unfinished darcs support for ikiwiki.
3 (Finishing this has been suggested as a [[soc]] project.)
5 > I haven't been working on this for months and also won't in the near
6 > future. Feel free to use what I have done so
7 > far and bring it into an usable state! Also, feel free to contact me
8 > if there are questions.
10 -- [Thomas Schwinge](mailto:tschwinge@gnu.org)
12 [[!toggle text="show"]]
13 [[!toggleable text="""
14 # Support for the darcs rcs, <URL:http://darcs.net/>.
15 # Copyright (C) 2006 Thomas Schwinge <tschwinge@gnu.org>
17 # This program is free software; you can redistribute it and/or modify it
18 # under the terms of the GNU General Public License as published by the
19 # Free Software Foundation; either version 2 of the License, or (at your
20 # option) any later version.
22 # This program is distributed in the hope that it will be useful, but
23 # WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 # General Public License for more details.
27 # You should have received a copy of the GNU General Public License along
28 # with this program; if not, write to the Free Software Foundation, Inc.,
29 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
32 # We're guaranteed to be the only instance of ikiwiki running at a given
33 # time. It is essential that only ikiwiki is working on a particular
34 # repository. That means one instance of ikiwiki and it also means that
35 # you must not `darcs push' into this repository, as this might create
36 # race conditions, as I understand it.
46 # Which darcs executable to use.
47 my $darcs = ($ENV{DARCS} or 'darcs');
52 sub darcs_info ($$$) {
55 my $file = shift; # Relative to the repodir.
57 my $child = open(DARCS_CHANGES, "-|");
59 exec($darcs, 'changes', '--repo=' . $repodir, '--xml-output', $file) or
60 error('failed to run `darcs changes\'');
63 # Brute force for now. :-/
64 while (<DARCS_CHANGES>) {
65 last if /^<\/created_as>$/;
67 ($_) = <DARCS_CHANGES> =~ /$field=\'([^\']+)/;
68 $field eq 'hash' and s/\.gz//; # Strip away the `.gz' from `hash'es.
70 close(DARCS_CHANGES) or error('`darcs changes\' exited ' . $?);
82 sub rcs_prepedit ($) {
83 # Prepares to edit a file under revision control. Returns a token that
84 # must be passed to rcs_commit() when the file is to be commited. For us,
85 # this token the hash value of the latest patch that modifies the file,
86 # i.e. something like its current revision. If the file is not yet added
87 # to the repository, we return TODO: the empty string.
89 my $file = shift; # Relative to the repodir.
91 my $hash = darcs_info('hash', $config{srcdir}, $file);
92 return defined $hash ? $hash : "";
95 sub rcs_commit ($$$) {
96 # Commit the page. Returns `undef' on success and a version of the page
97 # with conflict markers on failure.
99 my $file = shift; # Relative to the repodir.
101 my $rcstoken = shift;
103 # Compute if the ``revision'' of $file changed.
104 my $changed = darcs_info('hash', $config{srcdir}, $file) ne $rcstoken;
106 # Yes, the following is a bit convoluted.
108 # TODO. Invent a better, non-conflicting name.
109 rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
110 error("failed to rename $file to $file.save: $!");
112 # Roll the repository back to $rcstoken.
114 # TODO. Can we be sure that no changes are lost? I think that
115 # we can, if we make sure that the `darcs push' below will always
118 # We need to revert everything as `darcs obliterate' might choke
120 # TODO: `yes | ...' needed? Doesn't seem so.
121 system($darcs, "revert", "--repodir=" . $config{srcdir}, "--all") and
122 error("`darcs revert' failed");
123 # Remove all patches starting at $rcstoken.
124 # TODO. Something like `yes | darcs obliterate ...' seems to be needed.
125 system($darcs, "obliterate", "--quiet", "--repodir" . $config{srcdir},
126 "--match", "hash " . $rcstoken) and
127 error("`darcs obliterate' failed");
128 # Restore the $rcstoken one.
129 system($darcs, "pull", "--quiet", "--repodir=" . $config{srcdir},
130 "--match", "hash " . $rcstoken, "--all") and
131 error("`darcs pull' failed");
133 # We're back at $rcstoken. Re-install the modified file.
134 rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
135 error("failed to rename $file.save to $file: $!");
138 # Record the changes.
139 # TODO: What if $message is empty?
140 writefile("$file.log", $config{srcdir}, $message);
141 system($darcs, 'record', '--repodir=' . $config{srcdir}, '--all',
142 '--logfile=' . "$config{srcdir}/$file.log",
143 '--author=' . 'web commit <web-hurd@gnu.org>', $file) and
144 error('`darcs record\' failed');
146 # Update the repository by pulling from the default repository, which is
148 system($darcs, "pull", "--quiet", "--repodir=" . $config{srcdir},
149 "--all") and error("`darcs pull' failed\n");
151 # If this updating yields any conflicts, we'll record them now to resolve
152 # them. If nothing is recorded, there are no conflicts.
153 $rcstoken = darcs_info('hash', $config{srcdir}, $file);
154 # TODO: Use only the first line here, i.e. only the patch name?
155 writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
156 system($darcs, 'record', '--repodir=' . $config{srcdir}, '--all',
157 '--logfile=' . "$config{srcdir}/$file.log",
158 '--author=' . 'web commit <web-hurd@gnu.org>', $file) and
159 error('`darcs record\' failed');
160 my $conflicts = darcs_info('hash', $config{srcdir}, $file) ne $rcstoken;
161 unlink("$config{srcdir}/$file.log") or
162 error("failed to remove `$file.log'");
164 # Push the changes to the main repository.
165 system($darcs, 'push', '--quiet', '--repodir=' . $config{srcdir}, '--all')
166 and error('`darcs push\' failed');
170 my $document = readfile("$config{srcdir}/$file");
171 # Try to leave everything in a consistent state.
172 # TODO: `yes | ...' needed? Doesn't seem so.
173 system($darcs, "revert", "--repodir=" . $config{srcdir}, "--all") and
174 warn("`darcs revert' failed.\n");
182 my $file = shift; # Relative to the repodir.
184 # Intermediate directories will be added automagically.
185 system($darcs, 'add', '--quiet', '--repodir=' . $config{srcdir},
186 '--boring', $file) and error('`darcs add\' failed');
189 sub rcs_recentchanges ($) {
190 warn('rcs_recentchanges() is not implemented');
191 return 'rcs_recentchanges() is not implemented';
195 warn('rcs_notify() is not implemented');
198 sub rcs_getctime () {
199 warn('rcs_getctime() is not implemented');
205 This is my ([bma](bma@bmalee.eu)) darcs.pm - it's messy (my Perl isn't up to much) but seems to work. It uses just one repo, like the mercurial plugin (unlike the above version, which AIUI uses two).
207 `rcs_commit()` uses backticks instead of `system()`, to prevent darcs' output being sent to the browser and mucking with the HTTP headers (`darcs record` has no --quiet option). And `rcs_recentchanges()` uses regexes rather than parsing darcs' XML output.
209 [[!toggle text="show" id="bma"]]
210 [[!toggleable id="bma" text="""
218 use open qw{:utf8 :std};
222 sub rcs_update () { #{{{
223 # Do nothing - there's nowhere to update *from*.
226 sub rcs_prepedit ($) { #{{{
229 sub rcs_commit ($$$;$$) { #{{{
230 my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
232 # $user should probably be a name and an email address, by darcs
235 $user = possibly_foolish_untaint($user);
237 elsif (defined $ipaddr) {
238 $user = "Anonymous from $ipaddr";
244 $message = possibly_foolish_untaint($message);
246 # BUG: this outputs one line of text, and there's not a -q or --quiet
247 # option. Redirecting output to /dev/null works, but I still get the
248 # HTTP status and location headers displayed in the browser - is that
249 # darcs' fault or ikiwiki's?
250 # Doing it in backticks *works*, but I'm sure it could be done better.
251 my @cmdline = ("darcs", "record", "--repodir", "$config{srcdir}",
252 "-a", "-m", "$message", "--author", "$user", $file);
253 `darcs record --repodir "$config{srcdir}" -a -m "$message" --author "$user" $file`; # Return value? Output? Who needs 'em?
254 #if (system(@cmdline) != 0) {
255 # warn "'@cmdline' failed: $!";
258 return undef; # success
260 sub rcs_add ($) { # {{{
263 my @cmdline = ("darcs", "add", "--repodir", "$config{srcdir}", "-a", "-q", "$file");
264 if (system(@cmdline) != 0) {
265 warn "'@cmdline' failed: $!";
269 sub rcs_recentchanges ($) { #{{{
270 # TODO: This is horrible code. It doesn't work perfectly, and uses regexes
271 # rather than parsing Darcs' XML output.
275 return unless -d "$config{srcdir}/_darcs";
277 my $changelog = `darcs changes --xml --summary --repodir "$config{srcdir}"`;
278 $changelog = join("", split(/\s*\n\s*/, $changelog));
279 my @changes = split(/<\/patch>.*?<patch/m, $changelog);
282 foreach my $change (@changes) {
283 $change =~ m/hash='(.*?)'/;
285 $change =~ m/author='(.*?)'/;
287 my $committype = "web";
288 if($user =~ m/</) {
289 # Author fields generated by darcs include an email address: look for the "<".
290 $committype = "darcs";
292 $user = decode_entities $user;
294 $change =~ m/local_date='(.*?)'/;
296 $when=time - str2time($when, 'UTC');
297 $change =~ m/<name>(.*?)<\/name>/g;
298 my @message = {line => $1};
299 foreach my $match ($change =~ m/<comment>(.*?)<\/comment>/gm) {
300 push @message, {line => $1};
304 foreach my $match ($change =~ m/<.*?_(file|directory)>(.*?)(<(added|removed)_lines.*\/>)*<\/.*?_(file|directory)>/g) {
305 # My perl-fu is weak. I'm probably going about this all wrong, anyway.
306 push @pages, {page => pagename($match)} if ( -f $config{srcdir}."/".$match || -d $config{srcdir}."/".$match) and not $match =~ m/^$/;
308 push @ret, { rev => $rev,
310 committype => $committype,
312 message => [@message],
319 sub rcs_notify () { #{{{
323 sub rcs_getctime ($) { #{{{
324 error gettext("getctime not implemented");
335 Well, here's my version too. It only does getctime -- using a real XML parser, instead of regexp ugliness -- and maybe recentchanges, but that may be bitrotted, or maybe I never finished it, as I only need the getctime. As for actual commits, I have previously voiced my opinion, that this should be done by the plugin generating a patch bundle, and forwarding it to darcs in some way (`darcs apply` or even email to another host, possibly moderated), instead of the hacky direct modification of a working copy. It could also be faster to getctime in a batch. Just reading in all the changes the first time they're needed, might not be a big improvement in many cases, but if we got a batch request from ikiwiki, we could keep reaing the changes until all the files in this batch request have been met. --[[tuomov]]
337 [[!toggle text="show" id="tuomov"]]
338 [[!toggleable id="tuomov" text="""
341 # Stubs for no revision control.
352 sub rcs_prepedit ($) {
356 sub rcs_commit ($$$) {
357 return undef # success
363 sub rcs_recentchanges ($) {
367 eval q{use Date::Parse};
368 eval q{use XML::Simple};
370 my $repodir=$config{srcdir};
372 if (-d "$config{srcdir}/_darcs") {
373 my $child = open(LOG, "-|");
375 exec("darcs", "changes", "--xml",
376 "--repodir", "$repodir",
378 || error("darcs changes failed to run");
383 my $log = XMLin($data, ForceArray => 1);
385 foreach my $patch ($log->{patch}) {
386 my $date=$patch->{local_date};
387 my $hash=$patch->{hash};
388 my $when=concise(ago(time - str2time($date)));
391 my $child = open(SUMMARY, "-|");
393 exec("darcs", "annotate", "-s", "--xml",
394 "--match", "hash: $hash",
395 "--repodir", "$repodir")
396 || error("darcs annotate failed to run");
401 my $summary = XMLin("<lame>$data</lame>", ForceArray => 1);
407 user => $patch->{author},
408 #committype => $committype,
410 #message => [@message],
413 return @ret if @ret >= $num;
423 sub rcs_getctime ($) {
426 eval q{use Date::Parse};
427 eval q{use XML::Simple};
430 # Sigh... doing things the hard way again
431 my $repodir=$config{srcdir};
433 my $filer=substr($file, length($repodir));
436 my $child = open(LOG, "-|");
438 exec("darcs", "changes", "--xml", "--reverse",
439 "--repodir", "$repodir", "$filer")
440 || error("darcs changes $filer failed to run");
446 my $log = XMLin($data, ForceArray => 1);
448 my $datestr=$log->{patch}[0]->{local_date};
450 if (! defined $datestr) {
451 warn "failed to get ctime for $filer";
455 my $date=str2time($datestr);
457 debug("found ctime ".localtime($date)." for $file");
468 I merged the two versions above and made some fixes; it is recording my web edits in darcs and showing a recent changes page.
469 It is in a [darcs repository](http://joyful.com/darcsweb/darcsweb.cgi?r=ikiwiki-darcs), please send patches. --[[Simon_Michael]]
471 > I'd like to see at least the following fixed before I commit this: --[[Joey]]
472 > * Running `darcs record $filename` in backticks is not good (security)
473 > The thing to do is to open stdout to /dev/null before execing darcs.
474 > * Get `rcs_recentchanges_xml` working, parsing xml with regexps does
475 > not seem like a maintenance win.
476 > * `rcs_notify` should be removed, it's no longer used.
477 > * Some form of conflict handling. Using darcs to attempt to merge
478 > the changes is I gusss optional (although every other rcs backend,
479 > including svn manages to do this), but it needs to at *least* detect
480 > conflicts and return a page with conflict markers for the user to fix
483 I have addressed the recentchanges bit, you can find my hacked up darcs.pm at <http://web.mornfall.net/stuff/web-root/IkiWiki/Rcs/darcs.pm>.
485 It's got couple of FIXMEs, and a very site-specific filter for recentchanges. Not sure how to do that better though. I will eventually add web commits, probably of my own (and mention it here).