]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/darcs.pm
Provide a UUID in edittemplate templates.
[ikiwiki.git] / IkiWiki / Plugin / darcs.pm
index 978457b2c5163c450add7fe3d0c7d2be91679982..646f65df10688087decd275e0be6109203573aae 100644 (file)
@@ -1,46 +1,9 @@
 #!/usr/bin/perl
-# Support for the darcs rcs, <URL:http://darcs.net/>.
-# Copyright (C) 2006  Thomas Schwinge <tschwinge@gnu.org>
-#               2007  Benjamin A'Lee <bma@bmalee.eu>
-#                     Tuomo Valkonen <tuomov@iki.fi>
-#               2008  Simon Michael <simon@joyful.com>
-#                     Petr Ročkai <me@mornfall.net>
-#                     Sven M. Hallberg <pesco@khjk.org>
-#
-# This program is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License as published by the
-# Free Software Foundation; either version 2 of the License, or (at your
-# option) any later version.
-#
-# This program is distributed in the hope that it will be useful, but
-# WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
-# General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License along
-# with this program; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
-
-# History (see http://ikiwiki.info/todo/darcs/):
-#
-#  * Thomas Schwinge wrote the original file, implementing only rcs_commit.
-#  * Benjamin A'Lee contributed an alternative implementation.
-#  * Tuomo Valkonen contributed rcs_getctime and stub rcs_recentchanges.
-#  * Simon Michael contributed multiple changes.
-#  * Petr Ročkai fixed rcs_recentchanges and added caching to rcs_getctime.
-#  * Sven M. Hallberg merged the above and added missing features.
-
-
-# We're guaranteed to be the only instance of ikiwiki running at a given
-# time.  It is essential that only ikiwiki is working on a particular
-# repository.  That means one instance of ikiwiki and it also means that
-# you must not 'darcs push' into this repository, as this might create
-# race conditions, as I understand it.
-
 package IkiWiki::Plugin::darcs;
 
 use warnings;
 use strict;
