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