<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::monotone;

use warnings;
use strict;
use IkiWiki;
use Monotone;
use Date::Parse qw(str2time);
use Date::Format qw(time2str);

my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate sha1sums

sub import {
	hook(type =&gt; "checkconfig", id =&gt; "monotone", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "getsetup", id =&gt; "monotone", 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);
}

sub checkconfig () {
	if (!defined($config{mtnrootdir})) {
		$config{mtnrootdir} = $config{srcdir};
	}
	if (! -d "$config{mtnrootdir}/_MTN") {
		error("Ikiwiki srcdir does not seem to be a Monotone workspace (or set the mtnrootdir)!");
	}
	
	my $child = open(MTN, "-|");
	if (! $child) {
		open STDERR, "&gt;/dev/null";
		exec("mtn", "version") || error("mtn version failed to run");
	}

	my $version=undef;
	while (&lt;MTN&gt;) {
		if (/^monotone (\d+\.\d+) /) {
			$version=$1;
		}
	}

	close MTN || debug("mtn version exited $?");

	if (!defined($version)) {
		error("Cannot determine monotone version");
	}
	if ($version &lt; 0.38) {
		error("Monotone version too old, is $version but required 0.38");
	}

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

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 0, # rcs plugin
			rebuild =&gt; undef,
		},
		mtn_wrapper =&gt; {
			type =&gt; "string",
			example =&gt; "/srv/mtn/wiki/_MTN/ikiwiki-netsync-hook",
			description =&gt; "monotone netsync hook to generate",
			safe =&gt; 0, # file
			rebuild =&gt; 0,
		},
		mtn_wrappermode =&gt; {
			type =&gt; "string",
			example =&gt; '06755',
			description =&gt; "mode for mtn_wrapper (can safely be made suid)",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
		mtnkey =&gt; {
			type =&gt; "string",
			example =&gt; 'web@example.com',
			description =&gt; "your monotone key",
			safe =&gt; 1,
			rebuild =&gt; 0,
		},
		historyurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://viewmtn.example.com/branch/head/filechanges/com.example.branch/[[file]]",
			description =&gt; "viewmtn url to show file history ([[file]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		diffurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://viewmtn.example.com/revision/diff/[[r1]]/with/[[r2]]/[[file]]",
			description =&gt; "viewmtn url to show a diff ([[r1]], [[r2]], and [[file]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		mtnsync =&gt; {
			type =&gt; "boolean",
			example =&gt; 0,
			description =&gt; "sync on update and commit?",
			safe =&gt; 0, # paranoia
			rebuild =&gt; 0,
		},
		mtnrootdir =&gt; {
			type =&gt; "string",
			description =&gt; "path to your workspace (defaults to the srcdir; specify if the srcdir is a subdirectory of the workspace)",
			safe =&gt; 0, # path
			rebuild =&gt; 0,
		},
}

