Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JGit get pulled files

Tags:

java

git

jgit

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.

like image 732
Andrew Bystrov Avatar asked Jul 15 '16 08:07

Andrew Bystrov


1 Answers

I found the answer. Need use

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

instead of

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

like image 77
Andrew Bystrov Avatar answered Oct 17 '22 11:10

Andrew Bystrov