Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mercurial API for Java?

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.

like image 376
Thilo Avatar asked Dec 19 '08 08:12

Thilo


5 Answers

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.)

like image 166
Martin Geisler Avatar answered Nov 13 '22 17:11

Martin Geisler


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

like image 45
Mykel Alvis Avatar answered Nov 13 '22 18:11

Mykel Alvis


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.

like image 5
Keltia Avatar answered Nov 13 '22 19:11

Keltia


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).

like image 3
andri Avatar answered Nov 13 '22 19:11

andri


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

like image 3
MKP Avatar answered Nov 13 '22 18:11

MKP