12

Is there a way to programmatically download a single file from a remote Mercurial repository, in Java? I have asked a very similar question regarding git; now I'm hoping I can do something similar with mercurial as well.

  1. I prefer a solution which uses as little bandwidth as possible, preferably only downloading that single file. I do not need to browse the repository, I already have the file's path.
  2. I am not concerned with the history of the file, I only want its latest version.
  3. A solution that only prints the file to the output is great as well, of course - it doesn't really have to save the file to disk, I can do that myself.
  4. I prefer a solution which does not depend on other applications (e.g. an installation of a mercurial client on the machine). A Java library which contains a mercurial client implementation itself would be optimal. However, I will happily invoke hg if there's no other way.

From what I understand about how Mercurial works - allowing working only against local repositories - this could prove to be problematic; but as I was able to do this with the similar Git SCM I'm hoping there's a solution for Mercurial as well.

Community
  • 1
  • 1
Oak
  • 26,231
  • 8
  • 93
  • 152

2 Answers2

12

The mercurial wire protocol doesn't have a command for reading a single file from a remote repository. That's why the hg command line client can't do this either. The idea is that you should always make a local clone for such things.

However, the various web interfaces for mercurial typically have a way to get at file content. For example, for bitbucket repositories the URL looks like this:

http://bitbucket.org/<user>/<project>/raw/<revision>/<filename>

For the hg serve web interface, the URL looks like this:

http://<host>:<port>/raw-file/<revision>/<filename>
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Wim Coenen
  • 66,094
  • 13
  • 157
  • 251
  • 3
    In addition to that if you need to access private repo's on bitbucket, they have an api: http://confluence.atlassian.com/display/BITBUCKET/Using+the+Bitbucket+REST+APIs – dave Aug 01 '11 at 15:22
  • 2
    For the `hg server` web interface, you don't need to know the revision. You can use `tip` instead of the revision number. – Jade Jul 08 '12 at 03:03
3

The Bitbucket REST API is the tool you want for this.

https://api.bitbucket/1.0/repositories/{USER}/{REPO-NAME}/raw/tip/{PATH/TO/FILE}

Example from their docs:

Instead of getting the file formatted as JSON, you can get the raw file:

$ curl https://api.bitbucket.org/1.0/repositories/jespern/django-piston/raw/tip/piston/utils.py import time from django.http import HttpResponseNotAllowed, HttpResponseForbidden, HttpResponse, HttpResponseBadRequest from django.core....

saul.shanabrook
  • 3,068
  • 3
  • 31
  • 49