29

Is there a plain API to access Mercurial repositories from Java?

There are plugins for Netbeans and Eclipse, but unlike their Subversion counterparts, they do not use a common lower-level library but bring their own wrappers to call out to the Mercurial binary. Calling the binary would be okay (for now), but it seems very difficult to use those plugins in standalone applications (outside of the IDE they were built for).

There is also HgKit, but that is very pre-alpha.

Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
Thilo
  • 257,207
  • 101
  • 511
  • 656

5 Answers5

19

A new option is JavaHg, which gives you a high-level Java API. The unit tests give a good example of how it is to program with it (as of JavaHg 0.1):

public void commitTest() throws IOException {
    Repository repo = getTestRepository();
    writeFile("x", "abc");

    CommitCommand commit = CommitCommand.on(repo);
    StatusCommand status = StatusCommand.on(repo);

    List<StatusLine> statusLines = status.lines();
    Assert.assertEquals(1, statusLines.size());
    Assert.assertEquals(StatusLine.Type.UNKNOWN, statusLines.get(0).getType());

    AddCommand.on(repo).execute();
    statusLines = status.lines();
    Assert.assertEquals(1, statusLines.size());
    Assert.assertEquals(StatusLine.Type.ADDED, statusLines.get(0).getType());

    commit.message("Add a file").user("Martin Geisler");
    Changeset cset = commit.execute();
    Assert.assertEquals("Martin Geisler", cset.getUser());
    statusLines = status.lines();
    Assert.assertEquals(0, statusLines.size());
}

It interacts with the Mercurial command server present in version 1.9 and later. This means that there will be a persistent Mercurial process around that accepts multiple commands and so you avoid the startup overhead normally associated with launching Mercurial. We expect that it will be used in a coming version of MercurialEclipse. (I'm one of the authors of JavaHg.)

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Martin Geisler
  • 72,968
  • 25
  • 171
  • 229
  • hm, it looks like it is not a pure implementation, but rather a wrapper over binary - or am I wrong? – shabunc Feb 17 '14 at 20:13
  • 2
    @shabunc: you're correct. JavaHg is using the only stable API there is for Mercurial: the command line. This means that it will keep working if the underlying data structures change. However, you're correct that unlike, say, JGit, it isn't a Java-only library since you need to install Mercurial too. – Martin Geisler Feb 17 '14 at 21:55
  • Is https://foss.heptapod.net/mercurial/javahg the successor or a different project with the same name? – sschuberth Jan 20 '23 at 12:10
8

hg4j has more (i.e. clone) functionality now and appears to be under actual development

Mykel Alvis
  • 1,048
  • 11
  • 14
5

Have you looked at Jython? As far as I can see here, it should help using the python mercurial modules from within a Java environment, just like JRuby does for Ruby.

Keltia
  • 14,535
  • 3
  • 29
  • 30
  • apparently, Jython cannot run Mercurial just yet, partly because of the sections of Mercurial that are written in C. – Thilo Dec 19 '08 at 09:04
  • Mercurial has back-ported most C modules to Python. I think they're almost, if not completely, done with that. – Macke May 13 '09 at 21:53
  • 5
    We are done, you can now use a --pure flag to setup.py in order to use the modules found under mercurial/pure. But note that this comes with a large speed penalty (I don't know how Jython fares, though). – Martin Geisler May 24 '09 at 12:48
3

There is also a hg4j but for now it only allows reading the repository.

MKP
  • 190
  • 2
  • 11
3

The Maven SCM plugin seems to have a Mercurial provider available. However, I don't know how applicable that provider is in your case (ie how deeply it is tied to Maven architecture and/or how it interfaces with hg).

andri
  • 11,171
  • 2
  • 38
  • 49
  • That does look promising. It uses Maven interfaces, of course, but I guess/hope that those can be used outside of Maven as well, at least easier than the graphical plugins I mentioned in the question. As for interfacing with hg, it calls the command-line program (which is fine). – Thilo May 11 '09 at 00:33