Second revolution of addressing code remarks. Many changes and comments at once,...
[ikiwiki] / doc / todo / Attempt_to_extend_Mercurial_backend_support.mdwn
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.
8
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,
13 though.
14
15 > Absolutely, the [[/rcs]] chart shows mercurial is lagging behind
16 > nearly everything. 
17
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]] 
20
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]]
22
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
26         @@ -7,6 +7,8 @@
27          use Encode;
28          use open qw{:utf8 :std};
29          
30         +my $hg_dir=undef;
31         +
32          sub import {
33                 hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
34                 hook(type => "getsetup", id => "mercurial", call => \&getsetup);
35
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.
39
40 > Maybe.. I'd rather avoid unused cruft though. --[[Joey]]
41
42 >> OK, will be removed. *Done* --[[Daniel Andersson]]
43
44         @@ -69,6 +71,62 @@
45                         },
46          }
47          
48         +sub safe_hg (&@) {
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.
52         +
53         +       my ($error_handler, $data_handler, @cmdline) = @_;
54         +
55         +       my $pid = open my $OUT, "-|";
56         +
57         +       error("Cannot fork: $!") if !defined $pid;
58         +
59         +       if (!$pid) {
60         +               # In child.
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}: $!");
65         +               }
66         +               else {
67         +                       chdir $hg_dir
68         +                           or error("cannot chdir to $hg_dir: $!");
69         +               }
70
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.
76
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]]
78
79         +               exec @cmdline or error("Cannot exec '@cmdline': $!");
80         +       }
81         +       # In parent.
82         +
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.
86         +       binmode($OUT);
87
88 > Is this actually true for hg?
89
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.
91
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]]
93
94         +       my @lines;
95         +       while (<$OUT>) {
96         +               $_=decode_utf8($_, 0);
97         +
98         +               chomp;
99         +
100         +               if (! defined $data_handler) {
101         +                       push @lines, $_;
102         +               }
103         +               else {
104         +                       last unless $data_handler->($_);
105         +               }
106         +       }
107         +
108         +       close $OUT;
109         +
110         +       $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
111         +
112         +       return wantarray ? @lines : ($? == 0);
113         +}
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, @_) }
118         +
119          sub mercurial_log ($) {
120                 my $out = shift;
121                 my @infos;
122         @@ -116,10 +174,7 @@
123          }
124          
125          sub rcs_update () {
126         -       my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
127         -       if (system(@cmdline) != 0) {
128         -               warn "'@cmdline' failed: $!";
129         -       }
130         +       run_or_cry('hg', '-q', 'update');
131          }
132          
133          sub rcs_prepedit ($) {
134
135 With the `run_or_{die,cry,non}()` functions defined as in `git.pm`, some old Mercurial functions can be rewritten more compactly.
136
137         @@ -129,6 +184,14 @@
138          sub rcs_commit (@) {
139                 my %params=@_;
140          
141         +       return rcs_commit_helper(@_);
142         +}
143         +
144         +sub rcs_commit_helper (@) {
145         +       my %params=@_;
146         +
147         +       my %env=%ENV;
148
149 > This `%env` stash is unused; `%ENV` is never modified.
150
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]]
152
153         +
154                 my $user="Anonymous";
155                 if (defined $params{session}) {
156                         if (defined $params{session}->param("name")) {
157
158 Here comes the `rcs_commit{,_staged}` part. It is modeled on a `rcs_commit_helper` function, as in `git.pm`.
159
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.
161
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.
164
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]]
166
167         @@ -143,43 +206,45 @@
168                         $params{message} = "no message given";
169                 }
170          
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});
177         +
178         +       my @opts;
179         +       
180         +       if (exists $params{file}) {
181         +               push @opts, '--', $params{file};
182                 }
183         -
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);
187         +       
188         +       %ENV=%env;
189                 return undef; # success
190          }
191          
192          sub rcs_commit_staged (@) {
193                 # Commits all staged changes. Changes can be staged using rcs_add,
194                 # rcs_remove, and rcs_rename.
195         -       my %params=@_;
196         -       
197         -       error("rcs_commit_staged not implemented for mercurial"); # TODO
198         +       return rcs_commit_helper(@_);
199          }
200          
201          sub rcs_add ($) {
202                 my ($file) = @_;
203          
204         -       my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
205         -       if (system(@cmdline) != 0) {
206         -               warn "'@cmdline' failed: $!";
207         -       }
208         +       run_or_cry('hg', 'add', $file);
209          }
210          
211          sub rcs_remove ($) {
212         +       # Remove file from archive.
213         +
214                 my ($file) = @_;
215          
216         -       error("rcs_remove not implemented for mercurial"); # TODO
217         +       run_or_cry('hg', 'remove', '-f', $file);
218          }
219          
220          sub rcs_rename ($$) {
221                 my ($src, $dest) = @_;
222          
223         -       error("rcs_rename not implemented for mercurial"); # TODO
224         +       run_or_cry('hg', 'rename', '-f', $src, $dest);
225          }
226          
227          sub rcs_recentchanges ($) {
228
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]] 
232
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`:
234
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
239
240 >>> I added setting the environment variable `HGENCODING=utf-8` in `rcs_commit_helper`, which took care of these problems. *Done* --[[Daniel Andersson]]
241
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]]
243
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]]
245
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]]