[英]JGit: How to get all commits of a branch? (Without changes to the working directory …)
如何在不更改工作目錄的情況下獲取JGit分支的所有提交?
不幸的是JGit文檔不是很好......
用砂礫紅寶石很容易:
repo = Grit::Repo.new(pathToRepo)
repo.commits(branchName, false).each do |commit|
doSomethingWithTheCommit
end
再見,hurik
首先在這里嘗試: 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());
}
}
}
}
使用下面的代碼,您可以獲得分支或標記的所有提交 :
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());
}
varialbe treeName
將定義標記或分支。 此treeName
是分支或標記的完整名稱,例如主分支的refs/heads/master
或名為v1.0的標記的refs/tags/v1.0
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是必要的,我隱藏它們以使代碼更短。 祝好運。
我只是想找到一種方法來列出每個分支下的所有提交,我找到了這個帖子。 我終於做了一些與我在這里找到和工作的一些答案有所不同的東西。
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();
}
謝謝您的幫助
你說得對,該文檔確實應該更好。
您可以使用JGit Log命令或使用像gitective這樣的庫,這樣可以輕松地迭代提交。 您可以查看源代碼以了解有關JGit的更多信息。
在這個SO問題中描述了其他(更復雜的)方法。
可以通過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");
這似乎工作,但我認為添加(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()
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.