Jgit get files

I have one repository in two different folders ( folder1 and folder2 ). Inside the repositories there is an "oldFile" file

In folder1 perform the following actions:

 echo 123 > oldFile touch newFile git add newFile oldFile git commit -m "Change the oldFile from folder1 and add the newFile" oldFile newFile git push origin master 

After that, in folder2 do the following:

 echo zxc > oldFile; git add oldFile; git commit oldFile -m "Change oldFile from folder2" 

In this case, I want to get a merge conflict

And I want to see the pulled out files. I gave an example here .

 ObjectId oldHead = repository.resolve("HEAD^{tree}"); //save old objectId PullResult pullResult = pullCommand.setProgressMonitor(new TextProgressMonitor(new OutputStreamWriter(System.out))).call(); ObjectId head = repository.resolve("HEAD^{tree}"); ObjectReader reader = repository.newObjectReader(); CanonicalTreeParser oldTreeIter = new CanonicalTreeParser(); oldTreeIter.reset(reader, oldHead); CanonicalTreeParser newTreeIter = new CanonicalTreeParser(); newTreeIter.reset(reader, head); List<DiffEntry> diffs= git.diff() .setNewTree(newTreeIter) .setOldTree(oldTreeIter) .call(); 

In pullResult.getMergeResult().getMergeConflicts() I only have the oldFile file and the diffs collection diffs empty.

In the simple case (when I do not change the "OldFile" from folder2 ), the diffs compilation is not empty - it has 1 "newFile" file and pullResult.getMergeResult().getMergeConflicts() - emtpy (this is obvious).

How am I wrong? I want to get conflicting files and deleted files.

+7
java git jgit
source share
1 answer

I have found the answer. Need to use

ObjectId head = git.getRepository().resolve("refs/remotes/origin/HEAD^{tree}");

instead

ObjectId head = repository.resolve("HEAD^{tree}");

+4
source share

All Articles