我在两个不同的文件夹(folder1和folder2)中有一个存储库.在存储库中有一个“oldFile”文件
在folder1中,我执行以下步骤:
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
之后,在folder2中我会做下一步:
echo zxc > oldFile; git add oldFile; git commit oldFile -m "Change oldFile from folder2"
在这种情况下,我想得到一个合并冲突
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();
在pullResult.getMergeResult().getMergeConflicts()我只有文件oldFile,而diffs集合是空的.
在一个简单的例子中(当我不从folder2更改“OldFile”时)diffs集合不为空 – 它有1个文件“newFile”和pullResult.getMergeResult().getMergeConflicts()是emtpy(显然).
解决方法
我找到了答案.需要使用
ObjectId head = git.getRepository().resolve(“refs / remotes / origin / HEAD ^ {tree}”);
代替
ObjectId head = repository.resolve(“HEAD ^ {tree}”);