sub get_rev () {
	my $sha1 = `mtn --root=$config{mtnrootdir} automate get_base_revision_id`;

	($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
	if (! $sha1) {
		debug("Unable to get base revision for '$config{srcdir}'.")
	}

	return $sha1;
}

sub get_rev_auto ($) {
	my $automator=shift;

	my @results = $automator-&gt;call("get_base_revision_id");

	my $sha1 = $results[0];
	($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
	if (! $sha1) {
		debug("Unable to get base revision for '$config{srcdir}'.")
	}

	return $sha1;
}

sub mtn_merge ($$$$) {
	my $leftRev=shift;
	my $rightRev=shift;
	my $branch=shift;
	my $author=shift;
    
	my $mergeRev;

	my $child = open(MTNMERGE, "-|");
	if (! $child) {
		open STDERR, "&gt;&amp;STDOUT";
		exec("mtn", "--root=$config{mtnrootdir}",
		     "explicit_merge", $leftRev, $rightRev,
		     $branch, "--author", $author, "--key", 
		     $config{mtnkey}) || error("mtn merge failed to run");
	}

	while (&lt;MTNMERGE&gt;) {
		if (/^mtn.\s.merged.\s($sha1_pattern)$/) {
			$mergeRev=$1;
		}
	}
	
	close MTNMERGE || return undef;

	debug("merged $leftRev, $rightRev to make $mergeRev");

	return $mergeRev;
}

sub commit_file_to_new_rev ($$$$$$$$) {
	my $automator=shift;
	my $wsfilename=shift;
	my $oldFileID=shift;
	my $newFileContents=shift;
	my $oldrev=shift;
	my $branch=shift;
	my $author=shift;
	my $message=shift;
	
	#store the file
	my ($out, $err) = $automator-&gt;call("put_file", $oldFileID, $newFileContents);
	my ($newFileID) = ($out =~ m/^($sha1_pattern)$/);
	error("Failed to store file data for $wsfilename in repository")
		if (! defined $newFileID || length $newFileID != 40);

	# get the mtn filename rather than the workspace filename
	($out, $err) = $automator-&gt;call("get_corresponding_path", $oldrev, $wsfilename, $oldrev);
	my ($filename) = ($out =~ m/^file "(.*)"$/);
	error("Couldn't find monotone repository path for file $wsfilename") if (! $filename);
	debug("Converted ws filename of $wsfilename to repos filename of $filename");

	# then stick in a new revision for this file
	my $manifest = "format_version \"1\"\n\n".
	               "new_manifest [0000000000000000000000000000000000000000]\n\n".
	               "old_revision [$oldrev]\n\n".
	               "patch \"$filename\"\n".
	               " from [$oldFileID]\n".
	               "   to [$newFileID]\n";
	($out, $err) = $automator-&gt;call("put_revision", $manifest);
	my ($newRevID) = ($out =~ m/^($sha1_pattern)$/);
	error("Unable to make new monotone repository revision")
		if (! defined $newRevID || length $newRevID != 40);
	debug("put revision: $newRevID");
	
	# now we need to add certs for this revision...
	# author, branch, changelog, date
	$automator-&gt;call("cert", $newRevID, "author", $author);
	$automator-&gt;call("cert", $newRevID, "branch", $branch);
	$automator-&gt;call("cert", $newRevID, "changelog", $message);
	$automator-&gt;call("cert", $newRevID, "date",
		time2str("%Y-%m-%dT%T", time, "UTC"));
	
	debug("Added certs for rev: $newRevID");
	return $newRevID;
}

sub read_certs ($$) {
	my $automator=shift;
	my $rev=shift;
	my @results = $automator-&gt;call("certs", $rev);
	my @ret;

	my $line = $results[0];
	while ($line =~ m/\s+key\s"(.*?)"\nsignature\s"(ok|bad|unknown)"\n\s+name\s"(.*?)"\n\s+value\s"(.*?)"\n\s+trust\s"(trusted|untrusted)"\n/sg) {
		push @ret, {
			key =&gt; $1,
			signature =&gt; $2,
			name =&gt; $3,
			value =&gt; $4,
			trust =&gt; $5,
		};
	}

	return @ret;
}

sub get_changed_files ($$) {
	my $automator=shift;
	my $rev=shift;
	
	my @results = $automator-&gt;call("get_revision", $rev);
	my $changes=$results[0];

	my @ret;
	my %seen = ();
	
	while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?&lt;!\\)"\n/sg) {
		my $file = $2;
		# don't add the same file multiple times
		if (! $seen{$file}) {
			push @ret, $file;
			$seen{$file} = 1;
		}
	}
	
	return @ret;
}

sub rcs_update () {
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (defined($config{mtnsync}) &amp;&amp; $config{mtnsync}) {
		if (system("mtn", "--root=$config{mtnrootdir}", "sync",
		           "--quiet", "--ticker=none", 
		           "--key", $config{mtnkey}) != 0) {
			debug("monotone sync failed before update");
		}
	}

	if (system("mtn", "--root=$config{mtnrootdir}", "update", "--quiet") != 0) {
		debug("monotone update failed");
	}
}

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

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# For monotone, return the revision of the file when
	# editing begins.
	return get_rev();
}

