Fixed presentation issue for "RecentChanges" to emulate git.pm. All lifted issues...
[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 > I've looked over the current version and it looks ok to me. --[[Joey]]
24
25 >> I changed the by `mercurial.pm` recorded commit messages and the `rcs_recentchanges` logic to include more information, to emulate the `git.pm` behaviour regarding name presentation on RecentChanges. I don't have anything more to add at the moment, so if the code passes review, I'm done, and I tag this page as "patch". [Final patch version as per this page at my hg repo](http://510x.se/hg/program/ikiwiki/file/bc0e2f838fe3/Plugin/mercurial.pm) ([raw format](http://46.239.104.5:81/hg/program/ikiwiki/raw-file/bc0e2f838fe3/Plugin/mercurial.pm)). I keep the below conversation for reference, but it's mostly outdated. --[[Daniel Andersson]]
26
27 [[!tag patch]]
28
29 ***
30
31         diff -r 20c61288d7bd Plugin/mercurial.pm
32         --- a/Plugin/mercurial.pm       Fri Jul 15 02:55:12 2011 +0200
33         +++ b/Plugin/mercurial.pm       Fri Jul 15 03:29:10 2011 +0200
34         @@ -7,6 +7,8 @@
35          use Encode;
36          use open qw{:utf8 :std};
37          
38         +my $hg_dir=undef;
39         +
40          sub import {
41                 hook(type => "checkconfig", id => "mercurial", call => \&checkconfig);
42                 hook(type => "getsetup", id => "mercurial", call => \&getsetup);
43
44 A corresponding variable is declared for git. It is unused as of yet for
45 Mercurial, but when more advanced merge features become available for
46 `mercurial.pm`, I think it will come into play.
47
48 > Maybe.. I'd rather avoid unused cruft though. --[[Joey]]
49
50 >> OK, will be removed. *Done* --[[Daniel Andersson]]
51
52         @@ -69,6 +71,62 @@
53                         },
54          }
55          
56         +sub safe_hg (&@) {
57         +       # Start a child process safely without resorting to /bin/sh.
58         +       # Returns command output (in list content) or success state
59         +       # (in scalar context), or runs the specified data handler.
60         +
61         +       my ($error_handler, $data_handler, @cmdline) = @_;
62         +
63         +       my $pid = open my $OUT, "-|";
64         +
65         +       error("Cannot fork: $!") if !defined $pid;
66         +
67         +       if (!$pid) {
68         +               # In child.
69         +               # hg commands want to be in wc.
70         +               if (! defined $hg_dir) {
71         +                       chdir $config{srcdir}
72         +                           or error("cannot chdir to $config{srcdir}: $!");
73         +               }
74         +               else {
75         +                       chdir $hg_dir
76         +                           or error("cannot chdir to $hg_dir: $!");
77         +               }
78
79 > How can this possibly work, since `$hg_dir` is not set? The code
80 > that is being replaced seems to use `-R` to make hg use the right
81 > directory. If it worked for you without specifying the directory,
82 > it's quite likely this is the place the patch fails in some
83 > unusual circumstance.. but I think it could easily use `-R` here.
84
85 >> 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]]
86
87         +               exec @cmdline or error("Cannot exec '@cmdline': $!");
88         +       }
89         +       # In parent.
90         +
91         +       # hg output is probably utf-8 encoded, but may contain
92         +       # other encodings or invalidly encoded stuff. So do not rely
93         +       # on the normal utf-8 IO layer, decode it by hand.
94         +       binmode($OUT);
95
96 > Is this actually true for hg?
97
98 >> 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.
99
100 >>> *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]]
101
102         +       my @lines;
103         +       while (<$OUT>) {
104         +               $_=decode_utf8($_, 0);
105         +
106         +               chomp;
107         +
108         +               if (! defined $data_handler) {
109         +                       push @lines, $_;
110         +               }
111         +               else {
112         +                       last unless $data_handler->($_);
113         +               }
114         +       }
115         +
116         +       close $OUT;
117         +
118         +       $error_handler->("'@cmdline' failed: $!") if $? && $error_handler;
119         +
120         +       return wantarray ? @lines : ($? == 0);
121         +}
122         +# Convenient wrappers.
123         +sub run_or_die ($@) { safe_hg(\&error, undef, @_) }
124         +sub run_or_cry ($@) { safe_hg(sub { warn @_ }, undef, @_) }
125         +sub run_or_non ($@) { safe_hg(undef, undef, @_) }
126         +
127          sub mercurial_log ($) {
128                 my $out = shift;
129                 my @infos;
130         @@ -116,10 +174,7 @@
131          }
132          
133          sub rcs_update () {
134         -       my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "update");
135         -       if (system(@cmdline) != 0) {
136         -               warn "'@cmdline' failed: $!";
137         -       }
138         +       run_or_cry('hg', '-q', 'update');
139          }
140          
141          sub rcs_prepedit ($) {
142
143 With the `run_or_{die,cry,non}()` functions defined as in `git.pm`, some old Mercurial functions can be rewritten more compactly.
144
145         @@ -129,6 +184,14 @@
146          sub rcs_commit (@) {
147                 my %params=@_;
148          
149         +       return rcs_commit_helper(@_);
150         +}
151         +
152         +sub rcs_commit_helper (@) {
153         +       my %params=@_;
154         +
155         +       my %env=%ENV;
156
157 > This `%env` stash is unused; `%ENV` is never modified.
158
159 >> 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]]
160
161         +
162                 my $user="Anonymous";
163                 if (defined $params{session}) {
164                         if (defined $params{session}->param("name")) {
165
166 Here comes the `rcs_commit{,_staged}` part. It is modeled on a `rcs_commit_helper` function, as in `git.pm`.
167
168 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.
169
170 > Exactly how to encode the nickname from openid in the commit metadata,
171 > and get it back out in `rcs_recentchanges`, would probably vary from git.
172
173 >> 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]]
174
175 >>> I adapted some logic from `git.pm`. `hg` only has a single commiter name field, whereas `git` has both `GIT_AUTHOR_NAME` and `GIT_AUTHOR_EMAIL`. The behaviour can be emulated by encoding nick and commit medium into commiter name as "`https://www.google.com/accounts/o8/id?id=AItOawmUIes3yDLfQME0uvZvJKDN0NsdKPx_PTw <Daniel@web>`" and parsing this out as necessary when `rcs_recentchanges` is called. *Done* --[[Daniel Andersson]]
176
177         @@ -143,43 +206,45 @@
178                         $params{message} = "no message given";
179                 }
180          
181         -       my @cmdline = ("hg", "-q", "-R", $config{srcdir}, "commit", 
182         -                      "-m", IkiWiki::possibly_foolish_untaint($params{message}),
183         -                      "-u", IkiWiki::possibly_foolish_untaint($user));
184         -       if (system(@cmdline) != 0) {
185         -               warn "'@cmdline' failed: $!";
186         +       $params{message} = IkiWiki::possibly_foolish_untaint($params{message});
187         +
188         +       my @opts;
189         +       
190         +       if (exists $params{file}) {
191         +               push @opts, '--', $params{file};
192                 }
193         -
194         +       # hg commit returns non-zero if nothing really changed.
195         +       # So we should ignore its exit status (hence run_or_non).
196         +       run_or_non('hg', 'commit', '-m', $params{message}, '-q', @opts);
197         +       
198         +       %ENV=%env;
199                 return undef; # success
200          }
201          
202          sub rcs_commit_staged (@) {
203                 # Commits all staged changes. Changes can be staged using rcs_add,
204                 # rcs_remove, and rcs_rename.
205         -       my %params=@_;
206         -       
207         -       error("rcs_commit_staged not implemented for mercurial"); # TODO
208         +       return rcs_commit_helper(@_);
209          }
210          
211          sub rcs_add ($) {
212                 my ($file) = @_;
213          
214         -       my @cmdline = ("hg", "-q", "-R", "$config{srcdir}", "add", "$config{srcdir}/$file");
215         -       if (system(@cmdline) != 0) {
216         -               warn "'@cmdline' failed: $!";
217         -       }
218         +       run_or_cry('hg', 'add', $file);
219          }
220          
221          sub rcs_remove ($) {
222         +       # Remove file from archive.
223         +
224                 my ($file) = @_;
225          
226         -       error("rcs_remove not implemented for mercurial"); # TODO
227         +       run_or_cry('hg', 'remove', '-f', $file);
228          }
229          
230          sub rcs_rename ($$) {
231                 my ($src, $dest) = @_;
232          
233         -       error("rcs_rename not implemented for mercurial"); # TODO
234         +       run_or_cry('hg', 'rename', '-f', $src, $dest);
235          }
236          
237          sub rcs_recentchanges ($) {
238
239 > Remainder seems ok to me. Should probably test that the remove plugin 
240 > works, since this implements `rcs_remove` too and you didn't mention
241 > any tests that would run that code path. --[[Joey]] 
242
243 >> 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`:
244
245     [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
246     [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
247     [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
248     [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
249
250 >>> I added setting the environment variable `HGENCODING=utf-8` in `rcs_commit_helper`, which took care of these problems. *Done* --[[Daniel Andersson]]
251
252 >> 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]]
253
254 >> 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]]
255
256 >>> 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]]