]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/mercurial.pm
2ccba04da3e48540eba3138e99d4b038503e45aa
[ikiwiki.git] / IkiWiki / Rcs / mercurial.pm
1 #!/usr/bin/perl
2
3 package IkiWiki;
4
5 use warnings;
6 use strict;
7 use IkiWiki;
8 use Encode;
9 use open qw{:utf8 :std};
10
11 hook(type => "getsetup", id => "mercurial", call => sub { #{{{
12         return
13                 historyurl => {
14                         type => "string",
15                         default => "",
16                         example => "http://example.com:8000/log/tip/[[file]]",
17                         description => "url to hg serve'd repository, to show file history ([[file]] substituted)",
18                         safe => 1,
19                         rebuild => 1,
20                 },
21                 diffurl => {
22                         type => "string",
23                         default => "",
24                         example => "http://localhost:8000/?fd=[[r2]];file=[[file]]",
25                         description => "url to hg serve'd repository, to show diff ([[file]] and [[r2]] substituted)",
26                         safe => 1,
27                         rebuild => 1,
28                 },
29 }); #}}}
30
31 sub mercurial_log($) {
32         my $out = shift;
33         my @infos;
34
35         while (<$out>) {
36                 my $line = $_;
37                 my ($key, $value);
38
39                 if (/^description:/) {
40                         $key = "description";
41                         $value = "";
42
43                         # slurp everything as the description text 
44                         # until the next changeset
45                         while (<$out>) {
46                                 if (/^changeset: /) {
47                                         $line = $_;
48                                         last;
49                                 }
50
51                                 $value .= $_;
52                         }
53
54                         local $/ = "";
55                         chomp $value;
56                         $infos[$#infos]{$key} = $value;
57                 }
58
59                 chomp $line;
60                 ($key, $value) = split /: +/, $line, 2;
61
62                 if ($key eq "changeset") {
63                         push @infos, {};
64
65                         # remove the revision index, which is strictly 
66                         # local to the repository
67                         $value =~ s/^\d+://;
68                 }
69
70                 $infos[$#infos]{$key} = $value;
71         }
72         close $out;
73
74         return @infos;
75 }
76
77 sub rcs_update () { #{{{
78         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
79         if (system(@cmdline) != 0) {
80                 warn "'@cmdline' failed: $!";
81         }
82 } #}}}
83
84 sub rcs_prepedit ($) { #{{{
85         return "";
86 } #}}}
87
88 sub rcs_commit ($$$;$$) { #{{{
89         my ($file, $message, $rcstoken, $user, $ipaddr) = @_;
90
91         if (defined $user) {
92                 $user = possibly_foolish_untaint($user);
93         }
94         elsif (defined $ipaddr) {
95                 $user = "Anonymous from ".possibly_foolish_untaint($ipaddr);
96         }
97         else {
98                 $user = "Anonymous";
99         }
100
101         $message = possibly_foolish_untaint($message);
102         if (! length $message) {
103                 $message = "no message given";
104         }
105
106         my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit", 
107                        "-m", $message, "-u", $user);
108         if (system(@cmdline) != 0) {
109                 warn "'@cmdline' failed: $!";
110         }
111
112         return undef; # success
113 } #}}}
114
115 sub rcs_commit_staged ($$$) {
116         # Commits all staged changes. Changes can be staged using rcs_add,
117         # rcs_remove, and rcs_rename.
118         my ($message, $user, $ipaddr)=@_;
119         
120         error("rcs_commit_staged not implemented for mercurial"); # TODO
121 }
122
123 sub rcs_add ($) { # {{{
124         my ($file) = @_;
125
126         my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
127         if (system(@cmdline) != 0) {
128                 warn "'@cmdline' failed: $!";
129         }
130 } #}}}
131
132 sub rcs_remove ($) { # {{{
133         my ($file) = @_;
134
135         error("rcs_remove not implemented for mercurial"); # TODO
136 } #}}}
137
138 sub rcs_rename ($$) { # {{{
139         my ($src, $dest) = @_;
140
141         error("rcs_rename not implemented for mercurial"); # TODO
142 } #}}}
143
144 sub rcs_recentchanges ($) { #{{{
145         my ($num) = @_;
146
147         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
148                 "--style", "default");
149         open (my $out, "@cmdline |");
150
151         eval q{use Date::Parse};
152         error($@) if $@;
153
154         my @ret;
155         foreach my $info (mercurial_log($out)) {
156                 my @pages = ();
157                 my @message = ();
158         
159                 foreach my $msgline (split(/\n/, $info->{description})) {
160                         push @message, { line => $msgline };
161                 }
162
163                 foreach my $file (split / /,$info->{files}) {
164                         my $diffurl = $config{'diffurl'};
165                         $diffurl =~ s/\[\[file\]\]/$file/go;
166                         $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
167
168                         push @pages, {
169                                 page => pagename($file),
170                                 diffurl => $diffurl,
171                         };
172                 }
173
174                 my $user = $info->{"user"};
175                 $user =~ s/\s*<.*>\s*$//;
176                 $user =~ s/^\s*//;
177
178                 push @ret, {
179                         rev        => $info->{"changeset"},
180                         user       => $user,
181                         committype => "mercurial",
182                         when       => str2time($info->{"date"}),
183                         message    => [@message],
184                         pages      => [@pages],
185                 };
186         }
187
188         return @ret;
189 } #}}}
190
191 sub rcs_diff ($) { #{{{
192         # TODO
193 } #}}}
194
195 sub rcs_getctime ($) { #{{{
196         my ($file) = @_;
197
198         # XXX filename passes through the shell here, should try to avoid
199         # that just in case
200         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1', 
201                 "--style", "default", "$config{srcdir}/$file");
202         open (my $out, "@cmdline |");
203
204         my @log = mercurial_log($out);
205
206         if (length @log < 1) {
207                 return 0;
208         }
209
210         eval q{use Date::Parse};
211         error($@) if $@;
212         
213         my $ctime = str2time($log[0]->{"date"});
214         return $ctime;
215 } #}}}
216
217 1