如何使用 JGit 查找最后更改文件的人
How to find who last changed a file with JGit
我需要在我们的 GIT 存储库中最后 changed/committed 一个文件的用户。
项目包含很多子文件夹,master分支有很多合并提交。
我试过了:
File gitWorkDir = new File("D:/gitfolder/");
Git git = Git.open(gitWorkDir);
RevCommit latestCommit;
String path = "test.txt";
try( RevWalk revWalk = new RevWalk( git.getRepository() ) ) {
Ref headRef = git.getRepository().exactRef( Constants.HEAD );
RevCommit headCommit = revWalk.parseCommit( headRef.getObjectId());
revWalk.markStart( headCommit );
revWalk.sort( RevSort.COMMIT_TIME_DESC );
TreeFilter filter = AndTreeFilter.create( PathFilter.create( path ), TreeFilter.ANY_DIFF );
revWalk.setTreeFilter(filter);
latestCommit = revWalk.next();
git.close();
}
存储库和 HEAD 提交在那里,但 latestCommit
始终为空。
我必须定义文件的完整路径吗?
master 分支是如此 "branched out" 所以可能找不到提交,或者还有什么问题可能是个问题?
在 git 中我会使用 git log --follow path/to/your/file
。它为您提供了特定文件的完整git历史(包括提交作者,即使文件名已更改或文件已移动到另一个文件夹)。
要使用 JGit 执行此操作,请查看 this Whosebug question。
特别是这部分应该很有趣(取自上面的答案):
/**
* Returns the result of a git log --follow -- < path >
* @return
* @throws IOException
* @throws MissingObjectException
* @throws GitAPIException
*/
public ArrayList<RevCommit> call() throws IOException, MissingObjectException, GitAPIException {
ArrayList<RevCommit> commits = new ArrayList<RevCommit>();
git = new Git(repository);
RevCommit start = null;
do {
Iterable<RevCommit> log = git.log().addPath(path).call();
for (RevCommit commit : log) {
if (commits.contains(commit)) {
start = null;
} else {
start = commit;
commits.add(commit);
}
}
if (start == null) return commits;
}
while ((path = getRenamedPath( start)) != null);
return commits;
}
我需要在我们的 GIT 存储库中最后 changed/committed 一个文件的用户。
项目包含很多子文件夹,master分支有很多合并提交。
我试过了:
File gitWorkDir = new File("D:/gitfolder/");
Git git = Git.open(gitWorkDir);
RevCommit latestCommit;
String path = "test.txt";
try( RevWalk revWalk = new RevWalk( git.getRepository() ) ) {
Ref headRef = git.getRepository().exactRef( Constants.HEAD );
RevCommit headCommit = revWalk.parseCommit( headRef.getObjectId());
revWalk.markStart( headCommit );
revWalk.sort( RevSort.COMMIT_TIME_DESC );
TreeFilter filter = AndTreeFilter.create( PathFilter.create( path ), TreeFilter.ANY_DIFF );
revWalk.setTreeFilter(filter);
latestCommit = revWalk.next();
git.close();
}
存储库和 HEAD 提交在那里,但 latestCommit
始终为空。
我必须定义文件的完整路径吗? master 分支是如此 "branched out" 所以可能找不到提交,或者还有什么问题可能是个问题?
在 git 中我会使用 git log --follow path/to/your/file
。它为您提供了特定文件的完整git历史(包括提交作者,即使文件名已更改或文件已移动到另一个文件夹)。
要使用 JGit 执行此操作,请查看 this Whosebug question。
特别是这部分应该很有趣(取自上面的答案):
/**
* Returns the result of a git log --follow -- < path >
* @return
* @throws IOException
* @throws MissingObjectException
* @throws GitAPIException
*/
public ArrayList<RevCommit> call() throws IOException, MissingObjectException, GitAPIException {
ArrayList<RevCommit> commits = new ArrayList<RevCommit>();
git = new Git(repository);
RevCommit start = null;
do {
Iterable<RevCommit> log = git.log().addPath(path).call();
for (RevCommit commit : log) {
if (commits.contains(commit)) {
start = null;
} else {
start = commit;
commits.add(commit);
}
}
if (start == null) return commits;
}
while ((path = getRenamedPath( start)) != null);
return commits;
}