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.

4

5 回答 5

18

一个新选项是JavaHg,它为您提供高级 Java API。单元测试给出了如何使用它进行编程的一个很好的例子(从 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());
}

它与1.9 及更高版本中的Mercurial 命令服务器交互。这意味着将有一个持续的 Mercurial 进程接受多个命令,因此您可以避免通常与启动 Mercurial 相关的启动开销。我们预计它将在即将发布的 MercurialEclipse 版本中使用。(我是 JavaHg 的作者之一。)

于 2011-11-02T13:47:03.400 回答
8

hg4j现在有更多(即克隆)功能并且似乎正在实际开发中

于 2011-11-01T14:27:30.010 回答
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.

于 2008-12-19T08:49:51.723 回答
3

Maven SCM 插件似乎有一个Mercurial提供程序可用。但是,我不知道该提供程序在您的情况下的适用性(即它与 Maven 架构的关联程度和/或它如何与 hg 交互)。

于 2009-05-10T23:07:14.607 回答
3

还有一个hg4j,但目前它只允许读取存储库。

于 2011-08-06T22:10:27.577 回答