4

I know the support for git submodules is limited in JGit, but still was wondering how can I achieve this:

git submodule foreach git checkout <branchName>

and similar commands using JGit.

Or is there any other better Java based API for Git?

Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
yogytes
  • 53
  • 4

1 Answers1

5

In JGit there is a SubmoduleWalk that might help with your problem. To run a checkout command on all submodules, you would write something along these lines:

try (SubmoduleWalk walk = SubmoduleWalk.forIndex(repository)) {
  while(walk.next()) {
    try (Repository submoduleRepository = walk.getRepository()) {
      Git.wrap(submoduleRepository).checkout().call();
    }
  }
}
Rüdiger Herrmann
  • 20,512
  • 11
  • 62
  • 79
  • 1
    i see this "org.eclipse.jgit.api.errors.InvalidRefNameException: Branch name is not allowed". any ideas? – Srinivas Mar 01 '15 at 06:35
  • 1
    @Srinivas What have you done so far to solve the problem? If you need help please post a question with a [minimal, complete and verifiable example](http://stackoverflow.com/help/mcve). – Rüdiger Herrmann Mar 01 '15 at 12:31
  • 1
    https://github.com/srinivasupadhya/git-cmd/blob/master/src/com/tw/go/plugin/jgit/JGitHelper.java - cloneOrFetch() - checkoutSubmodule() throws that exception – Srinivas Mar 01 '15 at 12:44
  • The issue here is that the submodule walk doesn't pull the child module's branch from the .gitsubmodules file to properly allow this checkout. – Doug Jul 07 '15 at 15:46
  • @Doug You are right, I seems like, in JGit, you need to manually read from the `.gitsubmodules` file (e.g. using `StoredConfig`) and pass the branch name to the `CheckoutCommand`. Regarding this topic, I've found [this post](http://stackoverflow.com/questions/1777854/git-submodules-specify-a-branch-tag) useful in that it explains what to do in native git. – Rüdiger Herrmann Jul 09 '15 at 08:11
  • is this issue is solved. I am passing branch-name,still getting null pointer exception – Sohan Feb 19 '16 at 11:06
  • I am afraid I don't understand your question. If you see the ÌnvalidRefNameException` as described in the firs comment, the issues is apparently not yet resolved. If you face a different problem, please provide more context or ask a new question if necessary. – Rüdiger Herrmann Feb 19 '16 at 13:32