如何在不改變工作目錄的情況下使用JGit獲取所有提交的分支?JGit:如何獲得分支的所有提交? (沒有改變工作目錄...)
不幸的是,JGit文檔是不是很好...
在砂礫紅寶石它很容易:
repo = Grit::Repo.new(pathToRepo)
repo.commits(branchName, false).each do |commit|
doSomethingWithTheCommit
end
再見, hurik
如何在不改變工作目錄的情況下使用JGit獲取所有提交的分支?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());
}
}
}
}
你是對的,文檔應該真的更好。
您可以使用JGit Log command或使用像gitective這樣的庫,這使得迭代提交更容易。您可以查看源代碼以瞭解有關JGit的更多信息。
this SO-question中描述了其他(更復雜的)方法。
有了下面你的代碼可以得到所有分支或標記的提交:
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
代表代表分支或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語句將是必要的,我隱藏起來,使代碼更短。 祝你好運。
一個更短的和清晰的解決方案可以通過「日誌」命令JGit提供完成:
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");
當我爲我的分支做這件事時,我看到它和主人的提交。當使用主人時,我只看到它的提交。我期望「 - 停止複製」行爲,但這不是我所看到的git.log()。add(git.repository.resolve(「refs/heads/mybranch」))。call() – 2016-02-10 22:10:20
好的,那麼答案http://stackoverflow.com/a/35327138/411846中列出的addRange()選項應該可以工作。 – centic 2016-02-11 11:20:16
謝謝。我想我需要得到分支的頭,然後走回來,直到我找到一個RevCommit,其中所有的父母都來自另一個分支。沿途任何提交都可能有來自其他分支的多個父母,但只有來自另一個分支的父母的提交必須是分支的開頭。 – 2016-02-11 16:07:21
這似乎是工作,但我想補充(的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()
我只是在尋找一種方式,列出在EA所有提交ch分支,我發現這個線程。我終於做了一些與我在這裏找到並且工作的一些答案有所不同的東西。
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();
}
感謝您的幫助
我有這樣的解決方案,它需要大約五分鐘,芬蘭對我相當大的回購問題。 – user3495816 2016-02-02 09:51:07
請嘗試一些其他解決方案,這裏張貼。 – hurik 2016-04-08 09:53:30