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

use warnings;
use strict;
use IkiWiki;
use Encode;
use open qw{:utf8 :std};

sub import {
	hook(type =&gt; "checkconfig", id =&gt; "mercurial", call =&gt; \&amp;checkconfig);
	hook(type =&gt; "getsetup", id =&gt; "mercurial", 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 (exists $config{mercurial_wrapper} &amp;&amp; length $config{mercurial_wrapper}) {
		push @{$config{wrappers}}, {
			wrapper =&gt; $config{mercurial_wrapper},
			wrappermode =&gt; (defined $config{mercurial_wrappermode} ? $config{mercurial_wrappermode} : "06755"),
		};
	}
}

sub getsetup () {
	return
		plugin =&gt; {
			safe =&gt; 0, # rcs plugin
			rebuild =&gt; undef,
		},
		mercurial_wrapper =&gt; {
			type =&gt; "string",
			#example =&gt; # FIXME add example
			description =&gt; "mercurial post-commit hook to generate",
			safe =&gt; 0, # file
			rebuild =&gt; 0,
		},
		mercurial_wrappermode =&gt; {
			type =&gt; "string",
			example =&gt; '06755',
			description =&gt; "mode for mercurial_wrapper (can safely be made suid)",
			safe =&gt; 0,
			rebuild =&gt; 0,
		},
		historyurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://example.com:8000/log/tip/[[file]]",
			description =&gt; "url to hg serve'd repository, to show file history ([[file]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
		diffurl =&gt; {
			type =&gt; "string",
			example =&gt; "http://localhost:8000/?fd=[[r2]];file=[[file]]",
			description =&gt; "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
			safe =&gt; 1,
			rebuild =&gt; 1,
		},
}

sub mercurial_log ($) {
	my $out = shift;
	my @infos;

	while (&lt;$out&gt;) {
		my $line = $_;
		my ($key, $value);

		if (/^description:/) {
			$key = "description";
			$value = "";

			# slurp everything as the description text 
			# until the next changeset
			while (&lt;$out&gt;) {
				if (/^changeset: /) {
					$line = $_;
					last;
				}

				$value .= $_;
			}

			local $/ = "";
			chomp $value;
			$infos[$#infos]{$key} = $value;
		}

		chomp $line;
	        ($key, $value) = split /: +/, $line, 2;

		if ($key eq "changeset") {
			push @infos, {};

			# remove the revision index, which is strictly 
			# local to the repository
			$value =~ s/^\d+://;
		}

		$infos[$#infos]{$key} = $value;
	}
	close $out;

	return @infos;
}

sub rcs_update () {
	my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
	if (system(@cmdline) != 0) {
		warn "'@cmdline' failed: $!";
	}
}

sub rcs_prepedit ($) {
	return "";
}

sub rcs_commit ($$$;$$) {
	my ($file, $message, $rcstoken, $user, $ipaddr) = @_;

	if (defined $user) {
		$user = IkiWiki::possibly_foolish_untaint($user);
	}
	elsif (defined $ipaddr) {
		$user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
	}
	else {
		$user = "Anonymous";
	}

	$message = IkiWiki::possibly_foolish_untaint($message);
	if (! length $message) {
		$message = "no message given";
	}

	my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit", 
	               "-m", $message, "-u", $user);
	if (system(@cmdline) != 0) {
		warn "'@cmdline' 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)=@_;
	
	error("rcs_commit_staged not implemented for mercurial"); # TODO
}

sub rcs_add ($) {
	my ($file) = @_;

	my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
	if (system(@cmdline) != 0) {
		warn "'@cmdline' failed: $!";
	}
}

sub rcs_remove ($) {
	my ($file) = @_;

	error("rcs_remove not implemented for mercurial"); # TODO
}

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

	error("rcs_rename not implemented for mercurial"); # TODO
}

sub rcs_recentchanges ($) {
	my ($num) = @_;

	my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
		"--style", "default");
	open (my $out, "@cmdline |");

	eval q{use Date::Parse};
	error($@) if $@;

	my @ret;
	foreach my $info (mercurial_log($out)) {
		my @pages = ();
		my @message = ();
        
		foreach my $msgline (split(/\n/, $info-&gt;{description})) {
			push @message, { line =&gt; $msgline };
		}

		foreach my $file (split / /,$info-&gt;{files}) {
			my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
			$diffurl =~ s/\[\[file\]\]/$file/go;
			$diffurl =~ s/\[\[r2\]\]/$info-&gt;{changeset}/go;

			push @pages, {
				page =&gt; pagename($file),
				diffurl =&gt; $diffurl,
			};
		}

		my $user = $info-&gt;{"user"};
		$user =~ s/\s*&lt;.*&gt;\s*$//;
		$user =~ s/^\s*//;

		push @ret, {
			rev        =&gt; $info-&gt;{"changeset"},
			user       =&gt; $user,
			committype =&gt; "hg",
			when       =&gt; str2time($info-&gt;{"date"}),
			message    =&gt; [@message],
			pages      =&gt; [@pages],
		};
	}

	return @ret;
}

sub rcs_diff ($) {
	# TODO
}

sub rcs_getctime ($) {
	my ($file) = @_;

	# XXX filename passes through the shell here, should try to avoid
	# that just in case
	my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1', 
		"--style", "default", "$config{srcdir}/$file");
	open (my $out, "@cmdline |");

	my @log = mercurial_log($out);

	if (length @log &lt; 1) {
		return 0;
	}

	eval q{use Date::Parse};
	error($@) if $@;
	
	my $ctime = str2time($log[0]-&gt;{"date"});
	return $ctime;
}

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