]> sipb.mit.edu Git - ikiwiki.git/blobdiff - IkiWiki/Plugin/mercurial.pm
mercurial: Fix buggy getctime code.
[ikiwiki.git] / IkiWiki / Plugin / mercurial.pm
index 3a98e09d8721c24030e988bdf6473a661e3cc695..a80bb2da59ddf91772bfa8928652fe1cff02f186 100644 (file)
@@ -1,6 +1,5 @@
 #!/usr/bin/perl
-
-package IkiWiki;
+package IkiWiki::Plugin::mercurial;
 
 use warnings;
 use strict;
@@ -8,24 +7,42 @@ 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}) {
+sub import {
+       hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
+       hook(type => "getsetup", id => "mercurial", call => \&getsetup);
+       hook(type => "rcs", id => "rcs_update", call => \&rcs_update);
+       hook(type => "rcs", id => "rcs_prepedit", call => \&rcs_prepedit);
+       hook(type => "rcs", id => "rcs_commit", call => \&rcs_commit);
+       hook(type => "rcs", id => "rcs_commit_staged", call => \&rcs_commit_staged);
+       hook(type => "rcs", id => "rcs_add", call => \&rcs_add);
+       hook(type => "rcs", id => "rcs_remove", call => \&rcs_remove);
+       hook(type => "rcs", id => "rcs_rename", call => \&rcs_rename);
+       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 checkconfig () {
+       if (exists $config{mercurial_wrapper} && 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 { #{{{
+sub getsetup () {
        return
+               plugin => {
+                       safe => 0, # rcs plugin
+                       rebuild => undef,
+                       section => "rcs",
+               },
                mercurial_wrapper => {
                        type => "string",
                        #example => # FIXME add example
-                       description => "mercurial post-commit executable to generate",
+                       description => "mercurial post-commit hook to generate",
                        safe => 0, # file
                        rebuild => 0,
                },
@@ -50,9 +67,9 @@ hook(type => "getsetup", id => "mercurial", call => sub { #{{{
                        safe => 1,
                        rebuild => 1,
                },
-}); #}}}
+}
 
-sub mercurial_log ($) { #{{{
+sub mercurial_log ($) {
        my $out = shift;
        my @infos;
 
@@ -96,33 +113,33 @@ sub mercurial_log ($) { #{{{
        close $out;
 
        return @infos;
-} #}}}
+}
 
-sub rcs_update () { #{{{
+sub rcs_update () {
        my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
        if (system(@cmdline) != 0) {
                warn "'@cmdline' failed: $!";
        }
-} #}}}
+}
 
-sub rcs_prepedit ($) { #{{{
+sub rcs_prepedit ($) {
        return "";
-} #}}}
+}
 
-sub rcs_commit ($$$;$$) { #{{{
+sub rcs_commit ($$$;$$) {
        my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
 
        if (defined $user) {
-               $user = possibly_foolish_untaint($user);
+               $user = IkiWiki::possibly_foolish_untaint($user);
        }
        elsif (defined $ipaddr) {
-               $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
+               $user = "Anonymous from ".IkiWiki::possibly_foolish_untaint($ipaddr);
        }
        else {
                $user = "Anonymous";
        }
 
-       $message = possibly_foolish_untaint($message);
+       $message = IkiWiki::possibly_foolish_untaint($message);
        if (! length $message) {
                $message = "no message given";
        }
@@ -134,7 +151,7 @@ sub rcs_commit ($$$;$$) { #{{{
        }
 
        return undef; # success
-} #}}}
+}
 
 sub rcs_commit_staged ($$$) {
        # Commits all staged changes. Changes can be staged using rcs_add,
@@ -144,28 +161,28 @@ sub rcs_commit_staged ($$$) {
        error("rcs_commit_staged not implemented for mercurial"); # TODO
 }
 
-sub rcs_add ($) { # {{{
+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 ($) { # {{{
+sub rcs_remove ($) {
        my ($file) = @_;
 
        error("rcs_remove not implemented for mercurial"); # TODO
-} #}}}
+}
 
-sub rcs_rename ($$) { # {{{
+sub rcs_rename ($$) {
        my ($src, $dest) = @_;
 
        error("rcs_rename not implemented for mercurial"); # TODO
-} #}}}
+}
 
-sub rcs_recentchanges ($) { #{{{
+sub rcs_recentchanges ($) {
        my ($num) = @_;
 
        my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
@@ -185,7 +202,7 @@ sub rcs_recentchanges ($) { #{{{
                }
 
                foreach my $file (split / /,$info->{files}) {
-                       my $diffurl = $config{'diffurl'};
+                       my $diffurl = defined $config{diffurl} ? $config{'diffurl'} : "";
                        $diffurl =~ s/\[\[file\]\]/$file/go;
                        $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
 
@@ -202,7 +219,7 @@ sub rcs_recentchanges ($) { #{{{
                push @ret, {
                        rev        => $info->{"changeset"},
                        user       => $user,
-                       committype => "mercurial",
+                       committype => "hg",
                        when       => str2time($info->{"date"}),
                        message    => [@message],
                        pages      => [@pages],
@@ -210,32 +227,34 @@ sub rcs_recentchanges ($) { #{{{
        }
 
        return @ret;
-} #}}}
+}
 
-sub rcs_diff ($) { #{{{
+sub rcs_diff ($) {
        # TODO
-} #}}}
+}
 
-sub rcs_getctime ($) { #{{{
+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 @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v",
+               "--style", "default", $file);
+       open (my $out, "-|", @cmdline);
 
-       my @log = mercurial_log($out);
+       my @log = (mercurial_log($out));
 
-       if (length @log < 1) {
+       if (@log < 1) {
                return 0;
        }
 
        eval q{use Date::Parse};
        error($@) if $@;
        
-       my $ctime = str2time($log[0]->{"date"});
+       my $ctime = str2time($log[$#log]->{"date"});
        return $ctime;
-} #}}}
+}
+
+sub rcs_getmtime ($) {
+       error "rcs_getmtime is not implemented for mercurial\n"; # TODO
+}
 
 1