使用JGit,我已经签出了一个分支(branch1
)。一段时间后,我想获取()并查看自上次获取以来哪些文件已更新(在原始位置)。
FetchResult fetchResult = m_git.fetch().setRefSpecs(new RefSpec("refs/heads/branch1")).call();
// For example:
fetchResult.getUpdatedFiles()
我该怎么做?
答案 0 :(得分:1)
提取API的粒度级别是提交,而不是文件。因此,提取本身不会告诉您哪些文件已更新。
FetchResult
保留了有关提取操作结果的详细信息。
遍历FetchResult::getTrackingRefUpdates
以查看所有已更新的引用(在您的示例中应该只是其中之一)。对于您感兴趣的每个更改的引用,TrackingRefUpdate
描述了相应引用的获取操作的结果。
首先查看result
属性,看看总体结果是否符合您的要求。对于常规的引用更新,请使用getOldObjectId
和getNewObjectId
来区分旧提交和新提交。
在此处查看如何与JGit进行比较:How to show changes between commits with JGit
答案 1 :(得分:0)
我发现了一些对我有用的东西。
大部分内容来自一本jgit食谱:https://github.com/centic9/jgit-cookbook/blob/master/src/main/java/org/dstadler/jgit/porcelain/ShowFileDiff.java#start-of-content
下面的DiffEntry集合向我展示了修改后的文件。 (仅供参考,可以在合并之前完成)
fetch();
// todo: isn't there a critical section here? Only within the same repo maybe. We could prevent simultaneous use of the same racfid.
Ref fetchHead;
try {
fetchHead = m_git.getRepository().findRef("FETCH_HEAD");
//todo: show the diff bewteen fetch-head and the current head.
// - get object id of fetch
// - get oid of head
ObjectId head = m_git.getRepository().resolve(Constants.HEAD);
AbstractTreeIterator oldTreeParser = prepareTreeParser(m_git.getRepository(), fetchHead.getObjectId());
AbstractTreeIterator newTreeParser = prepareTreeParser(m_git.getRepository(), head);
List<DiffEntry> diff = m_git.diff().
setOldTree(oldTreeParser).
setNewTree(newTreeParser).
// setPathFilter(PathFilter.create("README.md")).
// to filter on Suffix use the following instead
//setPathFilter(PathSuffixFilter.create(".java")).
call();
for (DiffEntry entry : diff) {
System.out.println("Entry: " + entry + ", from: " + entry.getOldId() + ", to: " + entry.getNewId());
}
}
private static AbstractTreeIterator prepareTreeParser(Repository repository, ObjectId objectId) throws IOException {
// from the commit we can build the tree which allows us to construct the TreeParser
//noinspection Duplicates
try (RevWalk walk = new RevWalk(repository)) {
RevCommit commit = walk.parseCommit(objectId);
RevTree tree = walk.parseTree(commit.getTree().getId());
CanonicalTreeParser treeParser = new CanonicalTreeParser();
try (ObjectReader reader = repository.newObjectReader()) {
treeParser.reset(reader, tree.getId());
}
walk.dispose();
return treeParser;
}
}