JGit checkout vs `git checkout` problems

tl; dr JGit checkout throws exceptions and the git checkout command works fine

I am currently trying to use JGit to check for some changes from the Git online repository, in Java (for work). My current approach (and I'm very new to Git coming from SVN background, so this might be wrong):

  • clone the repository to a temporary location on my hard drive
  • figure out which revision I want (I tried using the SHA-1 hash as well as the branch name)
  • check that version
  • from there, I would use the extracted files as contributions to a later part of the program.
  • checking another version
  • use these files as input for another part of the program.

Essentially, I want to be able to change the contents of my temporary folder with any version. With the command line interface, I was able to do this with git checkout master and git checkout dylanbranch (where dylanbranch is the branch I made on my own clone with an arbitrarily selected revision), but my Java code tries to do the same thing fails:

 Git git = Git.open(new File("checkout")); //checkout is the folder with .git git.checkout().setName("master").call(); //succeeds git.checkout().setName("dylanbranch").call(); //fails 

And the exceptions printed on the console:

 Exception in thread "main" org.eclipse.jgit.api.errors.JGitInternalException: Checkout conflict with files: src/sizzle test/qunit at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:211) at com.avi.scm.git.BranchCheckout.main(BranchCheckout.java:30) Caused by: org.eclipse.jgit.errors.CheckoutConflictException: Checkout conflict with files: src/sizzle test/qunit at org.eclipse.jgit.dircache.DirCacheCheckout.checkout(DirCacheCheckout.java:387) at org.eclipse.jgit.api.CheckoutCommand.call(CheckoutCommand.java:162) ... 1 more 

I can verify that the files in question are marked as deleted and not set for commit using git status , although I’m not sure why these changes exist, and they come back anytime when I switch back to the main branch, Despite this, I can still successfully change the working tree with the Git command line.

So my question is: Why won't JDit work for this application when there is Git on the command line? Any other useful information is appreciated - educate me :)

Update . I tested the jQuery repository and noticed some more problems with JGit: when I work with the "master" branch, git status makes me wonder, m #On branch master and that there is nothing to commit (working directory clean) , but using the JGit status command I see that test/qunit and src/sizzle are marked as Missing . JGit reset does nothing.

+8
java git jgit
source share
3 answers

The two directories mentioned in the stack trace are Git submodules ( test/qunit and src/sizzle ), which is most likely the cause of the problem, since JGit does not yet have full support for the submodules.

This may work differently in version 1.1 of JGit released this month based on this commit .

Read more about the current state of support for the JGit submodule here .

+2
source share

I know this does not directly answer your question, but I also had problems with Java Git implementations. What worked best for me is to simply disable the java implementation and make git command line calls from the application. It may not be ideal, but it will do exactly what you need, since you are in complete control of the team.

Just call Runtime.getRuntime().exec(...)

Javadoc Link for Runtime Class

0
source share

I had a similar problem with Checkout. I think the fact that you can switch the branch with uninstalled content in Git is actually a tolerance, not a function. JGit is not globally tolerant like Git, so you should usually test many cases.

This doesn't seem to be your problem (which is related to submodules), but for more general cases, you would like to commit your changes before using checkout to switch to another branch.

Please note that CheckoutCommand works great for me to start a new branch from the old revision (you must set the branch name and initial version).

0
source share

All Articles