Search code examples
javajavahg

Run hg log command from java in an existing branch


I am trying to use JavaHg to run some hg commands from Java.

I already have an existing hg working directory.

:pwd
/Users/theodore/Work/proj1
:hg identify -b
PROJ1_FEATUREX_BRANCH

I want to connect to this working directory using JavaHg and run hg log command.

This is how far I got:

public static void main(String[] args) {
    RepositoryConfiguration conf = new RepositoryConfiguration();
    conf.setHgBin("/usr/local/bin/hg"); //Path to HG executable

    Repository repo = Repository.create(conf, new File("/Users/theodore/Work/proj1"));


    LogCommand log = LogCommand.on(repo);
    List<Changeset> changesets = log.execute();

    System.out.println(changesets);
    for(int i=0;i<changesets.size();i++) {
        Changeset cs = changesets.get(i);
        System.out.println( cs.getUser());
        System.out.println( cs.getMessage());
        System.out.println( cs.getAddedFiles() );
        System.out.println( cs.getModifiedFiles() );
    }

    repo.close();
}

But, the above code is trying to create a new repository in that folder each time.

So, it fails with this error:

Exception in thread "main" java.lang.RuntimeException: abort: repository /Users/theodore-3428/eclipse-workspace/hgviewer/~/DISKS/Work/CODE/HG/sdplive already exists!

Solution

  • Check the source code for Repository class. You need to use Repository.open(RepositoryConfiguration, File) isntead of Repository.create(RepositoryConfiguration, File), so create repo object like this:

    Repository repo = Repository.open(conf, new File("/Users/theodore/Work/proj1"));