<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">#!/usr/bin/perl
package IkiWiki::Plugin::darcs;

use warnings;
use strict;
use URI::Escape q{uri_escape_utf8};
use IkiWiki;

sub import {
	hook(type =&gt; "checkconfig", id =&gt; "darcs", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "getsetup", id =&gt; "darcs", call =&gt; \&amp;getsetup);
	hook(type =&gt; "rcs", id =&gt; "rcs_update", call =&gt; \&amp;rcs_update);
	hook(type =&gt; "rcs", id =&gt; "rcs_prepedit", call =&gt; \&amp;rcs_prepedit);
	hook(type =&gt; "rcs", id =&gt; "rcs_commit", call =&gt; \&amp;rcs_commit);
	hook(type =&gt; "rcs", id =&gt; "rcs_commit_staged", call =&gt; \&amp;rcs_commit_staged);
	hook(type =&gt; "rcs", id =&gt; "rcs_add", call =&gt; \&amp;rcs_add);
	hook(type =&gt; "rcs", id =&gt; "rcs_remove", call =&gt; \&amp;rcs_remove);
	hook(type =&gt; "rcs", id =&gt; "rcs_rename", call =&gt; \&amp;rcs_rename);
	hook(type =&gt; "rcs", id =&gt; "rcs_recentchanges", call =&gt; \&amp;rcs_recentchanges);
	hook(type =&gt; "rcs", id =&gt; "rcs_diff", call =&gt; \&amp;rcs_diff);
	hook(type =&gt; "rcs", id =&gt; "rcs_getctime", call =&gt; \&amp;rcs_getctime);
	hook(type =&gt; "rcs", id =&gt; "rcs_getmtime", call =&gt; \&amp;rcs_getmtime);
}

sub silentsystem (@) {
	open(SAVED_STDOUT, "&gt;&amp;STDOUT");
	open(STDOUT, "&gt;/dev/null");
	my $ret = system @_;
	open(STDOUT, "&gt;&amp;SAVED_STDOUT");
	return $ret;
}

sub darcs_info ($$$) {
	my $field = shift;
	my $repodir = shift;
	my $file = shift; # Relative to the repodir.

	my $child = open(DARCS_CHANGES, "-|");
	if (! $child) {
		exec('darcs', 'changes', '--repodir', $repodir, '--xml-output', $file) or
			error("failed to run 'darcs changes'");
	}

	# Brute force for now.  :-/
	while (&lt;DARCS_CHANGES&gt;) {
		last if /^&lt;\/created_as&gt;$/;
	}
	($_) = &lt;DARCS_CHANGES&gt; =~ /$field=\'([^\']+)/;
	$field eq 'hash' and s/\.gz//; # Strip away the '.gz' from 'hash'es.

	close(DARCS_CHANGES);

	return $_;
}

sub file_in_vc ($$) {
	my $repodir = shift;
	my $file = shift;

	my $child = open(DARCS_MANIFEST, "-|");
	if (! $child) {
		exec('darcs', 'query', 'manifest', '--repodir', $repodir) or
			error("failed to run 'darcs query manifest'");
	}
	my $found=0;
	while (&lt;DARCS_MANIFEST&gt;) {
		$found = 1 if /^(\.\/)?$file$/;
	}
	close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);

	return $found;
}

sub darcs_rev ($) {
	my $file = shift; # Relative to the repodir.
	my $repodir = $config{srcdir};

	return "" unless file_in_vc($repodir, $file);
	my $hash = darcs_info('hash', $repodir, $file);
	return defined $hash ? $hash : "";
}

sub checkconfig () {
	if (defined $config{darcs_wrapper} &amp;&amp; length $config{darcs_wrapper}) {
		push @{$config{wrappers}}, {
			wrapper =&gt; $config{darcs_wrapper},
			wrappermode =&gt; (defined $config{darcs_wrappermode} ? $config{darcs_wrappermode} : "06755"),
		};
	}
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 0, # rcs plugin
			rebuild =&gt; undef,
			section =&gt; "rcs",
		},
		darcs_wrapper =&gt; {
			type =&gt; "string",
			example =&gt; "/darcs/repo/_darcs/ikiwiki-wrapper",
			description =&gt; "wrapper to generate (set as master repo apply hook)",
			safe =&gt; 0, # file
			rebuild =&gt; 0,
		},
		darcs_wrappermode =&gt; {
			type =&gt; "string",
			example =&gt; '06755',
			description =&gt; "mode for darcs_wrapper (can safely be made suid)",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
		historyurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filehistory;f=[[file]]",
			description =&gt; "darcsweb url to show file history ([[file]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		diffurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://darcs.example.com/darcsweb.cgi?r=wiki;a=filediff;h=[[hash]];f=[[file]]",
			description =&gt; "darcsweb url to show a diff ([[hash]] and [[file]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
}