sub rcs_commit ($$$;$$) {
	# Tries to commit the page; returns undef on _success_ and
	# a version of the page with the rcs's conflict markers on failure.
	# The file is relative to the srcdir.
	my $file=shift;
	my $message=shift;
	my $rcstoken=shift;
	my $user=shift;
	my $ipaddr=shift;
	my $author;

	if (defined $user) {
		$author="Web user: " . $user;
	}
	elsif (defined $ipaddr) {
		$author="Web IP: " . $ipaddr;
	}
	else {
		$author="Web: Anonymous";
	}

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my ($oldrev)= $rcstoken=~ m/^($sha1_pattern)$/; # untaint
	my $rev = get_rev();
	if (defined $rev &amp;&amp; defined $oldrev &amp;&amp; $rev ne $oldrev) {
		my $automator = Monotone-&gt;new();
		$automator-&gt;open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});

		# Something has been committed, has this file changed?
		my ($out, $err);
		$automator-&gt;setOpts("r", $oldrev, "r", $rev);
		($out, $err) = $automator-&gt;call("content_diff", $file);
		debug("Problem committing $file") if ($err ne "");
		my $diff = $out;
		
		if ($diff) {
			# Commit a revision with just this file changed off
			# the old revision.
			#
			# first get the contents
			debug("File changed: forming branch");
			my $newfile=readfile("$config{srcdir}/$file");
			
			# then get the old content ID from the diff
			if ($diff !~ m/^---\s$file\s+($sha1_pattern)$/m) {
				error("Unable to find previous file ID for $file");
			}
			my $oldFileID = $1;

			# get the branch we're working in
			($out, $err) = $automator-&gt;call("get_option", "branch");
			chomp $out;
			error("Illegal branch name in monotone workspace") if ($out !~ m/^([-\@\w\.]+)$/);
			my $branch = $1;

			# then put the new content into the DB (and record the new content ID)
			my $newRevID = commit_file_to_new_rev($automator, $file, $oldFileID, $newfile, $oldrev, $branch, $author, $message);

			$automator-&gt;close();

			# if we made it to here then the file has been committed... revert the local copy
			if (system("mtn", "--root=$config{mtnrootdir}", "revert", $file) != 0) {
				debug("Unable to revert $file after merge on conflicted commit!");
			}
			debug("Divergence created! Attempting auto-merge.");

			# see if it will merge cleanly
			$ENV{MTN_MERGE}="fail";
			my $mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
			$ENV{MTN_MERGE}="";

			# push any changes so far
			if (defined($config{mtnsync}) &amp;&amp; $config{mtnsync}) {
				if (system("mtn", "--root=$config{mtnrootdir}", "push", "--quiet", "--ticker=none", "--key", $config{mtnkey}) != 0) {
					debug("monotone push failed");
				}
			}
			
			if (defined($mergeResult)) {
				# everything is merged - bring outselves up to date
				if (system("mtn", "--root=$config{mtnrootdir}",
				           "update", "-r", $mergeResult) != 0) {
					debug("Unable to update to rev $mergeResult after merge on conflicted commit!");
				}
			}
			else {
				debug("Auto-merge failed.  Using diff-merge to add conflict markers.");
				
				$ENV{MTN_MERGE}="diffutils";
				$ENV{MTN_MERGE_DIFFUTILS}="partial=true";
				$mergeResult = mtn_merge($newRevID, $rev, $branch, $author);
				$ENV{MTN_MERGE}="";
				$ENV{MTN_MERGE_DIFFUTILS}="";
				
				if (!defined($mergeResult)) {
					debug("Unable to insert conflict markers!");
					error("Your commit succeeded. Unfortunately, someone else committed something to the same ".
				 		"part of the wiki at the same time. Both versions are stored in the monotone repository, ".
						"but at present the different versions cannot be reconciled through the web interface. ".
						"Please use the non-web interface to resolve the conflicts.");
				}
				
				if (system("mtn", "--root=$config{mtnrootdir}",
				           "update", "-r", $mergeResult) != 0) {
					debug("Unable to update to rev $mergeResult after conflict-enhanced merge on conflicted commit!");
				}
				
				# return "conflict enhanced" file to the user
				# for cleanup note, this relies on the fact
				# that ikiwiki seems to call rcs_prepedit()
				# again after we return
				return readfile("$config{srcdir}/$file");
			}
			return undef;
		}
		$automator-&gt;close();
	}

	# If we reached here then the file we're looking at hasn't changed
	# since $oldrev. Commit it.

	if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
	           "--author", $author, "--key", $config{mtnkey}, "-m",
		   IkiWiki::possibly_foolish_untaint($message), $file) != 0) {
		debug("Traditional commit failed! Returning data as conflict.");
		my $conflict=readfile("$config{srcdir}/$file");
		if (system("mtn", "--root=$config{mtnrootdir}", "revert",
		           "--quiet", $file) != 0) {
			debug("monotone revert failed");
		}
		return $conflict;
	}
	if (defined($config{mtnsync}) &amp;&amp; $config{mtnsync}) {
		if (system("mtn", "--root=$config{mtnrootdir}", "push",
		           "--quiet", "--ticker=none", "--key",
		           $config{mtnkey}) != 0) {
			debug("monotone push failed");
		}
	}

	return undef # success
}

