考虑以下列方式使用 JGit 控制 git-pull -
如何访问这些有冲突的文件(例如我们和他们的文件),或者至少访问他们的ObjectId?
这是一个插图
oFetchCommand.call()
...
MergeCommand oMergeCommand = m_oGit.merge();
oMergeCommand.setCommit(false); // dry run
MergeResult oMergeResult = oMergeCommand.call();
if (oMergeResult.getConflicts() != null)
{
// ?
// TODO - read the complete 'our' and 'their' files,
// show them in an external editor for manual merge.
}
...
我想最佳解决方案是获取两个冲突文件的ObjectId。
答案 0 :(得分:0)
您可以像这样处理冲突文件:
Map allConflicts = oMergeResult.getConflicts();
for (String file : allConflicts.keySet()) {
//Call to your external merge tool (this suppose
//kdiff3 is in your PATH)
Runtime rt = Runtime.getRuntime();
Process pr = rt.exec("kdiff3" + file + "-m");
}
答案 1 :(得分:0)
要访问特定提交中的文件内容,您需要解析并解析您感兴趣的提交,然后使用TreeWalk
获取文件内容的object-id,最后使用ObjectLoader
访问内容。
例如:
// Skip the next three lines if you have a fully parse RevCommit at hand
RevWalk revWalk = new RevWalk( repository );
RevCommit commit = revWalk.parseCommit( repository.resolve( commit-id ) );
revWalk.close();
String path = ... // repository-relative path to file
TreeWalk treeWalk = TreeWalk.forPath( repository, path, commit.getTree() );
ObjectId blodId = treeWalk.getObjectId( 0 ); // this is the objectId of the file
treeWalk.close();
ObjectLoader objectLoader = repository.open( blodId, Constants.OBJ_BLOB );
InputStream inputStream = objectLoader.openStream();
// use inputStream
inputStream.close();
答案 2 :(得分:-1)
git checkout-index --stage=all -- $path
吐出三个临时名称,持有三个索引阶段(1 2 3,我们自己的基础)。
git ls-files -us $path
列出了未合并的路径和阶段的索引ID