sub rcs_update () {
	silentsystem('darcs', "pull", "--repodir", $config{srcdir}, "-qa")
}

sub rcs_prepedit ($) {
	# Prepares to edit a file under revision control.  Returns a token that
	# must be passed to rcs_commit() when the file is to be commited.  For us,
	# this token the hash value of the latest patch that modifies the file,
	# i.e. something like its current revision.

	my $file = shift; # Relative to the repodir.
	my $rev = darcs_rev($file);
	return $rev;
}

sub commitauthor (@) {
	my %params=@_;
	
	my $author="anon\@web";
	if (defined $params{session}) {
		if (defined $params{session}-&gt;param("name")) {
			return $params{session}-&gt;param("name").'@web';
		}
		elsif (defined $params{session}-&gt;remote_addr()) {
			return $params{session}-&gt;remote_addr().'@web';
		}
	}
	return 'anon@web';
}

sub rcs_commit (@) {
	# Commit the page.  Returns 'undef' on success and a version of the page
	# with conflict markers on failure.
	my %params=@_;

	my ($file, $message, $token) =
		($params{file}, $params{message}, $params{token});

	# Compute if the "revision" of $file changed.
	my $changed = darcs_rev($file) ne $token;

	# Yes, the following is a bit convoluted.
	if ($changed) {
		# TODO.  Invent a better, non-conflicting name.
		rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
			error("failed to rename $file to $file.save: $!");

		# Roll the repository back to $token.

		# TODO.  Can we be sure that no changes are lost?  I think that
		# we can, if we make sure that the 'darcs push' below will always
		# succeed.
	
		# We need to revert everything as 'darcs obliterate' might choke
		# otherwise.
		# TODO: 'yes | ...' needed?  Doesn't seem so.
		silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
			error("'darcs revert' failed");
		# Remove all patches starting at $token.
		my $child = open(DARCS_OBLITERATE, "|-");
		if (! $child) {
			open(STDOUT, "&gt;/dev/null");
			exec('darcs', "obliterate", "--repodir", $config{srcdir},
			   "--match", "hash " . $token) and
			   error("'darcs obliterate' failed");
		}
		1 while print DARCS_OBLITERATE "y";
		close(DARCS_OBLITERATE);
		# Restore the $token one.
		silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
			"--match", "hash " . $token, "--all") == 0 ||
			error("'darcs pull' failed");
	
		# We're back at $token.  Re-install the modified file.
		rename("$config{srcdir}/$file.save", "$config{srcdir}/$file") or
			error("failed to rename $file.save to $file: $!");
	}

	# Record the changes.
	my $author=commitauthor(%params);
	if (!defined $message || !length($message)) {
		$message = "empty message";
	}
	silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
			'-m', $message, '--author', $author, $file) == 0 ||
		error("'darcs record' failed");

	# Update the repository by pulling from the default repository, which is
	# master repository.
	silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
		"--all") == 0 || error("'darcs pull' failed");

	# If this updating yields any conflicts, we'll record them now to resolve
	# them.  If nothing is recorded, there are no conflicts.
	$token = darcs_rev($file);
	# TODO: Use only the first line here, i.e. only the patch name?
	writefile("$file.log", $config{srcdir}, 'resolve conflicts: ' . $message);
	silentsystem('darcs', 'record', '--repodir', $config{srcdir}, '--all',
		'-m', 'resolve conflicts: ' . $message, '--author', $author, $file) == 0 ||
		error("'darcs record' failed");
	my $conflicts = darcs_rev($file) ne $token;
	unlink("$config{srcdir}/$file.log") or
		error("failed to remove '$file.log'");

	# Push the changes to the main repository.
	silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
		error("'darcs push' failed");
	# TODO: darcs send?

	if ($conflicts) {
		my $document = readfile("$config{srcdir}/$file");
		# Try to leave everything in a consistent state.
		# TODO: 'yes | ...' needed?  Doesn't seem so.
		silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 || 
			warn("'darcs revert' failed");
		return $document;
	}
	else {
		return undef;
	}
}

sub rcs_commit_staged (@) {
	my %params=@_;

	my $author=commitauthor(%params);
	if (!defined $params{message} || !length($params{message})) {
		$params{message} = "empty message";
	}

	silentsystem('darcs', "record", "--repodir", $config{srcdir},
		"-a", "-A", $author,
		"-m", $params{message},
	) == 0 || error("'darcs record' failed");

	# Push the changes to the main repository.
	silentsystem('darcs', 'push', '--quiet', '--repodir', $config{srcdir}, '--all') == 0 ||
		error("'darcs push' failed");
	# TODO: darcs send?

	return undef;
}