sub rcs_commit_staged ($$$) {
	# Commits all staged changes. Changes can be staged using rcs_add,
	# rcs_remove, and rcs_rename.
	my ($message, $user, $ipaddr)=@_;
	
	# Note - this will also commit any spurious changes that happen to be
	# lying around in the working copy.  There shouldn't be any, but...
	
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $author;

	if (defined $user) {
		$author="Web user: " . $user;
	}
	elsif (defined $ipaddr) {
		$author="Web IP: " . $ipaddr;
	}
	else {
		$author="Web: Anonymous";
	}

	if (system("mtn", "--root=$config{mtnrootdir}", "commit", "--quiet",
	           "--author", $author, "--key", $config{mtnkey}, "-m",
		   IkiWiki::possibly_foolish_untaint($message)) != 0) {
		error("Monotone commit failed");
	}
}

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

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (system("mtn", "--root=$config{mtnrootdir}", "add", "--quiet",
	           $file) != 0) {
		error("Monotone add failed");
	}
}

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

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# Note: it is difficult to undo a remove in Monotone at the moment.
	# Until this is fixed, it might be better to make 'rm' move things
	# into an attic, rather than actually remove them.
	# To resurrect a file, you currently add a new file with the contents
	# you want it to have.  This loses all connectivity and automated
	# merging with the 'pre-delete' versions of the file.

	if (system("mtn", "--root=$config{mtnrootdir}", "rm", "--quiet",
	           $file) != 0) {
		error("Monotone remove failed");
	}
}

