]> sipb.mit.edu Git - ikiwiki.git/blob - IkiWiki/Rcs/mercurial.pm
All rcs backends need to implement rcs_remove
[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 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_remove ($) { # {{{
105         my ($file) = @_;
106
107         error("rcs_remove not implemented for mercurial"); # TODO
108 } #}}}
109
110 sub rcs_recentchanges ($) { #{{{
111         my ($num) = @_;
112
113         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", $num,
114                 "--style", "default");
115         open (my $out, "@cmdline |");
116
117         eval q{use Date::Parse};
118         error($@) if $@;
119
120         my @ret;
121         foreach my $info (mercurial_log($out)) {
122                 my @pages = ();
123                 my @message = ();
124         
125                 foreach my $msgline (split(/\n/, $info->{description})) {
126                         push @message, { line => $msgline };
127                 }
128
129                 foreach my $file (split / /,$info->{files}) {
130                         my $diffurl = $config{'diffurl'};
131                         $diffurl =~ s/\[\[file\]\]/$file/go;
132                         $diffurl =~ s/\[\[r2\]\]/$info->{changeset}/go;
133
134                         push @pages, {
135                                 page => pagename($file),
136                                 diffurl => $diffurl,
137                         };
138                 }
139
140                 my $user = $info->{"user"};
141                 $user =~ s/\s*<.*>\s*$//;
142                 $user =~ s/^\s*//;
143
144                 push @ret, {
145                         rev        => $info->{"changeset"},
146                         user       => $user,
147                         committype => "mercurial",
148                         when       => str2time($info->{"date"}),
149                         message    => [@message],
150                         pages      => [@pages],
151                 };
152         }
153
154         return @ret;
155 } #}}}
156
157 sub rcs_diff ($) { #{{{
158         # TODO
159 } #}}}
160
161 sub rcs_getctime ($) { #{{{
162         my ($file) = @_;
163
164         # XXX filename passes through the shell here, should try to avoid
165         # that just in case
166         my @cmdline = ("hg", "-R", $config{srcdir}, "log", "-v", "-l", '1', 
167                 "--style", "default", "$config{srcdir}/$file");
168         open (my $out, "@cmdline |");
169
170         my @log = mercurial_log($out);
171
172         if (length @log < 1) {
173                 return 0;
174         }
175
176         eval q{use Date::Parse};
177         error($@) if $@;
178         
179         my $ctime = str2time($log[0]->{"date"});
180         return $ctime;
181 } #}}}
182
183 1