如何在不更改工作目录的情况下获取JGit分支的所有提交?
不幸的是,JGit文档不是很好......
用砂砾红宝石很容易:
repo = Grit::Repo.new(pathToRepo)
repo.commits(branchName, false).each do |commit|
doSomethingWithTheCommit
end
再见 hurik
答案 0 :(得分:15)
首先在此尝试:https://stackoverflow.com/a/13925765/2246865
但它始终没有工作,所以我在这里找到了这个:http://www.eclipse.org/forums/index.php/t/280339/
在这里我的解决方案,它并不是很好,但它正在工作......
public static void main(String[] args) throws IOException, GitAPIException {
Repository repo = new FileRepository("pathToRepo/.git");
Git git = new Git(repo);
RevWalk walk = new RevWalk(repo);
List<Ref> branches = git.branchList().call();
for (Ref branch : branches) {
String branchName = branch.getName();
System.out.println("Commits of branch: " + branch.getName());
System.out.println("-------------------------------------");
Iterable<RevCommit> commits = git.log().all().call();
for (RevCommit commit : commits) {
boolean foundInThisBranch = false;
RevCommit targetCommit = walk.parseCommit(repo.resolve(
commit.getName()));
for (Map.Entry<String, Ref> e : repo.getAllRefs().entrySet()) {
if (e.getKey().startsWith(Constants.R_HEADS)) {
if (walk.isMergedInto(targetCommit, walk.parseCommit(
e.getValue().getObjectId()))) {
String foundInBranch = e.getValue().getName();
if (branchName.equals(foundInBranch)) {
foundInThisBranch = true;
break;
}
}
}
}
if (foundInThisBranch) {
System.out.println(commit.getName());
System.out.println(commit.getAuthorIdent().getName());
System.out.println(new Date(commit.getCommitTime() * 1000L));
System.out.println(commit.getFullMessage());
}
}
}
}
答案 1 :(得分:13)
使用下面的代码,您可以获得分支或标记的所有提交:
Repository repository = new FileRepository("/path/to/repository/.git");
String treeName = "refs/heads/master"; // tag or branch
for (RevCommit commit : git.log().add(repository.resolve(treeName)).call()) {
System.out.println(commit.getName());
}
变量treeName
将定义标记或分支。此treeName
是分支或标记的完整名称,例如 master 分支的refs/heads/master
或{em> v1.0的标记的refs/tags/v1.0
或者,您可以使用gitective API。以下代码与上面的代码相同:
Repository repository = new FileRepository("/path/to/repository/.git");
AndCommitFilter filters = new AndCommitFilter();
CommitListFilter revCommits = new CommitListFilter();
filters.add(revCommits);
CommitFinder finder = new CommitFinder(repository);
finder.setFilter(filters);
String treeName = "refs/heads/master"; // tag or branch
finder.findFrom(treeName);
for (RevCommit commit : revCommits) {
System.out.println(commit.getName());
}
有些try / catch是必要的,我隐藏它们以缩短代码。 祝你好运。
答案 2 :(得分:2)
我只是想找到一种方法来列出每个分支下的所有提交,我找到了这个帖子。我终于做了一些与我在这里找到的工作有所不同的工作。
public static void main(String[] args) throws IOException, GitAPIException {
Repository repo = new FileRepository("pathToRepo/.git");
Git git = new Git(repo);
List<Ref> branches = git.branchList().call();
for (Ref branch : branches) {
String branchName = branch.getName();
System.out.println("Commits of branch: " + branchName);
System.out.println("-------------------------------------");
Iterable<RevCommit> commits = git.log().add(repo.resolve(branchName)).call();
List<RevCommit> commitsList = Lists.newArrayList(commits.iterator());
for (RevCommit commit : commitsList) {
System.out.println(commit.getName());
System.out.println(commit.getAuthorIdent().getName());
System.out.println(new Date(commit.getCommitTime() * 1000L));
System.out.println(commit.getFullMessage());
}
}
git.close();
}
感谢您的帮助
答案 3 :(得分:0)
你是对的,文档应该真的更好。
您可以使用JGit Log command或使用像gitective这样的库,这样可以轻松地迭代提交。您可以查看源代码以了解有关JGit的更多信息。
this SO-question中描述了其他(更复杂的)方法。
答案 4 :(得分:0)
可以通过JGit提供的“log”命令完成更简洁的解决方案:
Repository repository = new FileRepository("pathToRepo/.git");
logs = new Git(repository).log()
.add(repository.resolve("remotes/origin/testbranch"))
.call();
count = 0;
for (RevCommit rev : logs) {
System.out.println("Commit: " + rev /* + ", name: " + rev.getName() + ", id: " + rev.getId().getName() */);
count++;
}
System.out.println("Had " + count + " commits overall on test-branch");
请参阅full snippet
上的jgit-cookbook答案 5 :(得分:0)
这似乎有效,但我想添加(ObjectId分支)为活动分支提供一个门,但我似乎需要一个范围
Git git = ...
ObjectId master = git.repository.resolve("refs/heads/master")
ObjectId branch = git.repository.resolve("refs/heads/mybranch")
git.log().addRange(master,branch).call()