sub rcs_add ($) {
	my $file = shift; # Relative to the repodir.

	if(! file_in_vc($config{srcdir}, $file)) {
		# Intermediate directories will be added automagically.
		system('darcs', 'add', '--quiet', '--repodir', $config{srcdir},
			'--boring', $file) == 0 || error("'darcs add' failed");
	}
}

sub rcs_remove ($) {
	my $file = shift; # Relative to the repodir.

	unlink($config{srcdir}.'/'.$file);
}

sub rcs_rename ($$) {
	my $a = shift; # Relative to the repodir.
	my $b = shift; # Relative to the repodir.

	system('darcs', 'mv', '--repodir', $config{srcdir}, $a, $b) == 0 ||
		error("'darcs mv' failed");
}

sub rcs_recentchanges ($) {
	my $num=shift;
	my @ret;

	eval q{use Date::Parse};
	eval q{use XML::Simple};

	my $repodir=$config{srcdir};

	my $child = open(LOG, "-|");
	if (! $child) {
		$ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
		exec("darcs", "changes", "--xml", 
			"--summary",
			 "--repodir", "$repodir",
			 "--last", "$num")
		|| error("'darcs changes' failed to run");
	}
	my $data;
	$data .= $_ while(&lt;LOG&gt;);
	close LOG;

	my $log = XMLin($data, ForceArray =&gt; 1);

	foreach my $patch (@{$log-&gt;{patch}}) {
		my $date=$patch-&gt;{local_date};
		my $hash=$patch-&gt;{hash};
		my $when=str2time($date);
		my (@pages, @files, @pg);
		push @pages, $_ foreach (@{$patch-&gt;{summary}-&gt;[0]-&gt;{modify_file}});
		push @pages, $_ foreach (@{$patch-&gt;{summary}-&gt;[0]-&gt;{add_file}});
		push @pages, $_ foreach (@{$patch-&gt;{summary}-&gt;[0]-&gt;{remove_file}});
		foreach my $f (@pages) {
			$f = $f-&gt;{content} if ref $f;
			$f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace

			push @files, $f;
		}
		foreach my $p (@{$patch-&gt;{summary}-&gt;[0]-&gt;{move}}) {
			push @files, $p-&gt;{from};
		}

		foreach my $f (@files) {
			my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
			my $ef = uri_escape_utf8($f);
			$d =~ s/\[\[file\]\]/$ef/go;
			$d =~ s/\[\[hash\]\]/$hash/go;

			push @pg, {
				page =&gt; pagename($f),
				diffurl =&gt; $d,
			};
		}
		next unless (scalar @pg &gt; 0);

		my @message;
		push @message, { line =&gt; $_ } foreach (@{$patch-&gt;{name}});

		my $committype;
		my $author;
		if ($patch-&gt;{author} =~ /(.*)\@web$/) {
			$author = $1;
			$committype = "web";
		}
		else {
			$author=$patch-&gt;{author};
			$committype = "darcs";
		}

		push @ret, {
			rev =&gt; $patch-&gt;{hash},
			user =&gt; $author,
			committype =&gt; $committype,
			when =&gt; $when, 
			message =&gt; [@message],
			pages =&gt; [@pg],
		};
	}

	return @ret;
}

sub rcs_diff ($;$) {
	my $rev=shift;
	my $maxlines=shift;
	my @lines;
	my $repodir=$config{srcdir};
	foreach my $line (`darcs diff --repodir  $repodir --match 'hash $rev'`) {
		if (@lines || $line=~/^diff/) {
			last if defined $maxlines &amp;&amp; @lines == $maxlines;
			push @lines, $line."\n";
		}
	}
	if (wantarray) {
		return @lines;
	}
	else {
		return join("", @lines);
	}
}

sub rcs_getctime ($) {
	my $file=shift;

	eval q{use Date::Parse};
	eval q{use XML::Simple};
	local $/=undef;

	my $child = open(LOG, "-|");
	if (! $child) {
		exec("darcs", "changes", "--xml", "--reverse",
			"--repodir", $config{srcdir}, $file)
		|| error("'darcs changes $file' failed to run");
	}

	my $data;
	{
		local $/=undef;
		$data = &lt;LOG&gt;;
	}
	close LOG;

	my $log = XMLin($data, ForceArray =&gt; 1);

	my $datestr = $log-&gt;{patch}[0]-&gt;{local_date};

	if (! defined $datestr) {
		warn "failed to get ctime for $file";
		return 0;
	}

	my $date = str2time($datestr);
	
	debug("ctime for '$file': ". localtime($date));

	return $date;
}

sub rcs_getmtime ($) {
	error "rcs_getmtime is not implemented for darcs\n"; # TODO
}

1
</pre></body></html>