summaryrefslogtreecommitdiff
path: root/IkiWiki/Plugin
diff options
context:
space:
mode:
authorJoey Hess <joey@kodama.kitenet.net>2008-07-26 22:27:08 -0400
committerJoey Hess <joey@kodama.kitenet.net>2008-07-26 22:27:24 -0400
commit14cd75746a2c73b50548b6fdb3583d536b6ef9bd (patch)
tree4e9390041f63ced41683be40a8946078af7ab92d /IkiWiki/Plugin
parentbb888daea0786a69d017b7617920827b637e954a (diff)
Version control backends promoted to first-class plugins
Diffstat (limited to 'IkiWiki/Plugin')
-rw-r--r--IkiWiki/Plugin/bzr.pm264
-rw-r--r--IkiWiki/Plugin/git.pm538
-rw-r--r--IkiWiki/Plugin/mercurial.pm241
-rw-r--r--IkiWiki/Plugin/monotone.pm678
-rw-r--r--IkiWiki/Plugin/norcs.pm99
-rw-r--r--IkiWiki/Plugin/svn.pm366
-rw-r--r--IkiWiki/Plugin/tla.pm271
7 files changed, 2457 insertions, 0 deletions
diff --git a/IkiWiki/Plugin/bzr.pm b/IkiWiki/Plugin/bzr.pm
new file mode 100644
index 000000000..5df522f6e
--- /dev/null
+++ b/IkiWiki/Plugin/bzr.pm
@@ -0,0 +1,264 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+use Encode;
+use open qw{:utf8 :std};
+
+hook(type => "checkconfig", id => "bzr", call => sub { #{{{
+ if (! defined $config{diffurl}) {
+ $config{diffurl}="";
+ }
+ if (length $config{bzr_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{bzr_wrapper},
+ wrappermode => (defined $config{bzr_wrappermode} ? $config{bzr_wrappermode} : "06755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "bzr", call => sub { #{{{
+ return
+ bzr_wrapper => {
+ type => "string",
+ #example => "", # FIXME add example
+ description => "bzr post-commit executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ bzr_wrappermode => {
+ type => "string",
+ example => '06755',
+ description => "mode for bzr_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ #example => "", # FIXME add example
+ description => "url to show file history, using loggerhead ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ example => "http://example.com/revision?start_revid=[[r2]]#[[file]]-s",
+ description => "url to view a diff, using loggerhead ([[file]] and [[r2]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+}); #}}}
+
+sub bzr_log ($) { #{{{
+ my $out = shift;
+ my @infos = ();
+ my $key = undef;
+
+ while (<$out>) {
+ my $line = $_;
+ my ($value);
+ if ($line =~ /^message:/) {
+ $key = "message";
+ $infos[$#infos]{$key} = "";
+ }
+ elsif ($line =~ /^(modified|added|renamed|renamed and modified|removed):/) {
+ $key = "files";
+ unless (defined($infos[$#infos]{$key})) { $infos[$#infos]{$key} = ""; }
+ }
+ elsif (defined($key) and $line =~ /^ (.*)/) {
+ $infos[$#infos]{$key} .= "$1\n";
+ }
+ elsif ($line eq "------------------------------------------------------------\n") {
+ $key = undef;
+ push (@infos, {});
+ }
+ else {
+ chomp $line;
+ ($key, $value) = split /: +/, $line, 2;
+ $infos[$#infos]{$key} = $value;
+ }
+ }
+ close $out;
+
+ return @infos;
+} #}}}
+
+sub rcs_update () { #{{{
+ my @cmdline = ("bzr", "update", "--quiet", $config{srcdir});
+ if (system(@cmdline) != 0) {
+ warn "'@cmdline' failed: $!";
+ }
+} #}}}
+
+sub rcs_prepedit ($) { #{{{
+ return "";
+} #}}}
+
+sub bzr_author ($$) { #{{{
+ my ($user, $ipaddr) = @_;
+
+ if (defined $user) {
+ return possibly_foolish_untaint($user);
+ }
+ elsif (defined $ipaddr) {
+ return "Anonymous from ".possibly_foolish_untaint($ipaddr);
+ }
+ else {
+ return "Anonymous";
+ }
+} #}}}
+
+sub rcs_commit ($$$;$$) { #{{{
+ my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
+
+ $user = bzr_author($user, $ipaddr);
+
+ $message = possibly_foolish_untaint($message);
+ if (! length $message) {
+ $message = "no message given";
+ }
+
+ my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
+ $config{srcdir}."/".$file);
+ 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)=@_;
+
+ $user = bzr_author($user, $ipaddr);
+
+ $message = possibly_foolish_untaint($message);
+ if (! length $message) {
+ $message = "no message given";
+ }
+
+ my @cmdline = ("bzr", "commit", "--quiet", "-m", $message, "--author", $user,
+ $config{srcdir});
+ if (system(@cmdline) != 0) {
+ warn "'@cmdline' failed: $!";
+ }
+
+ return undef; # success
+} #}}}
+
+sub rcs_add ($) { # {{{
+ my ($file) = @_;
+
+ my @cmdline = ("bzr", "add", "--quiet", "$config{srcdir}/$file");
+ if (system(@cmdline) != 0) {
+ warn "'@cmdline' failed: $!";
+ }
+} #}}}
+
+sub rcs_remove ($) { # {{{
+ my ($file) = @_;
+
+ my @cmdline = ("bzr", "rm", "--force", "--quiet", "$config{srcdir}/$file");
+ if (system(@cmdline) != 0) {
+ warn "'@cmdline' failed: $!";
+ }
+} #}}}
+
+sub rcs_rename ($$) { # {{{
+ my ($src, $dest) = @_;
+
+ my $parent = dirname($dest);
+ if (system("bzr", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
+ warn("bzr add $parent failed\n");
+ }
+
+ my @cmdline = ("bzr", "mv", "--quiet", "$config{srcdir}/$src", "$config{srcdir}/$dest");
+ if (system(@cmdline) != 0) {
+ warn "'@cmdline' failed: $!";
+ }
+} #}}}
+
+sub rcs_recentchanges ($) { #{{{
+ my ($num) = @_;
+
+ my @cmdline = ("bzr", "log", "-v", "--show-ids", "--limit", $num,
+ $config{srcdir});
+ open (my $out, "@cmdline |");
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+
+ my @ret;
+ foreach my $info (bzr_log($out)) {
+ my @pages = ();
+ my @message = ();
+
+ foreach my $msgline (split(/\n/, $info->{message})) {
+ push @message, { line => $msgline };
+ }
+
+ foreach my $file (split(/\n/, $info->{files})) {
+ my ($filename, $fileid) = ($file =~ /^(.*?) +([^ ]+)$/);
+
+ # Skip directories
+ next if ($filename =~ /\/$/);
+
+ # Skip source name in renames
+ $filename =~ s/^.* => //;
+
+ my $diffurl = $config{'diffurl'};
+ $diffurl =~ s/\[\[file\]\]/$filename/go;
+ $diffurl =~ s/\[\[file-id\]\]/$fileid/go;
+ $diffurl =~ s/\[\[r2\]\]/$info->{revno}/go;
+
+ push @pages, {
+ page => pagename($filename),
+ diffurl => $diffurl,
+ };
+ }
+
+ my $user = $info->{"committer"};
+ if (defined($info->{"author"})) { $user = $info->{"author"}; }
+ $user =~ s/\s*<.*>\s*$//;
+ $user =~ s/^\s*//;
+
+ push @ret, {
+ rev => $info->{"revno"},
+ user => $user,
+ committype => "bzr",
+ when => time - str2time($info->{"timestamp"}),
+ message => [@message],
+ pages => [@pages],
+ };
+ }
+
+ return @ret;
+} #}}}
+
+sub rcs_getctime ($) { #{{{
+ my ($file) = @_;
+
+ # XXX filename passes through the shell here, should try to avoid
+ # that just in case
+ my @cmdline = ("bzr", "log", "--limit", '1', "$config{srcdir}/$file");
+ open (my $out, "@cmdline |");
+
+ my @log = bzr_log($out);
+
+ if (length @log < 1) {
+ return 0;
+ }
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+
+ my $ctime = str2time($log[0]->{"timestamp"});
+ return $ctime;
+} #}}}
+
+1
diff --git a/IkiWiki/Plugin/git.pm b/IkiWiki/Plugin/git.pm
new file mode 100644
index 000000000..6c9aca650
--- /dev/null
+++ b/IkiWiki/Plugin/git.pm
@@ -0,0 +1,538 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+use Encode;
+use open qw{:utf8 :std};
+
+my $sha1_pattern = qr/[0-9a-fA-F]{40}/; # pattern to validate Git sha1sums
+my $dummy_commit_msg = 'dummy commit'; # message to skip in recent changes
+
+hook(type => "checkconfig", id => "git", call => sub { #{{{
+ if (! defined $config{diffurl}) {
+ $config{diffurl}="";
+ }
+ if (! defined $config{gitorigin_branch}) {
+ $config{gitorigin_branch}="origin";
+ }
+ if (! defined $config{gitmaster_branch}) {
+ $config{gitmaster_branch}="master";
+ }
+ if (length $config{git_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{git_wrapper},
+ wrappermode => (defined $config{git_wrappermode} ? $config{git_wrappermode} : "06755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "git", call => sub { #{{{
+ return
+ git_wrapper => {
+ type => "string",
+ example => "/git/wiki.git/hooks/post-update",
+ description => "git post-update executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ git_wrappermode => {
+ type => "string",
+ example => '06755',
+ description => "mode for git_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=history;f=[[file]]",
+ description => "gitweb url to show file history ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ example => "http://git.example.com/gitweb.cgi?p=wiki.git;a=blobdiff;h=[[sha1_to]];hp=[[sha1_from]];hb=[[sha1_parent]];f=[[file]]",
+ description => "gitweb url to show a diff ([[sha1_to]], [[sha1_from]], [[sha1_parent]], and [[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ gitorigin_branch => {
+ type => "string",
+ example => "origin",
+ description => "where to pull and push changes (set to empty string to disable)",
+ safe => 0, # paranoia
+ rebuild => 0,
+ },
+ gitmaster_branch => {
+ type => "string",
+ example => "master",
+ description => "branch that the wiki is stored in",
+ safe => 0, # paranoia
+ rebuild => 0,
+ },
+}); #}}}
+
+sub _safe_git (&@) { #{{{
+ # Start a child process safely without resorting /bin/sh.
+ # Return command output or success state (in scalar context).
+
+ my ($error_handler, @cmdline) = @_;
+
+ my $pid = open my $OUT, "-|";
+
+ error("Cannot fork: $!") if !defined $pid;
+
+ if (!$pid) {
+ # In child.
+ # Git commands want to be in wc.
+ chdir $config{srcdir}
+ or error("Cannot chdir to $config{srcdir}: $!");
+ exec @cmdline or error("Cannot exec '@cmdline': $!");
+ }
+ # In parent.
+
+ my @lines;
+ while (<$OUT>) {
+ chomp;
+ push @lines, $_;
+ }
+
+ close $OUT;
+
+ $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
+
+ return wantarray ? @lines : ($? == 0);
+}
+# Convenient wrappers.
+sub run_or_die ($@) { _safe_git(\&error, @_) }
+sub run_or_cry ($@) { _safe_git(sub { warn @_ }, @_) }
+sub run_or_non ($@) { _safe_git(undef, @_) }
+#}}}
+
+sub _merge_past ($$$) { #{{{
+ # Unlike with Subversion, Git cannot make a 'svn merge -rN:M file'.
+ # Git merge commands work with the committed changes, except in the
+ # implicit case of '-m' of git checkout(1). So we should invent a
+ # kludge here. In principle, we need to create a throw-away branch
+ # in preparing for the merge itself. Since branches are cheap (and
+ # branching is fast), this shouldn't cost high.
+ #
+ # The main problem is the presence of _uncommitted_ local changes. One
+ # possible approach to get rid of this situation could be that we first
+ # make a temporary commit in the master branch and later restore the
+ # initial state (this is possible since Git has the ability to undo a
+ # commit, i.e. 'git reset --soft HEAD^'). The method can be summarized
+ # as follows:
+ #
+ # - create a diff of HEAD:current-sha1
+ # - dummy commit
+ # - create a dummy branch and switch to it
+ # - rewind to past (reset --hard to the current-sha1)
+ # - apply the diff and commit
+ # - switch to master and do the merge with the dummy branch
+ # - make a soft reset (undo the last commit of master)
+ #
+ # The above method has some drawbacks: (1) it needs a redundant commit
+ # just to get rid of local changes, (2) somewhat slow because of the
+ # required system forks. Until someone points a more straight method
+ # (which I would be grateful) I have implemented an alternative method.
+ # In this approach, we hide all the modified files from Git by renaming
+ # them (using the 'rename' builtin) and later restore those files in
+ # the throw-away branch (that is, we put the files themselves instead
+ # of applying a patch).
+
+ my ($sha1, $file, $message) = @_;
+
+ my @undo; # undo stack for cleanup in case of an error
+ my $conflict; # file content with conflict markers
+
+ eval {
+ # Hide local changes from Git by renaming the modified file.
+ # Relative paths must be converted to absolute for renaming.
+ my ($target, $hidden) = (
+ "$config{srcdir}/${file}", "$config{srcdir}/${file}.${sha1}"
+ );
+ rename($target, $hidden)
+ or error("rename '$target' to '$hidden' failed: $!");
+ # Ensure to restore the renamed file on error.
+ push @undo, sub {
+ return if ! -e "$hidden"; # already renamed
+ rename($hidden, $target)
+ or warn "rename '$hidden' to '$target' failed: $!";
+ };
+
+ my $branch = "throw_away_${sha1}"; # supposed to be unique
+
+ # Create a throw-away branch and rewind backward.
+ push @undo, sub { run_or_cry('git', 'branch', '-D', $branch) };
+ run_or_die('git', 'branch', $branch, $sha1);
+
+ # Switch to throw-away branch for the merge operation.
+ push @undo, sub {
+ if (!run_or_cry('git', 'checkout', $config{gitmaster_branch})) {
+ run_or_cry('git', 'checkout','-f',$config{gitmaster_branch});
+ }
+ };
+ run_or_die('git', 'checkout', $branch);
+
+ # Put the modified file in _this_ branch.
+ rename($hidden, $target)
+ or error("rename '$hidden' to '$target' failed: $!");
+
+ # _Silently_ commit all modifications in the current branch.
+ run_or_non('git', 'commit', '-m', $message, '-a');
+ # ... and re-switch to master.
+ run_or_die('git', 'checkout', $config{gitmaster_branch});
+
+ # Attempt to merge without complaining.
+ if (!run_or_non('git', 'pull', '--no-commit', '.', $branch)) {
+ $conflict = readfile($target);
+ run_or_die('git', 'reset', '--hard');
+ }
+ };
+ my $failure = $@;
+
+ # Process undo stack (in reverse order). By policy cleanup
+ # actions should normally print a warning on failure.
+ while (my $handle = pop @undo) {
+ $handle->();
+ }
+
+ error("Git merge failed!\n$failure\n") if $failure;
+
+ return $conflict;
+} #}}}
+
+sub _parse_diff_tree ($@) { #{{{
+ # Parse the raw diff tree chunk and return the info hash.
+ # See git-diff-tree(1) for the syntax.
+
+ my ($prefix, $dt_ref) = @_;
+
+ # End of stream?
+ return if !defined @{ $dt_ref } ||
+ !defined @{ $dt_ref }[0] || !length @{ $dt_ref }[0];
+
+ my %ci;
+ # Header line.
+ while (my $line = shift @{ $dt_ref }) {
+ return if $line !~ m/^(.+) ($sha1_pattern)/;
+
+ my $sha1 = $2;
+ $ci{'sha1'} = $sha1;
+ last;
+ }
+
+ # Identification lines for the commit.
+ while (my $line = shift @{ $dt_ref }) {
+ # Regexps are semi-stolen from gitweb.cgi.
+ if ($line =~ m/^tree ([0-9a-fA-F]{40})$/) {
+ $ci{'tree'} = $1;
+ }
+ elsif ($line =~ m/^parent ([0-9a-fA-F]{40})$/) {
+ # XXX: collecting in reverse order
+ push @{ $ci{'parents'} }, $1;
+ }
+ elsif ($line =~ m/^(author|committer) (.*) ([0-9]+) (.*)$/) {
+ my ($who, $name, $epoch, $tz) =
+ ($1, $2, $3, $4 );
+
+ $ci{ $who } = $name;
+ $ci{ "${who}_epoch" } = $epoch;
+ $ci{ "${who}_tz" } = $tz;
+
+ if ($name =~ m/^[^<]+\s+<([^@>]+)/) {
+ $ci{"${who}_username"} = $1;
+ }
+ elsif ($name =~ m/^([^<]+)\s+<>$/) {
+ $ci{"${who}_username"} = $1;
+ }
+ else {
+ $ci{"${who}_username"} = $name;
+ }
+ }
+ elsif ($line =~ m/^$/) {
+ # Trailing empty line signals next section.
+ last;
+ }
+ }
+
+ debug("No 'tree' seen in diff-tree output") if !defined $ci{'tree'};
+
+ if (defined $ci{'parents'}) {
+ $ci{'parent'} = @{ $ci{'parents'} }[0];
+ }
+ else {
+ $ci{'parent'} = 0 x 40;
+ }
+
+ # Commit message (optional).
+ while ($dt_ref->[0] =~ /^ /) {
+ my $line = shift @{ $dt_ref };
+ $line =~ s/^ //;
+ push @{ $ci{'comment'} }, $line;
+ }
+ shift @{ $dt_ref } if $dt_ref->[0] =~ /^$/;
+
+ # Modified files.
+ while (my $line = shift @{ $dt_ref }) {
+ if ($line =~ m{^
+ (:+) # number of parents
+ ([^\t]+)\t # modes, sha1, status
+ (.*) # file names
+ $}xo) {
+ my $num_parents = length $1;
+ my @tmp = split(" ", $2);
+ my ($file, $file_to) = split("\t", $3);
+ my @mode_from = splice(@tmp, 0, $num_parents);
+ my $mode_to = shift(@tmp);
+ my @sha1_from = splice(@tmp, 0, $num_parents);
+ my $sha1_to = shift(@tmp);
+ my $status = shift(@tmp);
+
+ if ($file =~ m/^"(.*)"$/) {
+ ($file=$1) =~ s/\\([0-7]{1,3})/chr(oct($1))/eg;
+ }
+ $file =~ s/^\Q$prefix\E//;
+ if (length $file) {
+ push @{ $ci{'details'} }, {
+ 'file' => decode_utf8($file),
+ 'sha1_from' => $sha1_from[0],
+ 'sha1_to' => $sha1_to,
+ };
+ }
+ next;
+ };
+ last;
+ }
+
+ return \%ci;
+} #}}}
+
+sub git_commit_info ($;$) { #{{{
+ # Return an array of commit info hashes of num commits (default: 1)
+ # starting from the given sha1sum.
+
+ my ($sha1, $num) = @_;
+
+ $num ||= 1;
+
+ my @raw_lines = run_or_die('git', 'log', "--max-count=$num",
+ '--pretty=raw', '--raw', '--abbrev=40', '--always', '-c',
+ '-r', $sha1, '--', '.');
+ my ($prefix) = run_or_die('git', 'rev-parse', '--show-prefix');
+
+ my @ci;
+ while (my $parsed = _parse_diff_tree(($prefix or ""), \@raw_lines)) {
+ push @ci, $parsed;
+ }
+
+ warn "Cannot parse commit info for '$sha1' commit" if !@ci;
+
+ return wantarray ? @ci : $ci[0];
+} #}}}
+
+sub git_sha1 (;$) { #{{{
+ # Return head sha1sum (of given file).
+
+ my $file = shift || q{--};
+
+ # Ignore error since a non-existing file might be given.
+ my ($sha1) = run_or_non('git', 'rev-list', '--max-count=1', 'HEAD',
+ '--', $file);
+ if ($sha1) {
+ ($sha1) = $sha1 =~ m/($sha1_pattern)/; # sha1 is untainted now
+ } else { debug("Empty sha1sum for '$file'.") }
+ return defined $sha1 ? $sha1 : q{};
+} #}}}
+
+sub rcs_update () { #{{{
+ # Update working directory.
+
+ if (length $config{gitorigin_branch}) {
+ run_or_cry('git', 'pull', $config{gitorigin_branch});
+ }
+} #}}}
+
+sub rcs_prepedit ($) { #{{{
+ # Return the commit sha1sum of the file when editing begins.
+ # This will be later used in rcs_commit if a merge is required.
+
+ my ($file) = @_;
+
+ return git_sha1($file);
+} #}}}
+
+sub rcs_commit ($$$;$$) { #{{{
+ # Try to commit the page; returns undef on _success_ and
+ # a version of the page with the rcs's conflict markers on
+ # failure.
+
+ my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
+
+ # Check to see if the page has been changed by someone else since
+ # rcs_prepedit was called.
+ my $cur = git_sha1($file);
+ my ($prev) = $rcstoken =~ /^($sha1_pattern)$/; # untaint
+
+ if (defined $cur && defined $prev && $cur ne $prev) {
+ my $conflict = _merge_past($prev, $file, $dummy_commit_msg);
+ return $conflict if defined $conflict;
+ }
+
+ rcs_add($file);
+ return rcs_commit_staged($message, $user, $ipaddr);
+} #}}}
+
+sub rcs_commit_staged ($$$) {
+ # Commits all staged changes. Changes can be staged using rcs_add,
+ # rcs_remove, and rcs_rename.
+ my ($message, $user, $ipaddr)=@_;
+
+ # Set the commit author and email to the web committer.
+ my %env=%ENV;
+ if (defined $user || defined $ipaddr) {
+ my $u=defined $user ? $user : $ipaddr;
+ $ENV{GIT_AUTHOR_NAME}=$u;
+ $ENV{GIT_AUTHOR_EMAIL}="$u\@web";
+ }
+
+ # git commit returns non-zero if file has not been really changed.
+ # so we should ignore its exit status (hence run_or_non).
+ $message = possibly_foolish_untaint($message);
+ if (run_or_non('git', 'commit', '--cleanup=verbatim',
+ '-q', '-m', $message)) {
+ if (length $config{gitorigin_branch}) {
+ run_or_cry('git', 'push', $config{gitorigin_branch});
+ }
+ }
+
+ %ENV=%env;
+ return undef; # success
+}
+
+sub rcs_add ($) { # {{{
+ # Add file to archive.
+
+ my ($file) = @_;
+
+ run_or_cry('git', 'add', $file);
+} #}}}
+
+sub rcs_remove ($) { # {{{
+ # Remove file from archive.
+
+ my ($file) = @_;
+
+ run_or_cry('git', 'rm', '-f', $file);
+} #}}}
+
+sub rcs_rename ($$) { # {{{
+ my ($src, $dest) = @_;
+
+ run_or_cry('git', 'mv', '-f', $src, $dest);
+} #}}}
+
+sub rcs_recentchanges ($) { #{{{
+ # List of recent changes.
+
+ my ($num) = @_;
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+
+ my @rets;
+ foreach my $ci (git_commit_info('HEAD', $num)) {
+ # Skip redundant commits.
+ next if ($ci->{'comment'} && @{$ci->{'comment'}}[0] eq $dummy_commit_msg);
+
+ my ($sha1, $when) = (
+ $ci->{'sha1'},
+ $ci->{'author_epoch'}
+ );
+
+ my @pages;
+ foreach my $detail (@{ $ci->{'details'} }) {
+ my $file = $detail->{'file'};
+
+ my $diffurl = $config{'diffurl'};
+ $diffurl =~ s/\[\[file\]\]/$file/go;
+ $diffurl =~ s/\[\[sha1_parent\]\]/$ci->{'parent'}/go;
+ $diffurl =~ s/\[\[sha1_from\]\]/$detail->{'sha1_from'}/go;
+ $diffurl =~ s/\[\[sha1_to\]\]/$detail->{'sha1_to'}/go;
+
+ push @pages, {
+ page => pagename($file),
+ diffurl => $diffurl,
+ };
+ }
+
+ my @messages;
+ my $pastblank=0;
+ foreach my $line (@{$ci->{'comment'}}) {
+ $pastblank=1 if $line eq '';
+ next if $pastblank && $line=~m/^ *(signed[ \-]off[ \-]by[ :]|acked[ \-]by[ :]|cc[ :])/i;
+ push @messages, { line => $line };
+ }
+
+ my $user=$ci->{'author_username'};
+ my $web_commit = ($ci->{'author'} =~ /\@web>/);
+
+ # compatability code for old web commit messages
+ if (! $web_commit &&
+ defined $messages[0] &&
+ $messages[0]->{line} =~ m/$config{web_commit_regexp}/) {
+ $user = defined $2 ? "$2" : "$3";
+ $messages[0]->{line} = $4;
+ $web_commit=1;
+ }
+
+ push @rets, {
+ rev => $sha1,
+ user => $user,
+ committype => $web_commit ? "web" : "git",
+ when => $when,
+ message => [@messages],
+ pages => [@pages],
+ } if @pages;
+
+ last if @rets >= $num;
+ }
+
+ return @rets;
+} #}}}
+
+sub rcs_diff ($) { #{{{
+ my $rev=shift;
+ my ($sha1) = $rev =~ /^($sha1_pattern)$/; # untaint
+ my @lines;
+ foreach my $line (run_or_non("git", "show", $sha1)) {
+ if (@lines || $line=~/^diff --git/) {
+ push @lines, $line."\n";
+ }
+ }
+ if (wantarray) {
+ return @lines;
+ }
+ else {
+ return join("", @lines);
+ }
+} #}}}
+
+sub rcs_getctime ($) { #{{{
+ my $file=shift;
+ # Remove srcdir prefix
+ $file =~ s/^\Q$config{srcdir}\E\/?//;
+
+ my $sha1 = git_sha1($file);
+ my $ci = git_commit_info($sha1);
+ my $ctime = $ci->{'author_epoch'};
+ debug("ctime for '$file': ". localtime($ctime));
+
+ return $ctime;
+} #}}}
+
+1
diff --git a/IkiWiki/Plugin/mercurial.pm b/IkiWiki/Plugin/mercurial.pm
new file mode 100644
index 000000000..3a98e09d8
--- /dev/null
+++ b/IkiWiki/Plugin/mercurial.pm
@@ -0,0 +1,241 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+use Encode;
+use open qw{:utf8 :std};
+
+hook(type => "checkconfig", id => "mercurial", call => sub { #{{{
+ if (! defined $config{diffurl}) {
+ $config{diffurl}="";
+ }
+ if (length $config{mercurial_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{mercurial_wrapper},
+ wrappermode => (defined $config{mercurial_wrappermode} ? $config{mercurial_wrappermode} : "06755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "mercurial", call => sub { #{{{
+ return
+ mercurial_wrapper => {
+ type => "string",
+ #example => # FIXME add example
+ description => "mercurial post-commit executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ mercurial_wrappermode => {
+ type => "string",
+ example => '06755',
+ description => "mode for mercurial_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ example => "http://example.com:8000/log/tip/[[file]]",
+ description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
+ description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+}); #}}}
+
+sub mercurial_log ($) { #{{{
+ my $out = shift;
+ my @infos;
+
+ while (<$out>) {
+ my $line = $_;
+ my ($key, $value);
+
+ if (/^description:/) {
+ $key = "description";
+ $value = "";
+
+ # slurp everything as the description text
+ # until the next changeset
+ while (<$out>) {
+ 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 = possibly_foolish_untaint($user);
+ }
+ elsif (defined $ipaddr) {
+ $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
+ }
+ else {
+ $user = "Anonymous";
+ }
+
+ $message = 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->{description})) {
+ push @message, { line => $msgline };
+ }
+
+ foreach my $file (split / /,$info->{files}) {
+ my $diffurl = $config{'diffurl'};
+ $diffurl =~ s/\[\[file\]\]/$file/go;
+ $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
+
+ push @pages, {
+ page => pagename($file),
+ diffurl => $diffurl,
+ };
+ }
+
+ my $user = $info->{"user"};
+ $user =~ s/\s*<.*>\s*$//;
+ $user =~ s/^\s*//;
+
+ push @ret, {
+ rev => $info->{"changeset"},
+ user => $user,
+ committype => "mercurial",
+ when => str2time($info->{"date"}),
+ message => [@message],
+ pages => [@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 < 1) {
+ return 0;
+ }
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+
+ my $ctime = str2time($log[0]->{"date"});
+ return $ctime;
+} #}}}
+
+1
diff --git a/IkiWiki/Plugin/monotone.pm b/IkiWiki/Plugin/monotone.pm
new file mode 100644
index 000000000..d7e8f296a
--- /dev/null
+++ b/IkiWiki/Plugin/monotone.pm
@@ -0,0 +1,678 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+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
+
+hook(type => "checkconfig", id => "monotone", call => sub { #{{{
+ 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, ">/dev/null";
+ exec("mtn", "version") || error("mtn version failed to run");
+ }
+
+ my $version=undef;
+ while (<MTN>) {
+ if (/^monotone (\d+\.\d+) /) {
+ $version=$1;
+ }
+ }
+
+ close MTN || debug("mtn version exited $?");
+
+ if (!defined($version)) {
+ error("Cannot determine monotone version");
+ }
+ if ($version < 0.38) {
+ error("Monotone version too old, is $version but required 0.38");
+ }
+
+ if (length $config{mtn_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{mtn_wrapper},
+ wrappermode => (defined $config{mtn_wrappermode} ? $config{mtn_wrappermode} : "06755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "monotone", call => sub { #{{{
+ return
+ mtn_wrapper => {
+ type => "string",
+ example => "/srv/mtn/wiki/_MTN/ikiwiki-netsync-hook",
+ description => "monotone netsync hook executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ mtn_wrappermode => {
+ type => "string",
+ example => '06755',
+ description => "mode for mtn_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ mtnkey => {
+ type => "string",
+ example => 'web@example.com',
+ description => "your monotone key",
+ safe => 1,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ example => "http://viewmtn.example.com/branch/head/filechanges/com.example.branch/[[file]]",
+ description => "viewmtn url to show file history ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ example => "http://viewmtn.example.com/revision/diff/[[r1]]/with/[[r2]]/[[file]]",
+ description => "viewmtn url to show a diff ([[r1]], [[r2]], and [[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ mtnsync => {
+ type => "boolean",
+ example => 0,
+ description => "sync on update and commit?",
+ safe => 0, # paranoia
+ rebuild => 0,
+ },
+ mtnrootdir => {
+ type => "string",
+ description => "path to your workspace (defaults to the srcdir; specify if the srcdir is a subdirectory of the workspace)",
+ safe => 0, # path
+ rebuild => 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->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, ">&STDOUT";
+ exec("mtn", "--root=$config{mtnrootdir}",
+ "explicit_merge", $leftRev, $rightRev,
+ $branch, "--author", $author, "--key",
+ $config{mtnkey}) || error("mtn merge failed to run");
+ }
+
+ while (<MTNMERGE>) {
+ 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->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->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->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->call("cert", $newRevID, "author", $author);
+ $automator->call("cert", $newRevID, "branch", $branch);
+ $automator->call("cert", $newRevID, "changelog", $message);
+ $automator->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->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 => $1,
+ signature => $2,
+ name => $3,
+ value => $4,
+ trust => $5,
+ };
+ }
+
+ return @ret;
+} #}}}
+
+sub get_changed_files ($$) { #{{{
+ my $automator=shift;
+ my $rev=shift;
+
+ my @results = $automator->call("get_revision", $rev);
+ my $changes=$results[0];
+
+ my @ret;
+ my %seen = ();
+
+ while ($changes =~ m/\s*(add_file|patch|delete|rename)\s"(.*?)(?<!\\)"\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}) && $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 && defined $oldrev && $rev ne $oldrev) {
+ my $automator = Monotone->new();
+ $automator->open_args("--root", $config{mtnrootdir}, "--key", $config{mtnkey});
+
+ # Something has been committed, has this file changed?
+ my ($out, $err);
+ $automator->setOpts("r", $oldrev, "r", $rev);
+ ($out, $err) = $automator->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->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->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}) && $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->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",
+ 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}) && $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",
+ 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") || error("mtn log failed to run");
+ }
+
+ while (($num >= 0) and (my $line = <MTNLOG>)) {
+ if ($line =~ m/^($sha1_pattern)/) {
+ push @revs, $1;
+ $num -= 1;
+ }
+ }
+ close MTNLOG || debug("mtn log exited $?");
+
+ my $automator = Monotone->new();
+ $automator->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->{signature} eq "ok" &&
+ $cert->{trust} eq "trusted") {
+ if ($cert->{name} eq "author") {
+ $user = $cert->{value};
+ # detect the source of the commit
+ # from the changelog
+ if ($cert->{key} eq $config{mtnkey}) {
+ $committype = "web";
+ } else {
+ $committype = "monotone";
+ }
+ } elsif ($cert->{name} eq "date") {
+ $when = str2time($cert->{value}, 'UTC');
+ } elsif ($cert->{name} eq "changelog") {
+ my $messageText = $cert->{value};
+ # split the changelog into multiple
+ # lines
+ foreach my $msgline (split(/\n/, $messageText)) {
+ push @message, { line => $msgline };
+ }
+ }
+ }
+ }
+
+ my @changed_files = get_changed_files($automator, $rev);
+ my $file;
+
+ my ($out, $err) = $automator->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 => pagename($file),
+ diffurl => $diffurl,
+ };
+ }
+ else {
+ push @pages, {
+ page => pagename($file),
+ }
+ }
+ }
+
+ push @ret, {
+ rev => $rev,
+ user => $user,
+ committype => $committype,
+ when => $when,
+ message => [@message],
+ pages => [@pages],
+ } if @pages;
+ }
+
+ $automator->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) = <MTNDIFF>;
+
+ 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 (<MTNLOG>) {
+ 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->new();
+ $automator->open(undef, $config{mtnrootdir});
+
+ my $certs = [read_certs($automator, $firstRev)];
+
+ $automator->close();
+
+ my $date;
+
+ foreach my $cert (@$certs) {
+ if ($cert->{signature} eq "ok" && $cert->{trust} eq "trusted") {
+ if ($cert->{name} eq "date") {
+ $date = $cert->{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
diff --git a/IkiWiki/Plugin/norcs.pm b/IkiWiki/Plugin/norcs.pm
new file mode 100644
index 000000000..04ba5f028
--- /dev/null
+++ b/IkiWiki/Plugin/norcs.pm
@@ -0,0 +1,99 @@
+#!/usr/bin/perl
+# Stubs for no revision control.
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+
+sub rcs_update () {
+ # Update working directory to current version.
+ # (May be more complex for distributed RCS.)
+}
+
+sub rcs_prepedit ($) {
+ # Prepares to edit a file under revision control. Returns a token
+ # that must be passed into rcs_commit when the file is ready
+ # for committing.
+ # The file is relative to the srcdir.
+ return ""
+}
+
+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, $message, $rcstoken, $user, $ipaddr) = @_;
+ 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)=@_;
+ return undef # success
+}
+
+sub rcs_add ($) {
+ # Add a file. The filename is relative to the root of the srcdir.
+ # Note that this should not check the new file in, it should only
+ # prepare for it to be checked in when rcs_commit is called.
+ # Note that the file may be in a new subdir that is not yet added
+ # to version control; the subdir can be added if so.
+}
+
+sub rcs_remove ($) {
+ # Remove a file. The filename is relative to the root of the srcdir.
+ # Note that this should not check the removal in, it should only
+ # prepare for it to be checked in when rcs_commit is called.
+ # Note that the new file may be in a new subdir that is not yet added
+ # to version control; the subdir can be added if so.
+}
+
+sub rcs_rename ($$) {
+ # Rename a file. The filenames are relative to the root of the srcdir.
+ # Note that this should not commit the rename, it should only
+ # prepare it for when rcs_commit is called.
+ # The new filename may be in a new subdir, that is not yet added to
+ # version control. If so, the subdir will exist already, and should
+ # be added to revision control.
+}
+
+sub rcs_recentchanges ($) {
+ # Examine the RCS history and generate a list of recent changes.
+ # The data structure returned for each change is:
+ # {
+ # rev => # the RCSs id for this commit
+ # user => # name of user who made the change,
+ # committype => # either "web" or the name of the rcs,
+ # when => # time when the change was made,
+ # message => [
+ # { line => "commit message line" },
+ # { line => "commit message line" },
+ # # etc,
+ # ],
+ # pages => [
+ # {
+ # page => # name of page changed,
+ # diffurl => # optional url to a diff showing
+ # # the changes,
+ # },
+ # # repeat for each page changed in this commit,
+ # ],
+ # }
+}
+
+sub rcs_diff ($) {
+ # Optional, used to get diffs for recentchanges.
+ # The parameter is the rev from rcs_recentchanges.
+ # Should return a list of lines of the diff (including \n) in list
+ # context, and the whole diff in scalar context.
+}
+
+sub rcs_getctime ($) {
+ # Optional, used to get the page creation time from the RCS.
+ error gettext("getctime not implemented");
+}
+
+1
diff --git a/IkiWiki/Plugin/svn.pm b/IkiWiki/Plugin/svn.pm
new file mode 100644
index 000000000..0e7df3659
--- /dev/null
+++ b/IkiWiki/Plugin/svn.pm
@@ -0,0 +1,366 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+use POSIX qw(setlocale LC_CTYPE);
+
+hook(type => "checkconfig", id => "svn", call => sub { #{{{
+ if (! defined $config{diffurl}) {
+ $config{diffurl}="";
+ }
+ if (! defined $config{svnpath}) {
+ $config{svnpath}="trunk";
+ }
+ if (exists $config{svnpath}) {
+ # code depends on the path not having extraneous slashes
+ $config{svnpath}=~tr#/#/#s;
+ $config{svnpath}=~s/\/$//;
+ $config{svnpath}=~s/^\///;
+ }
+ if (length $config{svn_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{svn_wrapper},
+ wrappermode => (defined $config{svn_wrappermode} ? $config{svn_wrappermode} : "04755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "svn", call => sub { #{{{
+ return
+ svnrepo => {
+ type => "string",
+ example => "/svn/wiki",
+ description => "subversion repository location",
+ safe => 0, # path
+ rebuild => 0,
+ },
+ svnpath => {
+ type => "string",
+ example => "trunk",
+ description => "path inside repository where the wiki is located",
+ safe => 0, # paranoia
+ rebuild => 0,
+ },
+ svn_wrapper => {
+ type => "string",
+ example => "/svn/wikirepo/hooks/post-commit",
+ description => "svn post-commit executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ svn_wrappermode => {
+ type => "string",
+ example => '04755',
+ description => "mode for svn_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ example => "http://svn.example.org/trunk/[[file]]",
+ description => "viewvc url to show file history ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ example => "http://svn.example.org/trunk/[[file]]?root=wiki&amp;r1=[[r1]]&amp;r2=[[r2]]",
+ description => "viewvc url to show a diff ([[file]], [[r1]], and [[r2]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+}); #}}}
+
+# svn needs LC_CTYPE set to a UTF-8 locale, so try to find one. Any will do.
+sub find_lc_ctype() {
+ my $current = setlocale(LC_CTYPE());
+ return $current if $current =~ m/UTF-?8$/i;
+
+ # Make some obvious attempts to avoid calling `locale -a`
+ foreach my $locale ("$current.UTF-8", "en_US.UTF-8", "en_GB.UTF-8") {
+ return $locale if setlocale(LC_CTYPE(), $locale);
+ }
+
+ # Try to get all available locales and pick the first UTF-8 one found.
+ if (my @locale = grep(/UTF-?8$/i, `locale -a`)) {
+ chomp @locale;
+ return $locale[0] if setlocale(LC_CTYPE(), $locale[0]);
+ }
+
+ # fallback to the current locale
+ return $current;
+} # }}}
+$ENV{LC_CTYPE} = $ENV{LC_CTYPE} || find_lc_ctype();
+
+sub svn_info ($$) { #{{{
+ my $field=shift;
+ my $file=shift;
+
+ my $info=`LANG=C svn info $file`;
+ my ($ret)=$info=~/^$field: (.*)$/m;
+ return $ret;
+} #}}}
+
+sub rcs_update () { #{{{
+ if (-d "$config{srcdir}/.svn") {
+ if (system("svn", "update", "--quiet", $config{srcdir}) != 0) {
+ warn("svn update failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_prepedit ($) { #{{{
+ # Prepares to edit a file under revision control. Returns a token
+ # that must be passed into rcs_commit when the file is ready
+ # for committing.
+ # The file is relative to the srcdir.
+ my $file=shift;
+
+ if (-d "$config{srcdir}/.svn") {
+ # For subversion, return the revision of the file when
+ # editing begins.
+ my $rev=svn_info("Revision", "$config{srcdir}/$file");
+ return defined $rev ? $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;
+
+ if (defined $user) {
+ $message="web commit by $user".(length $message ? ": $message" : "");
+ }
+ elsif (defined $ipaddr) {
+ $message="web commit from $ipaddr".(length $message ? ": $message" : "");
+ }
+
+ if (-d "$config{srcdir}/.svn") {
+ # Check to see if the page has been changed by someone
+ # else since rcs_prepedit was called.
+ my ($oldrev)=$rcstoken=~/^([0-9]+)$/; # untaint
+ my $rev=svn_info("Revision", "$config{srcdir}/$file");
+ if (defined $rev && defined $oldrev && $rev != $oldrev) {
+ # Merge their changes into the file that we've
+ # changed.
+ if (system("svn", "merge", "--quiet", "-r$oldrev:$rev",
+ "$config{srcdir}/$file", "$config{srcdir}/$file") != 0) {
+ warn("svn merge -r$oldrev:$rev failed\n");
+ }
+ }
+
+ if (system("svn", "commit", "--quiet",
+ "--encoding", "UTF-8", "-m",
+ possibly_foolish_untaint($message),
+ $config{srcdir}) != 0) {
+ my $conflict=readfile("$config{srcdir}/$file");
+ if (system("svn", "revert", "--quiet", "$config{srcdir}/$file") != 0) {
+ warn("svn revert failed\n");
+ }
+ return $conflict;
+ }
+ }
+ 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)=@_;
+
+ if (defined $user) {
+ $message="web commit by $user".(length $message ? ": $message" : "");
+ }
+ elsif (defined $ipaddr) {
+ $message="web commit from $ipaddr".(length $message ? ": $message" : "");
+ }
+
+ if (system("svn", "commit", "--quiet",
+ "--encoding", "UTF-8", "-m",
+ possibly_foolish_untaint($message),
+ $config{srcdir}) != 0) {
+ warn("svn commit failed\n");
+ return 1; # failure
+ }
+ return undef # success
+}
+
+sub rcs_add ($) { #{{{
+ # filename is relative to the root of the srcdir
+ my $file=shift;
+
+ if (-d "$config{srcdir}/.svn") {
+ my $parent=dirname($file);
+ while (! -d "$config{srcdir}/$parent/.svn") {
+ $file=$parent;
+ $parent=dirname($file);
+ }
+
+ if (system("svn", "add", "--quiet", "$config{srcdir}/$file") != 0) {
+ warn("svn add failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_remove ($) { #{{{
+ # filename is relative to the root of the srcdir
+ my $file=shift;
+
+ if (-d "$config{srcdir}/.svn") {
+ if (system("svn", "rm", "--force", "--quiet", "$config{srcdir}/$file") != 0) {
+ warn("svn rm failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_rename ($$) { #{{{
+ # filenames relative to the root of the srcdir
+ my ($src, $dest)=@_;
+
+ if (-d "$config{srcdir}/.svn") {
+ # Add parent directory for $dest
+ my $parent=dirname($dest);
+ if (! -d "$config{srcdir}/$parent/.svn") {
+ while (! -d "$config{srcdir}/$parent/.svn") {
+ $parent=dirname($dest);
+ }
+ if (system("svn", "add", "--quiet", "$config{srcdir}/$parent") != 0) {
+ warn("svn add $parent failed\n");
+ }
+ }
+
+ if (system("svn", "mv", "--force", "--quiet",
+ "$config{srcdir}/$src", "$config{srcdir}/$dest") != 0) {
+ warn("svn rename failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_recentchanges ($) { #{{{
+ my $num=shift;
+ my @ret;
+
+ return unless -d "$config{srcdir}/.svn";
+
+ eval q{
+ use Date::Parse;
+ use XML::SAX;
+ use XML::Simple;
+ };
+ error($@) if $@;
+
+ # avoid using XML::SAX::PurePerl, it's buggy with UTF-8 data
+ my @parsers = map { ${$_}{Name} } @{XML::SAX->parsers()};
+ do {
+ $XML::Simple::PREFERRED_PARSER = pop @parsers;
+ } until $XML::Simple::PREFERRED_PARSER ne 'XML::SAX::PurePerl';
+
+ # --limit is only supported on Subversion 1.2.0+
+ my $svn_version=`svn --version -q`;
+ my $svn_limit='';
+ $svn_limit="--limit $num"
+ if $svn_version =~ /\d\.(\d)\.\d/ && $1 >= 2;
+
+ my $svn_url=svn_info("URL", $config{srcdir});
+ my $xml = XMLin(scalar `svn $svn_limit --xml -v log '$svn_url'`,
+ ForceArray => [ 'logentry', 'path' ],
+ GroupTags => { paths => 'path' },
+ KeyAttr => { path => 'content' },
+ );
+ foreach my $logentry (@{$xml->{logentry}}) {
+ my (@pages, @message);
+
+ my $rev = $logentry->{revision};
+ my $user = $logentry->{author};
+
+ my $when=str2time($logentry->{date}, 'UTC');
+
+ foreach my $msgline (split(/\n/, $logentry->{msg})) {
+ push @message, { line => $msgline };
+ }
+
+ my $committype="web";
+ if (defined $message[0] &&
+ $message[0]->{line}=~/$config{web_commit_regexp}/) {
+ $user=defined $2 ? "$2" : "$3";
+ $message[0]->{line}=$4;
+ }
+ else {
+ $committype="svn";
+ }
+
+ foreach my $file (keys %{$logentry->{paths}}) {
+ if (length $config{svnpath}) {
+ next unless $file=~/^\/\Q$config{svnpath}\E\/([^ ]+)(?:$|\s)/;
+ $file=$1;
+ }
+
+ my $diffurl=$config{diffurl};
+ $diffurl=~s/\[\[file\]\]/$file/g;
+ $diffurl=~s/\[\[r1\]\]/$rev - 1/eg;
+ $diffurl=~s/\[\[r2\]\]/$rev/g;
+
+ push @pages, {
+ page => pagename($file),
+ diffurl => $diffurl,
+ } if length $file;
+ }
+ push @ret, {
+ rev => $rev,
+ user => $user,
+ committype => $committype,
+ when => $when,
+ message => [@message],
+ pages => [@pages],
+ } if @pages;
+ return @ret if @ret >= $num;
+ }
+
+ return @ret;
+} #}}}
+
+sub rcs_diff ($) { #{{{
+ my $rev=possibly_foolish_untaint(int(shift));
+ return `svnlook diff $config{svnrepo} -r$rev --no-diff-deleted`;
+} #}}}
+
+sub rcs_getctime ($) { #{{{
+ my $file=shift;
+
+ my $svn_log_infoline=qr/^r\d+\s+\|\s+[^\s]+\s+\|\s+(\d+-\d+-\d+\s+\d+:\d+:\d+\s+[-+]?\d+).*/;
+
+ my $child = open(SVNLOG, "-|");
+ if (! $child) {
+ exec("svn", "log", $file) || error("svn log $file failed to run");
+ }
+
+ my $date;
+ while (<SVNLOG>) {
+ if (/$svn_log_infoline/) {
+ $date=$1;
+ }
+ }
+ close SVNLOG || warn "svn log $file exited $?";
+
+ if (! defined $date) {
+ warn "failed to parse svn log for $file\n";
+ return 0;
+ }
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+ $date=str2time($date);
+ debug("found ctime ".localtime($date)." for $file");
+ return $date;
+} #}}}
+
+1
diff --git a/IkiWiki/Plugin/tla.pm b/IkiWiki/Plugin/tla.pm
new file mode 100644
index 000000000..e1389a346
--- /dev/null
+++ b/IkiWiki/Plugin/tla.pm
@@ -0,0 +1,271 @@
+#!/usr/bin/perl
+
+package IkiWiki;
+
+use warnings;
+use strict;
+use IkiWiki;
+
+hook(type => "checkconfig", id => "tla", call => sub { #{{{
+ if (! defined $config{diffurl}) {
+ $config{diffurl}="";
+ }
+ if (length $config{tla_wrapper}) {
+ push @{$config{wrappers}}, {
+ wrapper => $config{tla_wrapper},
+ wrappermode => (defined $config{tla_wrappermode} ? $config{tla_wrappermode} : "06755"),
+ };
+ }
+}); #}}}
+
+hook(type => "getsetup", id => "tla", call => sub { #{{{
+ return
+ tla_wrapper => {
+ type => "string",
+ #example => "", # TODO example
+ description => "tla post-commit executable to generate",
+ safe => 0, # file
+ rebuild => 0,
+ },
+ tla_wrappermode => {
+ type => "string",
+ example => '06755',
+ description => "mode for tla_wrapper (can safely be made suid)",
+ safe => 0,
+ rebuild => 0,
+ },
+ historyurl => {
+ type => "string",
+ #example => "", # TODO example
+ description => "url to show file history ([[file]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+ diffurl => {
+ type => "string",
+ #example => "", # TODO example
+ description => "url to show a diff ([[file]] and [[rev]] substituted)",
+ safe => 1,
+ rebuild => 1,
+ },
+}); #}}}
+
+sub quiet_system (@) {
+ # See Debian bug #385939.
+ open (SAVEOUT, ">&STDOUT");
+ close STDOUT;
+ open (STDOUT, ">/dev/null");
+ my $ret=system(@_);
+ close STDOUT;
+ open (STDOUT, ">&SAVEOUT");
+ close SAVEOUT;
+ return $ret;
+}
+
+sub rcs_update () { #{{{
+ if (-d "$config{srcdir}/{arch}") {
+ if (quiet_system("tla", "replay", "-d", $config{srcdir}) != 0) {
+ warn("tla replay failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_prepedit ($) { #{{{
+ my $file=shift;
+
+ if (-d "$config{srcdir}/{arch}") {
+ # For Arch, return the tree-id of archive when
+ # editing begins.
+ my $rev=`tla tree-id $config{srcdir}`;
+ return defined $rev ? $rev : "";
+ }
+} #}}}
+
+sub rcs_commit ($$$;$$) { #{{{
+ my $file=shift;
+ my $message=shift;
+ my $rcstoken=shift;
+ my $user=shift;
+ my $ipaddr=shift;
+
+ if (defined $user) {
+ $message="web commit by $user".(length $message ? ": $message" : "");
+ }
+ elsif (defined $ipaddr) {
+ $message="web commit from $ipaddr".(length $message ? ": $message" : "");
+ }
+
+ if (-d "$config{srcdir}/{arch}") {
+ # Check to see if the page has been changed by someone
+ # else since rcs_prepedit was called.
+ my ($oldrev)=$rcstoken=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
+ my $rev=`tla tree-id $config{srcdir}`;
+ if (defined $rev && defined $oldrev && $rev ne $oldrev) {
+ # Merge their changes into the file that we've
+ # changed.
+ if (quiet_system("tla", "update", "-d",
+ "$config{srcdir}") != 0) {
+ warn("tla update failed\n");
+ }
+ }
+
+ if (quiet_system("tla", "commit",
+ "-L".possibly_foolish_untaint($message),
+ '-d', $config{srcdir}) != 0) {
+ my $conflict=readfile("$config{srcdir}/$file");
+ if (system("tla", "undo", "-n", "--quiet", "-d", "$config{srcdir}") != 0) {
+ warn("tla undo failed\n");
+ }
+ return $conflict;
+ }
+ }
+ 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 tla"); # TODO
+}
+
+sub rcs_add ($) { #{{{
+ my $file=shift;
+
+ if (-d "$config{srcdir}/{arch}") {
+ if (quiet_system("tla", "add", "$config{srcdir}/$file") != 0) {
+ warn("tla add failed\n");
+ }
+ }
+} #}}}
+
+sub rcs_remove ($) { # {{{
+ my $file = shift;
+
+ error("rcs_remove not implemented for tla"); # TODO
+} #}}}
+
+sub rcs_rename ($$) { # {{{a
+ my ($src, $dest) = @_;
+
+ error("rcs_rename not implemented for tla"); # TODO
+} #}}}
+
+sub rcs_recentchanges ($) {
+ my $num=shift;
+ my @ret;
+
+ return unless -d "$config{srcdir}/{arch}";
+
+ eval q{use Date::Parse};
+ error($@) if $@;
+ eval q{use Mail::Header};
+ error($@) if $@;
+
+ my $logs = `tla logs -d $config{srcdir}`;
+ my @changesets = reverse split(/\n/, $logs);
+
+ for (my $i=0; $i<$num && $i<$#changesets; $i++) {
+ my ($change)=$changesets[$i]=~/^([A-Za-z0-9@\/._-]+)$/; # untaint
+
+ open(LOG, "tla cat-log -d $config{srcdir} $change|");
+ my $head = Mail::Header->new(\*LOG);
+ close(LOG);
+
+ my $rev = $head->get("Revision");
+ my $summ = $head->get("Summary");
+ my $newfiles = $head->get("New-files");
+ my $modfiles = $head->get("Modified-files");
+ my $remfiles = $head->get("Removed-files");
+ my $user = $head->get("Creator");
+
+ my @paths = grep { !/^(.*\/)?\.arch-ids\/.*\.id$/ }
+ split(/ /, "$newfiles $modfiles .arch-ids/fake.id");
+
+ my $sdate = $head->get("Standard-date");
+ my $when = str2time($sdate, 'UTC');
+
+ my $committype = "web";
+ if (defined $summ && $summ =~ /$config{web_commit_regexp}/) {
+ $user = defined $2 ? "$2" : "$3";
+ $summ = $4;
+ }
+ else {
+ $committype="tla";
+ }
+
+ my @message;
+ push @message, { line => $summ };
+
+ my @pages;
+
+ foreach my $file (@paths) {
+ my $diffurl=$config{diffurl};
+ $diffurl=~s/\[\[file\]\]/$file/g;
+ $diffurl=~s/\[\[rev\]\]/$change/g;
+ push @pages, {
+ page => pagename($file),
+ diffurl => $diffurl,
+ } if length $file;
+ }
+ push @ret, {
+ rev => $change,
+ user => $user,
+ committype => $committype,
+ when => $when,
+ message => [@message],
+ pages => [@pages],
+ } if @pages;
+
+ last if $i == $num;
+ }
+
+ return @ret;
+}
+
+sub rcs_diff ($) { #{{{
+ my $rev=shift;
+ my $logs = `tla logs -d $config{srcdir}`;
+ my @changesets = reverse split(/\n/, $logs);
+ my $i;
+
+ for($i=0;$i<$#changesets;$i++) {
+ last if $changesets[$i] eq $rev;
+ }
+
+ my $revminusone = $changesets[$i+1];
+ return `tla diff -d $config{srcdir} $revminusone`;
+} #}}}
+
+sub rcs_getctime ($) { #{{{
+ my $file=shift;
+ eval q{use Date::Parse};
+ error($@) if $@;
+ eval q{use Mail::Header};
+ error($@) if $@;
+
+ my $logs = `tla logs -d $config{srcdir}`;
+ my @changesets = reverse split(/\n/, $logs);
+ my $sdate;
+
+ for (my $i=0; $i<$#changesets; $i++) {
+ my $change = $changesets[$i];
+
+ open(LOG, "tla cat-log -d $config{srcdir} $change|");
+ my $head = Mail::Header->new(\*LOG);
+ close(LOG);
+
+ $sdate = $head->get("Standard-date");
+ my $newfiles = $head->get("New-files");
+
+ my ($lastcreation) = grep {/^$file$/} split(/ /, "$newfiles");
+ last if defined($lastcreation);
+ }
+
+ my $date=str2time($sdate, 'UTC');
+ debug("found ctime ".localtime($date)." for $file");
+ return $date;
+} #}}}
+
+1