Andrew Bystrov Andrew Bystrov - 4 months ago 15
Git Question

JGit get pulled files

I have one repository in two different folders (

folder1
and
folder2
). Within repositories has a
"oldFile"
file

In the
folder1
I do the following steps:

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 the
folder2
I do next steps:

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 pulled files. I got the example from 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 have only file
oldFile
and the
diffs
collection is empty.

In a simple case ( when I don't change the "OldFile" from
folder2
) the diffs collection is not empty - it has 1 the file "newFile" and
pullResult.getMergeResult().getMergeConflicts()
is emtpy ( it's obviously).

What do I do wrong? I want to get conflicted files and successful pulled files.

Answer

I found the answer. Need use

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

instead of

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