Skip to content

The Unified and Autonomous Development Platform

Improve AheadBehindCommand to be able to calculate multiple branches against a base branch efficiently.

robin shinece06c8167bd6
diff --git a/commons.git/src/main/java/com/pmease/commons/git/Git.java b/commons.git/src/main/java/com/pmease/commons/git/Git.java
index 09c07b46f7..97dcf3a401 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/Git.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/Git.java
@@ -460,9 +460,9 @@ public class Git implements Serializable {
return new ListBranchCommand(repoDir).call();
}
- public AheadBehind getAheadBehind(String leftRev, String rightRev) {
+ public Map<String, AheadBehind> getAheadBehinds(String baseRev, String... compareRevs) {
AheadBehindCommand cmd = new AheadBehindCommand(repoDir);
- return cmd.leftRev(leftRev).rightRev(rightRev).call();
+ return cmd.baseCommit(baseRev).compareRevs(compareRevs).call();
}
/**
diff --git a/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java b/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java
index 37f119c83c..ae90dcf2d9 100644
--- a/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java
+++ b/commons.git/src/main/java/com/pmease/commons/git/command/AheadBehindCommand.java
@@ -1,65 +1,254 @@
package com.pmease.commons.git.command;
import java.io.File;
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.Stack;
import java.util.StringTokenizer;
+import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
import com.pmease.commons.git.AheadBehind;
+import com.pmease.commons.git.GitUtils;
import com.pmease.commons.util.execution.Commandline;
import com.pmease.commons.util.execution.LineConsumer;
-public class AheadBehindCommand extends GitCommand<AheadBehind> {
+public class AheadBehindCommand extends GitCommand<Map<String, AheadBehind>> {
private static final Logger logger = LoggerFactory.getLogger(AheadBehindCommand.class);
- private String leftRev;
+ private String baseRev;
- private String rightRev;
+ private String[] compareRevs;
public AheadBehindCommand(File repoDir) {
super(repoDir);
}
- public AheadBehindCommand leftRev(String leftRev) {
- this.leftRev = Preconditions.checkNotNull(leftRev);
+ public AheadBehindCommand baseCommit(String baseRev) {
+ this.baseRev = Preconditions.checkNotNull(baseRev);
return this;
}
- public AheadBehindCommand rightRev(String rightRev) {
- this.rightRev = Preconditions.checkNotNull(rightRev);
+ public AheadBehindCommand compareRevs(String... compareRevs) {
+ this.compareRevs = Preconditions.checkNotNull(compareRevs);
return this;
}
@Override
- public AheadBehind call() {
- Commandline cmd = cmd();
- cmd.addArgs("rev-list", "--left-right", "--count");
- cmd.addArgs(Preconditions.checkNotNull(leftRev, "leftRev")
- + "..." + Preconditions.checkNotNull(rightRev, "rightRev"));
-
- final AheadBehind counter = new AheadBehind();
- cmd.execute(new LineConsumer() {
-
- @Override
- public void consume(String line) {
- StringTokenizer tokenizer = new StringTokenizer(line);
- counter.setAhead(Integer.parseInt(tokenizer.nextToken()));
- counter.setBehind(Integer.parseInt(tokenizer.nextToken()));
+ public Map<String, AheadBehind> call() {
+ Preconditions.checkNotNull(baseRev);
+ Preconditions.checkArgument(compareRevs != null);
+
+ if (compareRevs.length > 1) {
+ final List<String> nonHashRevs = new ArrayList<>();
+ final Map<String, String> revHashes = new HashMap<>();
+ if (GitUtils.isHash(baseRev))
+ revHashes.put(baseRev, baseRev);
+ else
+ nonHashRevs.add(baseRev);
+ for (String each: compareRevs) {
+ if (GitUtils.isHash(each))
+ revHashes.put(each, each);
+ else
+ nonHashRevs.add(each);
}
- }, new LineConsumer() {
+ Commandline cmd = cmd();
+ cmd.addArgs("rev-parse");
+ for (String each: nonHashRevs)
+ cmd.addArgs(each);
+
+ final AtomicInteger index = new AtomicInteger(0);
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ revHashes.put(nonHashRevs.get(index.getAndIncrement()), line);
+ }
+
+ }, new LineConsumer() {
- @Override
- public void consume(String line) {
- logger.error(line);
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ cmd = cmd();
+ cmd.addArgs("rev-list", "--parents", "^" + revHashes.get(baseRev));
+ for (String each: compareRevs)
+ cmd.addArgs(revHashes.get(each));
+
+ final Map<String, Set<String>> commits = new HashMap<>();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ parseCommitLine(commits, line);
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ Map<String, AheadBehind> aheadBehinds = new HashMap<>();
+ for (String each: compareRevs) {
+ AheadBehind ab = new AheadBehind();
+ ab.setAhead(findAncestors(commits, revHashes.get(each)).size());
+ aheadBehinds.put(each, ab);
}
- });
-
- return counter;
- }
+ cmd = cmd();
+ cmd.addArgs("merge-base", "--all", "--octopus");
+ for (String each: compareRevs)
+ cmd.addArgs(revHashes.get(each));
+
+ final Set<String> mergeBases = new HashSet<>();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ StringTokenizer tokenizer = new StringTokenizer(line);
+ while (tokenizer.hasMoreTokens())
+ mergeBases.add(tokenizer.nextToken());
+ }
+
+ }, new LineConsumer() {
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ cmd = cmd();
+ cmd.addArgs("rev-list");
+ for (String each: mergeBases)
+ cmd.addArgs("^" + each);
+
+ cmd.addArgs(baseRev);
+
+ final Set<String> mergeBaseBehindBase = new HashSet<>();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ mergeBaseBehindBase.add(line);
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ cmd = cmd();
+ cmd.addArgs("rev-list", "--parents");
+ for (String each: mergeBases)
+ cmd.addArgs("^" + each);
+ for (String each: compareRevs)
+ cmd.addArgs(revHashes.get(each));
+
+ commits.clear();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ parseCommitLine(commits, line);
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ for (Map.Entry<String, AheadBehind> entry: aheadBehinds.entrySet()) {
+ String branchHash = revHashes.get(entry.getKey());
+ Set<String> copyOfMergeBaseBehindBase = new HashSet<>(mergeBaseBehindBase);
+ copyOfMergeBaseBehindBase.removeAll(findAncestors(commits, branchHash));
+ entry.getValue().setBehind(copyOfMergeBaseBehindBase.size());
+ }
+
+ return aheadBehinds;
+ } else if (compareRevs.length == 1) {
+ Commandline cmd = cmd();
+ cmd.addArgs("rev-list", "--left-right", "--count");
+ cmd.addArgs(baseRev + "..." + compareRevs[0]);
+
+ final AheadBehind aheadBehind = new AheadBehind();
+ cmd.execute(new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ StringTokenizer tokenizer = new StringTokenizer(line);
+ aheadBehind.setBehind(Integer.parseInt(tokenizer.nextToken()));
+ aheadBehind.setAhead(Integer.parseInt(tokenizer.nextToken()));
+ }
+
+ }, new LineConsumer() {
+
+ @Override
+ public void consume(String line) {
+ logger.error(line);
+ }
+
+ }).checkReturnCode();
+
+ Map<String, AheadBehind> aheadBehinds = new HashMap<>();
+ aheadBehinds.put(compareRevs[0], aheadBehind);
+ return aheadBehinds;
+ } else {
+ return new HashMap<String, AheadBehind>();
+ }
+ }
+
+ private void parseCommitLine(Map<String, Set<String>> commits, String line) {
+ Set<String> parents = new HashSet<>();
+ StringTokenizer tokenizer = new StringTokenizer(line);
+ String commit = tokenizer.nextToken();
+ while (tokenizer.hasMoreTokens())
+ parents.add(tokenizer.nextToken());
+ commits.put(commit, parents);
+ }
+
+ private Set<String> findAncestors(Map<String, Set<String>> commits, String commit) {
+ Set<String> ancestors = new HashSet<>();
+ Stack<String> stack = new Stack<>();
+ stack.push(commit);
+ while (!stack.isEmpty()) {
+ commit = stack.pop();
+ if (!ancestors.contains(commit)) {
+ Set<String> parents = commits.get(commit);
+ if (parents != null) {
+ ancestors.add(commit);
+ for (String parent: parents)
+ stack.push(parent);
+ }
+ }
+ }
+ return ancestors;
+ }
+
}
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branch/BranchChoiceProvider.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branch/BranchChoiceProvider.java
index 0f29bd0b6d..2e055080d3 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branch/BranchChoiceProvider.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/component/branch/BranchChoiceProvider.java
@@ -39,9 +39,15 @@ public class BranchChoiceProvider extends ChoiceProvider<Branch> {
criteria.addOrder(Order.asc("name"));
int first = page * Constants.DEFAULT_PAGE_SIZE;
- List<Branch> branches = GitPlex.getInstance(Dao.class).query(criteria, first, Constants.DEFAULT_PAGE_SIZE);
-
- response.addAll(branches);
+ List<Branch> branches = GitPlex.getInstance(Dao.class).query(criteria, first, Constants.DEFAULT_PAGE_SIZE+1);
+ if (branches.size() > Constants.DEFAULT_PAGE_SIZE) {
+ branches.remove(branches.size()-1);
+ response.addAll(branches);
+ response.setHasMore(true);
+ } else {
+ response.addAll(branches);
+ response.setHasMore(false);
+ }
}
@Override
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
index f5843d11fd..f67869545c 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/TestPage.java
@@ -23,8 +23,7 @@ public class TestPage extends BasePage {
@Override
public void onClick() {
Git git = new Git(new File("W:\\temp\\gitplex_storage\\repositories\\1"));
- for (int i=1; i<100; i++)
- git.createBranch("dev" + i, "master~" + i*10);
+ System.out.println(git.getAheadBehinds("master", "dev2", "dev3"));
}
});
diff --git a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
index 94b14104f7..a397c90bb7 100644
--- a/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
+++ b/gitplex.web/src/main/java/com/pmease/gitplex/web/page/repository/code/branches/RepoBranchesPage.java
@@ -34,7 +34,6 @@ import org.apache.wicket.model.Model;
import org.apache.wicket.request.mapper.parameter.PageParameters;
import com.google.common.base.Preconditions;
-import com.google.common.collect.Maps;
import com.pmease.commons.git.AheadBehind;
import com.pmease.commons.git.BriefCommit;
import com.pmease.commons.hibernate.dao.Dao;
@@ -101,21 +100,23 @@ public class RepoBranchesPage extends RepositoryPage {
@Override
protected Map<String, AheadBehind> load() {
- Map<String, AheadBehind> map = Maps.newHashMap();
- List<Branch> branches = branchesView.getModelObject();
- for (long i=branchesView.getFirstItemOffset(); i<branches.size(); i++) {
+ List<String> compareHashes = new ArrayList<>();
+ List<Branch> branchesInView = branchesView.getModelObject();
+ for (long i=branchesView.getFirstItemOffset(); i<branchesInView.size(); i++) {
if (i-branchesView.getFirstItemOffset() >= branchesView.getItemsPerPage())
break;
- Branch branch = branches.get((int)i);
- if (branch.equals(getBaseBranch())) {
- map.put(branch.getName(), new AheadBehind());
- } else {
- map.put(branch.getName(),
- getRepository().git().getAheadBehind(branch.getName(), getBaseBranch().getName()));
- }
+ Branch branch = branchesInView.get((int)i);
+ if (!branch.equals(getBaseBranch()))
+ compareHashes.add(lastCommitsModel.getObject().get(branch.getName()).getHash());
}
- return map;
+ String baseHash = lastCommitsModel.getObject().get(getBaseBranch().getName()).getHash();
+ Map<String, AheadBehind> aheadBehinds = getRepository().git().getAheadBehinds(
+ baseHash,
+ compareHashes.toArray(new String[compareHashes.size()]));
+ aheadBehinds.put(baseHash, new AheadBehind());
+
+ return aheadBehinds;
}
};
@@ -315,7 +316,8 @@ public class RepoBranchesPage extends RepositoryPage {
});
- final AheadBehind ab = Preconditions.checkNotNull(aheadBehindsModel.getObject().get(branch.getName()));
+ String branchHash = lastCommitsModel.getObject().get(branch.getName()).getHash();
+ final AheadBehind ab = Preconditions.checkNotNull(aheadBehindsModel.getObject().get(branchHash));
item.add(new Link<Void>("behindLink") {