1 Using the Mercurial backend, the lack of `rcs_commit_staged` is noticed
2 frequently. I couldn't find any tries to update `mercurial.pm`, so not
3 letting lack of Mercurial AND Perl knowledge bring me down, I copy-pasted
4 from `git.pm` to mimic its behaviour from a Mercurial perspective. I hope
5 it can be a foundation for development by those more proficient in
6 ikiwiki's inner workings. I have doubts that I personally will be able to
7 revise it more, based on my Perl skills.
9 I've tested it briefly. `ikiwiki-calendar` and posting of comments now
10 works with automatic commits, i.e. the `rcs_commit_staged` function works
11 in those cases. Under my current setup, I don't know where else to expect
12 it to work. I would be flabberghasted if there wasn't any problems with it,
15 > Absolutely, the [[/rcs]] chart shows mercurial is lagging behind
18 > I don't think this stuff is hard, or unlikely to work, familiarity with
19 > the rcs's particular details is the main thing. --[[Joey]]
21 Diff follows, for anyone to annotate. First code version is also available at [my hg-repo](http://510x.se/hg/program/ikiwiki/file/e741fcfd800f/Plugin/mercurial.pm). Latest version should be [here](http://46.239.104.5:81/hg/program/ikiwiki/file/tip/Plugin/mercurial.pm) ([raw format](http://46.239.104.5:81/hg/program/ikiwiki/raw-file/tip/Plugin/mercurial.pm)). I'll notify on this page with "*Done*" remarks when I've actually commited changes to my local repository. I don't know if I should replace the code and the comments below when I've changed something. I'll probably do this when the code feels more mature. --[[Daniel Andersson]]
23 diff -r 20c61288d7bd Plugin/mercurial.pm
24 --- a/Plugin/mercurial.pm Fri Jul 15 02:55:12 2011 +0200
25 +++ b/Plugin/mercurial.pm Fri Jul 15 03:29:10 2011 +0200
28 use open qw{:utf8 :std};
33 hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
34 hook(type => "getsetup", id => "mercurial", call => \&getsetup);
36 A corresponding variable is declared for git. It is unused as of yet for
37 Mercurial, but when more advanced merge features become available for
38 `mercurial.pm`, I think it will come into play.
40 > Maybe.. I'd rather avoid unused cruft though. --[[Joey]]
42 >> OK, will be removed. *Done* --[[Daniel Andersson]]
49 + # Start a child process safely without resorting to /bin/sh.
50 + # Returns command output (in list content) or success state
51 + # (in scalar context), or runs the specified data handler.
53 + my ($error_handler, $data_handler, @cmdline) = @_;
55 + my $pid = open my $OUT, "-|";
57 + error("Cannot fork: $!") if !defined $pid;
61 + # hg commands want to be in wc.
62 + if (! defined $hg_dir) {
63 + chdir $config{srcdir}
64 + or error("cannot chdir to $config{srcdir}: $!");
68 + or error("cannot chdir to $hg_dir: $!");
71 > How can this possibly work, since `$hg_dir` is not set? The code
72 > that is being replaced seems to use `-R` to make hg use the right
73 > directory. If it worked for you without specifying the directory,
74 > it's quite likely this is the place the patch fails in some
75 > unusual circumstance.. but I think it could easily use `-R` here.
77 >> It works since `if (! defined $hg_dir)` always hits, and `chdir $config{srcdir}` is well defined. The whole logic is just used in `git.pm` for merge functionality that is not present in `mercurial.pm`, so by the cruft argument above, this should be replaced with just chdir `$config{srcdir}` (which is equivalent to `hg -R` or `hg --cwd` from what I know). Will be removed. *Done* --[[Daniel Andersson]]
79 + exec @cmdline or error("Cannot exec '@cmdline': $!");
83 + # hg output is probably utf-8 encoded, but may contain
84 + # other encodings or invalidly encoded stuff. So do not rely
85 + # on the normal utf-8 IO layer, decode it by hand.
88 > Is this actually true for hg?
90 >> I don't know. ["hg stores everything internally as UTF-8, except for pathnames"](https://jira.atlassian.com/browse/FE-3198), but output is dependent on the system's locale. The environment variable `HGENCODING=utf-8` can be set to ensure that Mercurial's own output is always UTF-8, but when viewing a diff containing non-UTF-8 changes, the affected lines are nevertheless output in their original encoding. I personally think that this is the correct way to output it, though, unless there is a possibility that someone is running ikiwiki wih a non-UTF-8 locale.
92 >>> *Done*. I removed the `encode_utf8()` part and instead set `HGENCODING=utf-8` where the external `hg` command was called. It seems to have taken care of "all" character encoding issues (but it is an almost infinite error pool to draw from, so some problem might pop up). --[[Daniel Andersson]]
96 + $_=decode_utf8($_, 0);
100 + if (! defined $data_handler) {
104 + last unless $data_handler->($_);
110 + $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
112 + return wantarray ? @lines : ($? == 0);
114 +# Convenient wrappers.
115 +sub run_or_die ($@) { safe_hg(\&error, undef, @_) }
116 +sub run_or_cry ($@) { safe_hg(sub { warn @_ }, undef, @_) }
117 +sub run_or_non ($@) { safe_hg(undef, undef, @_) }
119 sub mercurial_log ($) {
126 - my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
127 - if (system(@cmdline) != 0) {
128 - warn "'@cmdline' failed: $!";
130 + run_or_cry('hg', '-q', 'update');
133 sub rcs_prepedit ($) {
135 With the `run_or_{die,cry,non}()` functions defined as in `git.pm`, some old Mercurial functions can be rewritten more compactly.
141 + return rcs_commit_helper(@_);
144 +sub rcs_commit_helper (@) {
149 > This `%env` stash is unused; `%ENV` is never modified.
151 >> Yes, the code is missing setting the username at all. The local `hgrc` file is always used. I'll add setting of `$ENV{HGUSER}`. *Done* --[[Daniel Andersson]]
154 my $user="Anonymous";
155 if (defined $params{session}) {
156 if (defined $params{session}->param("name")) {
158 Here comes the `rcs_commit{,_staged}` part. It is modeled on a `rcs_commit_helper` function, as in `git.pm`.
160 Some old `mercurial.pm` logic concerning commiter name is kept instead of transplanting the more elaborate logic from `git.pm`. Maybe it is better to "steal" that as well.
162 > Exactly how to encode the nickname from openid in the commit metadata,
163 > and get it back out in `rcs_recentchanges`, would probably vary from git.
165 >> Yes, right now the long and ugly OpenID strings, e.g. `https://www.google.com/accounts/o8/id?id=AItOawmUIes3yDLfQME0uvZvJKDN0NsdKPx_PTw`, gets recorded as author and are shown as `id [www.google.com/accounts/o8]` in RecentChanges. I see that here on `ikiwiki.info`, my commits, identified by OpenID, are shown as authored by simply `Daniel`. I'll look into it. --[[Daniel Andersson]]
167 @@ -143,43 +206,45 @@
168 $params{message} = "no message given";
171 - my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit",
172 - "-m", IkiWiki::possibly_foolish_untaint($params{message}),
173 - "-u", IkiWiki::possibly_foolish_untaint($user));
174 - if (system(@cmdline) != 0) {
175 - warn "'@cmdline' failed: $!";
176 + $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
180 + if (exists $params{file}) {
181 + push @opts, '--', $params{file};
184 + # hg commit returns non-zero if nothing really changed.
185 + # So we should ignore its exit status (hence run_or_non).
186 + run_or_non('hg', 'commit', '-m', $params{message}, '-q', @opts);
189 return undef; # success
192 sub rcs_commit_staged (@) {
193 # Commits all staged changes. Changes can be staged using rcs_add,
194 # rcs_remove, and rcs_rename.
197 - error("rcs_commit_staged not implemented for mercurial"); # TODO
198 + return rcs_commit_helper(@_);
204 - my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
205 - if (system(@cmdline) != 0) {
206 - warn "'@cmdline' failed: $!";
208 + run_or_cry('hg', 'add', $file);
212 + # Remove file from archive.
216 - error("rcs_remove not implemented for mercurial"); # TODO
217 + run_or_cry('hg', 'remove', '-f', $file);
220 sub rcs_rename ($$) {
221 my ($src, $dest) = @_;
223 - error("rcs_rename not implemented for mercurial"); # TODO
224 + run_or_cry('hg', 'rename', '-f', $src, $dest);
227 sub rcs_recentchanges ($) {
229 > Remainder seems ok to me. Should probably test that the remove plugin
230 > works, since this implements `rcs_remove` too and you didn't mention
231 > any tests that would run that code path. --[[Joey]]
233 >> I tested `rename`. It fails if the page title includes e.g. åäö. Trying to rename a page from "title without special chars" to "title with åäö" renders in `/var/log/apache2/error.log`:
235 [Fri Jul 15 14:58:17 2011] [error] [client 46.239.104.5] transaction abort!, referer: http://46.239.104.5:81/blog/ikiwiki.cgi
236 [Fri Jul 15 14:58:17 2011] [error] [client 46.239.104.5] rollback completed, referer: http://46.239.104.5:81/blog/ikiwiki.cgi
237 [Fri Jul 15 14:58:17 2011] [error] [client 46.239.104.5] abort: decoding near 'itle_with_\xc3\xa5\xc3\xa4\xc3\xb6.mdw': 'ascii' codec can't decode byte 0xc3 in position 66: ordinal not in range(128)!, referer: http://46.239.104.5:81/blog/ikiwiki.cgi
238 [Fri Jul 15 14:58:17 2011] [error] [client 46.239.104.5] 'hg commit -m rename posts/title_without_special_chars.mdwn to posts/title_with_\xc3\xa5\xc3\xa4\xc3\xb6.mdwn -q' failed: at /usr/share/perl5/IkiWiki/Plugin/mercurial.pm line 123., referer: http://46.239.104.5:81/blog/ikiwiki.cgi
240 >>> I added setting the environment variable `HGENCODING=utf-8` in `rcs_commit_helper`, which took care of these problems. *Done* --[[Daniel Andersson]]
242 >> When this has happened, directly following by `rename` and `remove` doesn't work as it should, since the file is not commited. `hg remove -f` doesn't physically remove files that aren't tracked (no `hg` command does). Perhaps a regular `unlink $file` should be called to not clutter the source dir if `hg remove` failed because the file wasn't tracked. --[[Daniel Andersson]]
244 >> I've also noted that when a post is added or removed, the commit message lacks the page title. It contains the title when the page is renamed though, so it should be an easy fix. I'll look into it. --[[Daniel Andersson]]
246 >>> This is to do with `{rename,remove,editchanges}.pm`. The last two simply don't give a message to `rcs_commit_staged`. Separate issue. --[[Daniel Andersson]]