]> git.vanrenterghem.biz Git - git.ikiwiki.info.git/blob - IkiWiki/Rcs/mercurial.pm
* Applied a patch from MichaƂ to make the mercurial backend pass --quiet to
[git.ikiwiki.info.git] / IkiWiki / Rcs / mercurial.pm
1 #!/usr/bin/perl
3 use warnings;
4 use strict;
5 use IkiWiki;
6 use Encode;
7 use open qw{:utf8 :std};
9 package IkiWiki;
11 sub mercurial_log($) {
12         my $out = shift;
13         my @infos;
15         while (<$out>) {
16                 my $line = $_;
17                 my ($key, $value);
19                 if (/^description:/) {
20                         $key = "description";
21                         $value = "";
23                         # slurp everything as the description text 
24                         # until the next changeset
25                         while (<$out>) {
26                                 if (/^changeset: /) {
27                                         $line = $_;
28                                         last;
29                                 }
31                                 $value .= $_;
32                         }
34                         local $/ = "";
35                         chomp $value;
36                         $infos[$#infos]{$key} = $value;
37                 }
39                 chomp $line;
40                 ($key, $value) = split /: +/, $line, 2;
42                 if ($key eq "changeset") {
43                         push @infos, {};
45                         # remove the revision index, which is strictly 
46                         # local to the repository
47                         $value =~ s/^\d+://;
48                 }
50                 $infos[$#infos]{$key} = $value;
51         }
52         close $out;
54         return @infos;
55 }
57 sub rcs_update () { #{{{
58         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
59         if (system(@cmdline) != 0) {
60                 warn "'@cmdline' failed: $!";
61         }
62 } #}}}
64 sub rcs_prepedit ($) { #{{{
65         return "";
66 } #}}}
68 sub rcs_commit ($$$;$$) { #{{{
69         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
71         if (defined $user) {
72                 $user = possibly_foolish_untaint($user);
73         }
74         elsif (defined $ipaddr) {
75                 $user = "Anonymous from $ipaddr";
76         }
77         else {
78                 $user = "Anonymous";
79         }
81         $message = possibly_foolish_untaint($message);
83         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "commit", 
84                        "-m", "$message", "-u", "$user");
85         if (system(@cmdline) != 0) {
86                 warn "'@cmdline' failed: $!";
87         }
89         return undef; # success
90 } #}}}
92 sub rcs_add ($) { # {{{
93         my ($file) = @_;
95         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$file");
96         if (system(@cmdline) != 0) {
97                 warn "'@cmdline' failed: $!";
98         }
99 } #}}}
101 sub rcs_recentchanges ($) { #{{{
102         my ($num) = @_;
104         eval q{use CGI 'escapeHTML'};
105         error($@) if $@;
107         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num);
108         open (my $out, "@cmdline |");
110         eval q{use Date::Parse};
111         error($@) if $@;
113         my @ret;
114         foreach my $info (mercurial_log($out)) {
115                 my @pages = ();
116                 my @message = ();
117         
118                 foreach my $msgline (split(/\n/, $info->{description})) {
119                         push @message, { line => $msgline };
120                 }
122                 foreach my $file (split / /,$info->{files}) {
123                         my $diffurl = $config{'diffurl'};
124                         $diffurl =~ s/\[\[file\]\]/$file/go;
125                         $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
127                         push @pages, {
128                                 page => pagename($file),
129                                 diffurl => $diffurl,
130                         };
131                 }
133                 my $user = $info->{"user"};
134                 $user =~ s/\s*<.*>\s*$//;
135                 $user =~ s/^\s*//;
137                 push @ret, {
138                         rev        => $info->{"changeset"},
139                         user       => $user,
140                         committype => "mercurial",
141                         when       => time - str2time($info->{"date"}),
142                         message    => [@message],
143                         pages      => [@pages],
144                 };
145         }
147         return @ret;
148 } #}}}
150 sub rcs_notify () { #{{{
151         # TODO
152 } #}}}
154 sub rcs_getctime ($) { #{{{
155         my ($file) = @_;
157         # XXX filename passes through the shell here, should try to avoid
158         # that just in case
159         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1', $file);
160         open (my $out, "@cmdline |");
162         my @log = mercurial_log($out);
164         if (length @log < 1) {
165                 return 0;
166         }
168         eval q{use Date::Parse};
169         error($@) if $@;
170         
171         my $ctime = str2time($log[0]->{"date"});
172         return $ctime;
173 } #}}}