Java – jgit get pull file
I have a repository in two different folders (folder1 and folder2) There is an "oldfile" file in the repository
In folder1, I perform 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 folder2, I will do the next step:
echo zxc > oldFile; git add oldFile; git commit oldFile -m "Change oldFile from folder2"
In this case, I want to get a merger conflict
I want to see the pull file I got an 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 only have the file oldfile, and the diffs collection is empty
In a simple example (when I don't change "oldfile" from folder2), the diffs collection is not empty – it has a file "newfile" and pullresult getMergeResult(). Getmergeconflicts () is emtpy (obviously)
What did I do wrong? I want to get conflicting files and successful pull files
Solution
I found the answer Need to use
ObjectId head = git. getRepository(). resolve(“refs / remotes / origin / HEAD ^ {tree}”);
replace
ObjectId head = repository. resolve(“HEAD ^ {tree}”);