+use URI::Escape q{uri_escape_utf8};
 use IkiWiki;
 
 sub import {
@@ -56,6 +19,7 @@ sub import {
        hook(type => "rcs", id => "rcs_recentchanges", call => \&rcs_recentchanges);
        hook(type => "rcs", id => "rcs_diff", call => \&rcs_diff);
        hook(type => "rcs", id => "rcs_getctime", call => \&rcs_getctime);
+       hook(type => "rcs", id => "rcs_getmtime", call => \&rcs_getmtime);
 }
 
 sub silentsystem (@) {
@@ -89,7 +53,7 @@ sub darcs_info ($$$) {
        return $_;
 }
 
-sub file_in_vc($$) {
+sub file_in_vc ($$) {
        my $repodir = shift;
        my $file = shift;
 
@@ -100,23 +64,23 @@ sub file_in_vc($$) {
        }
        my $found=0;
        while (<DARCS_MANIFEST>) {
-               $found = 1, last if /^(\.\/)?$file$/;
+               $found = 1 if /^(\.\/)?$file$/;
        }
        close(DARCS_MANIFEST) or error("'darcs query manifest' exited " . $?);
 
        return $found;
 }
 
-sub darcs_rev($) {
+sub darcs_rev ($) {
        my $file = shift; # Relative to the repodir.
        my $repodir = $config{srcdir};
 
-       return "" if (! file_in_vc($repodir, $file));
+       return "" unless file_in_vc($repodir, $file);
        my $hash = darcs_info('hash', $repodir, $file);
        return defined $hash ? $hash : "";
 }
 
-sub checkconfig() {
+sub checkconfig () {
        if (defined $config{darcs_wrapper} && length $config{darcs_wrapper}) {
                push @{$config{wrappers}}, {
                        wrapper => $config{darcs_wrapper},
@@ -125,11 +89,12 @@ sub checkconfig() {
        }
 }
 
-sub getsetup() {
+sub getsetup () {
        return
                plugin => {
                        safe => 0, # rcs plugin
                        rebuild => undef,
+                       section => "rcs",
                },
                darcs_wrapper => {
                        type => "string",
@@ -176,14 +141,31 @@ sub rcs_prepedit ($) {
        return $rev;
 }
 
-sub rcs_commit ($$$;$$) {
+sub commitauthor (@) {
+       my %params=@_;
+       
+       my $author="anon\@web";
+       if (defined $params{session}) {
+               if (defined $params{session}->param("name")) {
+                       return $params{session}->param("name").'@web';
+               }
+               elsif (defined $params{session}->remote_addr()) {
+                       return $params{session}->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, $rcstoken, $user, $ipaddr) = @_;
+       my ($file, $message, $token) =
+               ($params{file}, $params{message}, $params{token});
 
        # Compute if the "revision" of $file changed.
-       my $changed = darcs_rev($file) ne $rcstoken;
+       my $changed = darcs_rev($file) ne $token;
 
        # Yes, the following is a bit convoluted.
        if ($changed) {
@@ -191,7 +173,7 @@ sub rcs_commit ($$$;$$) {
                rename("$config{srcdir}/$file", "$config{srcdir}/$file.save") or
                        error("failed to rename $file to $file.save: $!");
 
-               # Roll the repository back to $rcstoken.
+               # 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
@@ -202,37 +184,28 @@ sub rcs_commit ($$$;$$) {
                # TODO: 'yes | ...' needed?  Doesn't seem so.
                silentsystem('darcs', "revert", "--repodir", $config{srcdir}, "--all") == 0 ||
                        error("'darcs revert' failed");
-               # Remove all patches starting at $rcstoken.
+               # Remove all patches starting at $token.
                my $child = open(DARCS_OBLITERATE, "|-");
                if (! $child) {
                        open(STDOUT, ">/dev/null");
                        exec('darcs', "obliterate", "--repodir", $config{srcdir},
-                          "--match", "hash " . $rcstoken) and
+                          "--match", "hash " . $token) and
                           error("'darcs obliterate' failed");
                }
                1 while print DARCS_OBLITERATE "y";
                close(DARCS_OBLITERATE);
-               # Restore the $rcstoken one.
+               # Restore the $token one.
                silentsystem('darcs', "pull", "--quiet", "--repodir", $config{srcdir},
-                       "--match", "hash " . $rcstoken, "--all") == 0 ||
+                       "--match", "hash " . $token, "--all") == 0 ||
                        error("'darcs pull' failed");
        
-               # We're back at $rcstoken.  Re-install the modified file.
+               # 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;
-       if (defined $user) {
-               $author = "$user\@web";
-       }
-       elsif (defined $ipaddr) {
-               $author = "$ipaddr\@web";
-       }
-       else {
-               $author = "anon\@web";
-       }
+       my $author=commitauthor(%params);
        if (!defined $message || !length($message)) {
                $message = "empty message";
        }
@@ -243,17 +216,17 @@ sub rcs_commit ($$$;$$) {
        # 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");
+               "--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.
-       $rcstoken = darcs_rev($file);
+       $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 $rcstoken;
+       my $conflicts = darcs_rev($file) ne $token;
        unlink("$config{srcdir}/$file.log") or
                error("failed to remove '$file.log'");
 
@@ -275,25 +248,18 @@ sub rcs_commit ($$$;$$) {
        }
 }
 
-sub rcs_commit_staged($$$) {
-       my ($message, $user, $ipaddr) = @_;
+sub rcs_commit_staged (@) {
+       my %params=@_;
 
-       my $author;
-       if (defined $user) {
-               $author = "$user\@web";
-       }
-       elsif (defined $ipaddr) {
-               $author = "$ipaddr\@web";
-       }
-       else {
-               $author = "anon\@web";
-       }
-       if (!defined $message || !length($message)) {
-               $message = "empty message";
+       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", $message) == 0 || error("'darcs record' failed");
+       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 ||
@@ -336,8 +302,6 @@ sub rcs_recentchanges ($) {
 
        my $repodir=$config{srcdir};
 
-       debug("darcs recent changes: $num");
-
        my $child = open(LOG, "-|");
        if (! $child) {
                $ENV{"DARCS_DONT_ESCAPE_ANYTHING"}=1;
@@ -353,15 +317,14 @@ sub rcs_recentchanges ($) {
 
        my $log = XMLin($data, ForceArray => 1);
 
-       debug("parsing recent changes...");
        foreach my $patch (@{$log->{patch}}) {
                my $date=$patch->{local_date};
                my $hash=$patch->{hash};
                my $when=str2time($date);
                my (@pages, @files, @pg);
-               push @pages, $_ for (@{$patch->{summary}->[0]->{modify_file}});
-               push @pages, $_ for (@{$patch->{summary}->[0]->{add_file}});
-               push @pages, $_ for (@{$patch->{summary}->[0]->{remove_file}});
+               push @pages, $_ foreach (@{$patch->{summary}->[0]->{modify_file}});
+               push @pages, $_ foreach (@{$patch->{summary}->[0]->{add_file}});
+               push @pages, $_ foreach (@{$patch->{summary}->[0]->{remove_file}});
                foreach my $f (@pages) {
                        $f = $f->{content} if ref $f;
                        $f =~ s,^\s+,,; $f =~ s,\s+$,,; # cut whitespace
@@ -374,34 +337,34 @@ sub rcs_recentchanges ($) {
 
                foreach my $f (@files) {
                        my $d = defined $config{'diffurl'} ? $config{'diffurl'} : "";
-                       $d =~ s/\[\[file\]\]/$f/go;
+                       my $ef = uri_escape_utf8($f);
+                       $d =~ s/\[\[file\]\]/$ef/go;
                        $d =~ s/\[\[hash\]\]/$hash/go;
 
-                       debug("file: $f");
-                       debug("diffurl: $d");
                        push @pg, {
                                page => pagename($f),
                                diffurl => $d,
                        };
                }
                next unless (scalar @pg > 0);
-               debug("recent change: " . $patch->{name}[0] . " ("
-                       . scalar @pg . " changes)");
 
                my @message;
                push @message, { line => $_ } foreach (@{$patch->{name}});
 
                my $committype;
-               if ($patch->{author} =~ /\@web$/) {
+               my $author;
+               if ($patch->{author} =~ /(.*)\@web$/) {
+                       $author = $1;
                        $committype = "web";
                }
                else {
+                       $author=$patch->{author};
                        $committype = "darcs";
                }
 
                push @ret, {
                        rev => $patch->{hash},
-                       user => $patch->{author},
+                       user => $author,
                        committype => $committype,
                        when => $when, 
                        message => [@message],
@@ -412,11 +375,14 @@ sub rcs_recentchanges ($) {
        return @ret;
 }
 
-sub rcs_diff ($) {
+sub rcs_diff ($;$) {
        my $rev=shift;
+       my $maxlines=shift;
        my @lines;
-       foreach my $line (silentsystem("darcs", "diff", "--match", "hash ".$rev)) {
+       my $repodir=$config{srcdir};
+       foreach my $line (`darcs diff --repodir  $repodir --match 'hash $rev'`) {
                if (@lines || $line=~/^diff/) {
+                       last if defined $maxlines && @lines == $maxlines;
                        push @lines, $line."\n";
                }
        }
@@ -435,17 +401,11 @@ sub rcs_getctime ($) {
        eval q{use XML::Simple};
        local $/=undef;
 
-       # Sigh... doing things the hard way again
-       my $repodir=$config{srcdir};
-
-       my $filer=substr($file, length($repodir));
-       $filer =~ s:^[/]+::;
-
        my $child = open(LOG, "-|");
        if (! $child) {
                exec("darcs", "changes", "--xml", "--reverse",
-                       "--repodir", "$repodir", "$filer")
-               || error("'darcs changes $filer' failed to run");
+                       "--repodir", $config{srcdir}, $file)
+               || error("'darcs changes $file' failed to run");
        }
 
        my $data;
@@ -460,7 +420,7 @@ sub rcs_getctime ($) {
        my $datestr = $log->{patch}[0]->{local_date};
 
        if (! defined $datestr) {
-               warn "failed to get ctime for $filer";
+               warn "failed to get ctime for $file";
                return 0;
        }
 
@@ -471,4 +431,8 @@ sub rcs_getctime ($) {
        return $date;
 }
 
+sub rcs_getmtime ($) {
+       error "rcs_getmtime is not implemented for darcs\n"; # TODO
+}
+
 1