sub rcs_rename ($$) {
	my ($src, $dest) = @_;

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	if (system("mtn", "--root=$config{mtnrootdir}", "rename", "--quiet",
	           $src, $dest) != 0) {
		error("Monotone rename failed");
	}
}

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

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	# use log --brief to get a list of revs, as this
	# gives the results in a nice order
	# (otherwise we'd have to do our own date sorting)

	my @revs;

	my $child = open(MTNLOG, "-|");
	if (! $child) {
		exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
		     "--brief", "--last=$num") || error("mtn log failed to run");
	}

	while (my $line = &lt;MTNLOG&gt;) {
		if ($line =~ m/^($sha1_pattern)/) {
			push @revs, $1;
		}
	}
	close MTNLOG || debug("mtn log exited $?");

	my $automator = Monotone-&gt;new();
	$automator-&gt;open(undef, $config{mtnrootdir});

	while (@revs != 0) {
		my $rev = shift @revs;
		# first go through and figure out the messages, etc

		my $certs = [read_certs($automator, $rev)];
		
		my $user;
		my $when;
		my $committype;
		my (@pages, @message);
		
		foreach my $cert (@$certs) {
			if ($cert-&gt;{signature} eq "ok" &amp;&amp;
			    $cert-&gt;{trust} eq "trusted") {
				if ($cert-&gt;{name} eq "author") {
					$user = $cert-&gt;{value};
				 	# detect the source of the commit
					# from the changelog
					if ($cert-&gt;{key} eq $config{mtnkey}) {
						$committype = "web";
					} else {
						$committype = "monotone";
					}
				} elsif ($cert-&gt;{name} eq "date") {
					$when = str2time($cert-&gt;{value}, 'UTC');
				} elsif ($cert-&gt;{name} eq "changelog") {
					my $messageText = $cert-&gt;{value};
					# split the changelog into multiple
					# lines
					foreach my $msgline (split(/\n/, $messageText)) {
						push @message, { line =&gt; $msgline };
					}
				}
			}
		}
		
		my @changed_files = get_changed_files($automator, $rev);
		my $file;
		
		my ($out, $err) = $automator-&gt;call("parents", $rev);
		my @parents = ($out =~ m/^($sha1_pattern)$/);
		my $parent = $parents[0];

		foreach $file (@changed_files) {
			next unless length $file;
			
			if (defined $config{diffurl} and (@parents == 1)) {
				my $diffurl=$config{diffurl};
				$diffurl=~s/\[\[r1\]\]/$parent/g;
				$diffurl=~s/\[\[r2\]\]/$rev/g;
				$diffurl=~s/\[\[file\]\]/$file/g;
				push @pages, {
					page =&gt; pagename($file),
					diffurl =&gt; $diffurl,
				};
			}
			else {
				push @pages, {
					page =&gt; pagename($file),
				}
			}
		}
		
		push @ret, {
			rev =&gt; $rev,
			user =&gt; $user,
			committype =&gt; $committype,
			when =&gt; $when,
			message =&gt; [@message],
			pages =&gt; [@pages],
		} if @pages;
	}

	$automator-&gt;close();

	return @ret;
}

sub rcs_diff ($) {
	my $rev=shift;
	my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
	
	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $child = open(MTNDIFF, "-|");
	if (! $child) {
		exec("mtn", "diff", "--root=$config{mtnrootdir}", "-r", "p:".$sha1, "-r", $sha1) || error("mtn diff $sha1 failed to run");
	}

	my (@lines) = &lt;MTNDIFF&gt;;

	close MTNDIFF || debug("mtn diff $sha1 exited $?");

	if (wantarray) {
		return @lines;
	}
	else {
		return join("", @lines);
	}
}

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

	chdir $config{srcdir}
	    or error("Cannot chdir to $config{srcdir}: $!");

	my $child = open(MTNLOG, "-|");
	if (! $child) {
		exec("mtn", "log", "--root=$config{mtnrootdir}", "--no-graph",
		     "--brief", $file) || error("mtn log $file failed to run");
	}

	my $firstRev;
	while (&lt;MTNLOG&gt;) {
		if (/^($sha1_pattern)/) {
			$firstRev=$1;
		}
	}
	close MTNLOG || debug("mtn log $file exited $?");

	if (! defined $firstRev) {
		debug "failed to parse mtn log for $file";
		return 0;
	}

	my $automator = Monotone-&gt;new();
	$automator-&gt;open(undef, $config{mtnrootdir});

	my $certs = [read_certs($automator, $firstRev)];

	$automator-&gt;close();

	my $date;

	foreach my $cert (@$certs) {
		if ($cert-&gt;{signature} eq "ok" &amp;&amp; $cert-&gt;{trust} eq "trusted") {
			if ($cert-&gt;{name} eq "date") {
				$date = $cert-&gt;{value};
			}
		}
	}

	if (! defined $date) {
		debug "failed to find date cert for revision $firstRev when looking for creation time of $file";
		return 0;
	}

	$date=str2time($date, 'UTC');
	debug("found ctime ".localtime($date)." for $file");
	return $date;
}

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