I want to be able to list the files and directories of a mercurial repository in a web browser using javascript.
views:
83answers:
1
+1
A:
If you're looking to parse the output of hgweb (the web front end for mercurial repositories) try appending ?style=raw
to the /file/ URLs to get some easily parseable text output:
This URL: http://hg.intevation.org/mercurial/crew/file/tip/?style=raw
Gets you this output
drwxr-xr-x contrib
drwxr-xr-x doc
drwxr-xr-x help
drwxr-xr-x hgext
drwxr-xr-x i18n
drwxr-xr-x mercurial
drwxr-xr-x templates
drwxr-xr-x tests
-rw-r--r-- 565 .hgignore
-rw-r--r-- 2168 .hgsigs
-rw-r--r-- 1291 .hgtags
-rw-r--r-- 1663 CONTRIBUTORS
-rw-r--r-- 17992 COPYING
-rw-r--r-- 3330 Makefile
-rw-r--r-- 306 README
-rwxr-xr-x 787 hg
-rwxr-xr-x 1251 hgeditor
-rw-r--r-- 886 hgweb.cgi
-rw-r--r-- 2280 hgwebdir.cgi
-rw-r--r-- 9521 setup.py
which is the root of the mercurial repo. To descend a directory, just add it into the URL, like this:
http://hg.intevation.org/mercurial/crew/file/tip/doc/?style=raw
You can get the contents of the files that same way.
Ry4an
2009-10-08 05:00:48
That's an excellent tip -- I knew about the `?style=raw` feature, but I have never really seen it used for anything useful (except Mercurial's test suite).
Martin Geisler
2009-10-09 07:38:19
Thanks. I don't need to tell you, I'm sure, but if hitting a hgweb you control you're better off building a custom template that uses proper escaping and better delimiters, but this is probably as good as it gets for repos in the wild.
Ry4an
2009-10-09 15:23:27
Thanks, this is exactly what I wanted. But is this dependent on the way the mercurial repository is exposed to the web? I remember reading that there are several different ways to make a mercurial repository public on the Internet.
AshleyS
2009-10-10 10:34:27
AshleyS, this type of output will be found on all mercurial repos on the web whether they're served from a stand-alone webserver (Apache, Lighty, etc.) or whether they served from `hg serve`. If they're available as static-http repos (very rare) it won't work, and for ssh-only repos (no web access) you'd need a different scheme as well. This, however, should work for any repo you find on the web.
Ry4an
2009-10-10 14:30:48