Add extension point to branches overview (#1888)

Prepare branches overview to show additional branch details.

Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
Eduard Heimbuch
2021-12-10 11:04:59 +01:00
committed by GitHub
parent b2d7ed88e4
commit b8d6c219ee
21 changed files with 249 additions and 78 deletions

View File

@@ -33,6 +33,7 @@ import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.revwalk.RevWalkUtils;
import org.eclipse.jgit.revwalk.filter.RevFilter;
import sonia.scm.repository.Branch;
import sonia.scm.repository.BranchDetails;
import sonia.scm.repository.InternalRepositoryException;
import sonia.scm.repository.api.BranchDetailsCommandResult;
@@ -52,14 +53,15 @@ public class GitBranchDetailsCommand extends AbstractGitCommand implements Branc
@Override
public BranchDetailsCommandResult execute(BranchDetailsCommandRequest branchDetailsCommandRequest) {
String defaultBranch = context.getConfig().getDefaultBranch();
if (branchDetailsCommandRequest.getBranchName().equals(defaultBranch)) {
return new BranchDetailsCommandResult(0, 0);
String branchName = branchDetailsCommandRequest.getBranchName();
if (branchName.equals(defaultBranch)) {
return new BranchDetailsCommandResult(new BranchDetails(branchName, 0, 0));
}
try {
Repository repository = open();
ObjectId branchCommit = getObjectId(branchDetailsCommandRequest.getBranchName(), repository);
ObjectId branchCommit = getObjectId(branchName, repository);
ObjectId defaultCommit = getObjectId(defaultBranch, repository);
return computeAheadBehind(repository, branchCommit, defaultCommit);
return computeAheadBehind(repository, branchName, branchCommit, defaultCommit);
} catch (IOException e) {
throw new InternalRepositoryException(context.getRepository(), "could not compute ahead/behind", e);
}
@@ -73,7 +75,7 @@ public class GitBranchDetailsCommand extends AbstractGitCommand implements Branc
return branchCommit;
}
private BranchDetailsCommandResult computeAheadBehind(Repository repository, ObjectId branchCommit, ObjectId defaultCommit) throws MissingObjectException, IncorrectObjectTypeException {
private BranchDetailsCommandResult computeAheadBehind(Repository repository, String branchName, ObjectId branchCommit, ObjectId defaultCommit) throws MissingObjectException, IncorrectObjectTypeException {
// this implementation is a copy of the implementation in org.eclipse.jgit.lib.BranchTrackingStatus
try (RevWalk walk = new RevWalk(repository)) {
@@ -90,7 +92,7 @@ public class GitBranchDetailsCommand extends AbstractGitCommand implements Branc
int aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase);
int behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase);
return new BranchDetailsCommandResult(aheadCount, behindCount);
return new BranchDetailsCommandResult(new BranchDetails(branchName, aheadCount, behindCount));
} catch (IOException e) {
throw new InternalRepositoryException(context.getRepository(), "could not compute ahead/behind", e);
}

View File

@@ -26,6 +26,7 @@ package sonia.scm.repository.spi;
import org.junit.Test;
import sonia.scm.NotFoundException;
import sonia.scm.repository.BranchDetails;
import sonia.scm.repository.api.BranchDetailsCommandResult;
import static org.assertj.core.api.Assertions.assertThat;
@@ -38,7 +39,7 @@ public class GitBranchDetailsCommandTest extends AbstractGitCommandTestBase {
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
request.setBranchName("master");
BranchDetailsCommandResult result = command.execute(request);
BranchDetails result = command.execute(request).getDetails();
assertThat(result.getChangesetsAhead()).get().isEqualTo(0);
assertThat(result.getChangesetsBehind()).get().isEqualTo(0);
@@ -50,7 +51,7 @@ public class GitBranchDetailsCommandTest extends AbstractGitCommandTestBase {
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
request.setBranchName("test-branch");
BranchDetailsCommandResult result = command.execute(request);
BranchDetails result = command.execute(request).getDetails();
assertThat(result.getChangesetsAhead()).get().isEqualTo(1);
assertThat(result.getChangesetsBehind()).get().isEqualTo(2);
@@ -62,7 +63,7 @@ public class GitBranchDetailsCommandTest extends AbstractGitCommandTestBase {
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
request.setBranchName("partially_merged");
BranchDetailsCommandResult result = command.execute(request);
BranchDetails result = command.execute(request).getDetails();
assertThat(result.getChangesetsAhead()).get().isEqualTo(3);
assertThat(result.getChangesetsBehind()).get().isEqualTo(1);