mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-03 12:05:52 +01:00
Feature/branch details (#1876)
Enrich branch overview with more details like last committer and ahead/behind commits. Since calculating this information is pretty intense, we request it in chunks to prevent very long loading times. Also we cache the results in frontend and backend. Co-authored-by: René Pfeuffer <rene.pfeuffer@cloudogu.com>
This commit is contained in:
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.eclipse.jgit.errors.IncorrectObjectTypeException;
|
||||
import org.eclipse.jgit.errors.MissingObjectException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
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.InternalRepositoryException;
|
||||
import sonia.scm.repository.api.BranchDetailsCommandResult;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
|
||||
import static sonia.scm.ContextEntry.ContextBuilder.entity;
|
||||
import static sonia.scm.NotFoundException.notFound;
|
||||
|
||||
public class GitBranchDetailsCommand extends AbstractGitCommand implements BranchDetailsCommand {
|
||||
|
||||
@Inject
|
||||
GitBranchDetailsCommand(GitContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BranchDetailsCommandResult execute(BranchDetailsCommandRequest branchDetailsCommandRequest) {
|
||||
String defaultBranch = context.getConfig().getDefaultBranch();
|
||||
if (branchDetailsCommandRequest.getBranchName().equals(defaultBranch)) {
|
||||
return new BranchDetailsCommandResult(0, 0);
|
||||
}
|
||||
try {
|
||||
Repository repository = open();
|
||||
ObjectId branchCommit = getObjectId(branchDetailsCommandRequest.getBranchName(), repository);
|
||||
ObjectId defaultCommit = getObjectId(defaultBranch, repository);
|
||||
return computeAheadBehind(repository, branchCommit, defaultCommit);
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not compute ahead/behind", e);
|
||||
}
|
||||
}
|
||||
|
||||
private ObjectId getObjectId(String branch, Repository repository) throws IOException {
|
||||
ObjectId branchCommit = getCommitOrDefault(repository, branch);
|
||||
if (branchCommit == null) {
|
||||
throw notFound(entity(Branch.class, branch).in(context.getRepository()));
|
||||
}
|
||||
return branchCommit;
|
||||
}
|
||||
|
||||
private BranchDetailsCommandResult computeAheadBehind(Repository repository, 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)) {
|
||||
|
||||
RevCommit localCommit = walk.parseCommit(branchCommit);
|
||||
RevCommit trackingCommit = walk.parseCommit(defaultCommit);
|
||||
|
||||
walk.setRevFilter(RevFilter.MERGE_BASE);
|
||||
walk.markStart(localCommit);
|
||||
walk.markStart(trackingCommit);
|
||||
RevCommit mergeBase = walk.next();
|
||||
|
||||
walk.reset();
|
||||
walk.setRevFilter(RevFilter.ALL);
|
||||
int aheadCount = RevWalkUtils.count(walk, localCommit, mergeBase);
|
||||
int behindCount = RevWalkUtils.count(walk, trackingCommit, mergeBase);
|
||||
|
||||
return new BranchDetailsCommandResult(aheadCount, behindCount);
|
||||
} catch (IOException e) {
|
||||
throw new InternalRepositoryException(context.getRepository(), "could not compute ahead/behind", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -29,14 +29,17 @@ import com.google.common.base.Strings;
|
||||
import org.checkerframework.checker.nullness.qual.Nullable;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.PersonIdent;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.lib.Repository;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.revwalk.RevWalk;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.Branch;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Person;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
@@ -52,8 +55,7 @@ public class GitBranchesCommand extends AbstractGitCommand implements BranchesCo
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitBranchesCommand.class);
|
||||
|
||||
@Inject
|
||||
public GitBranchesCommand(GitContext context)
|
||||
{
|
||||
public GitBranchesCommand(GitContext context) {
|
||||
super(context);
|
||||
}
|
||||
|
||||
@@ -89,24 +91,23 @@ public class GitBranchesCommand extends AbstractGitCommand implements BranchesCo
|
||||
LOG.warn("could not determine branch name for branch name {} at revision {}", ref.getName(), ref.getObjectId());
|
||||
return null;
|
||||
} else {
|
||||
Long lastCommitDate = getCommitDate(repository, refWalk, branchName, ref);
|
||||
if (branchName.equals(defaultBranchName)) {
|
||||
return Branch.defaultBranch(branchName, GitUtil.getId(ref.getObjectId()), lastCommitDate);
|
||||
} else {
|
||||
return Branch.normalBranch(branchName, GitUtil.getId(ref.getObjectId()), lastCommitDate);
|
||||
try {
|
||||
RevCommit commit = getCommit(repository, refWalk, ref);
|
||||
Long lastCommitDate = getCommitTime(commit);
|
||||
PersonIdent authorIdent = commit.getAuthorIdent();
|
||||
Person lastCommitter = new Person(authorIdent.getName(), authorIdent.getEmailAddress());
|
||||
if (branchName.equals(defaultBranchName)) {
|
||||
return Branch.defaultBranch(branchName, GitUtil.getId(ref.getObjectId()), lastCommitDate, lastCommitter);
|
||||
} else {
|
||||
return Branch.normalBranch(branchName, GitUtil.getId(ref.getObjectId()), lastCommitDate, lastCommitter);
|
||||
}
|
||||
} catch (IOException e) {
|
||||
LOG.info("failed to read commit date/author of branch {} with revision {}", branchName, ref.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Long getCommitDate(Repository repository, RevWalk refWalk, String branchName, Ref ref) {
|
||||
try {
|
||||
return getCommitTime(getCommit(repository, refWalk, ref));
|
||||
} catch (IOException e) {
|
||||
LOG.info("failed to read commit date of branch {} with revision {}", branchName, ref.getName());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
private String determineDefaultBranchName(Git git) {
|
||||
String defaultBranchName = context.getConfig().getDefaultBranch();
|
||||
if (Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
|
||||
@@ -58,7 +58,8 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider {
|
||||
Command.BUNDLE,
|
||||
Command.UNBUNDLE,
|
||||
Command.MIRROR,
|
||||
Command.FILE_LOCK
|
||||
Command.FILE_LOCK,
|
||||
Command.BRANCH_DETAILS
|
||||
);
|
||||
|
||||
protected static final Set<Feature> FEATURES = EnumSet.of(
|
||||
@@ -186,6 +187,11 @@ public class GitRepositoryServiceProvider extends RepositoryServiceProvider {
|
||||
return commandInjector.getInstance(GitFileLockCommand.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public BranchDetailsCommand getBranchDetailsCommand() {
|
||||
return commandInjector.getInstance(GitBranchDetailsCommand.class);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Set<Command> getSupportedCommands() {
|
||||
return COMMANDS;
|
||||
|
||||
@@ -0,0 +1,79 @@
|
||||
/*
|
||||
* MIT License
|
||||
*
|
||||
* Copyright (c) 2020-present Cloudogu GmbH and Contributors
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to deal
|
||||
* in the Software without restriction, including without limitation the rights
|
||||
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
* copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in all
|
||||
* copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
* SOFTWARE.
|
||||
*/
|
||||
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.junit.Test;
|
||||
import sonia.scm.NotFoundException;
|
||||
import sonia.scm.repository.api.BranchDetailsCommandResult;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
|
||||
public class GitBranchDetailsCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldGetZerosForDefaultBranch() {
|
||||
GitBranchDetailsCommand command = new GitBranchDetailsCommand(createContext());
|
||||
|
||||
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
|
||||
request.setBranchName("master");
|
||||
BranchDetailsCommandResult result = command.execute(request);
|
||||
|
||||
assertThat(result.getChangesetsAhead()).get().isEqualTo(0);
|
||||
assertThat(result.getChangesetsBehind()).get().isEqualTo(0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCountSimpleAheadAndBehind() {
|
||||
GitBranchDetailsCommand command = new GitBranchDetailsCommand(createContext());
|
||||
|
||||
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
|
||||
request.setBranchName("test-branch");
|
||||
BranchDetailsCommandResult result = command.execute(request);
|
||||
|
||||
assertThat(result.getChangesetsAhead()).get().isEqualTo(1);
|
||||
assertThat(result.getChangesetsBehind()).get().isEqualTo(2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCountMoreComplexAheadAndBehind() {
|
||||
GitBranchDetailsCommand command = new GitBranchDetailsCommand(createContext());
|
||||
|
||||
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
|
||||
request.setBranchName("partially_merged");
|
||||
BranchDetailsCommandResult result = command.execute(request);
|
||||
|
||||
assertThat(result.getChangesetsAhead()).get().isEqualTo(3);
|
||||
assertThat(result.getChangesetsBehind()).get().isEqualTo(1);
|
||||
}
|
||||
|
||||
@Test(expected = NotFoundException.class)
|
||||
public void shouldThrowNotFoundExceptionForUnknownBranch() {
|
||||
GitBranchDetailsCommand command = new GitBranchDetailsCommand(createContext());
|
||||
|
||||
BranchDetailsCommandRequest request = new BranchDetailsCommandRequest();
|
||||
request.setBranchName("no-such-branch");
|
||||
command.execute(request);
|
||||
}
|
||||
}
|
||||
@@ -26,11 +26,14 @@ package sonia.scm.repository.spi;
|
||||
|
||||
import org.junit.Test;
|
||||
import sonia.scm.repository.Branch;
|
||||
import sonia.scm.repository.Person;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static sonia.scm.repository.Branch.defaultBranch;
|
||||
import static sonia.scm.repository.Branch.normalBranch;
|
||||
|
||||
public class GitBranchesCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
@@ -40,10 +43,35 @@ public class GitBranchesCommandTest extends AbstractGitCommandTestBase {
|
||||
|
||||
List<Branch> branches = branchesCommand.getBranches();
|
||||
|
||||
assertThat(branches).contains(
|
||||
Branch.defaultBranch("master", "fcd0ef1831e4002ac43ea539f4094334c79ea9ec", 1339428655000L),
|
||||
Branch.normalBranch("mergeable", "91b99de908fcd04772798a31c308a64aea1a5523", 1541586052000L),
|
||||
Branch.normalBranch("rename", "383b954b27e052db6880d57f1c860dc208795247", 1589203061000L)
|
||||
assertThat(findBranch(branches, "master")).isEqualTo(
|
||||
defaultBranch(
|
||||
"master",
|
||||
"fcd0ef1831e4002ac43ea539f4094334c79ea9ec",
|
||||
1339428655000L,
|
||||
new Person("Zaphod Beeblebrox", "zaphod.beeblebrox@hitchhiker.com")
|
||||
)
|
||||
);
|
||||
assertThat(findBranch(branches, "mergeable")).isEqualTo(
|
||||
normalBranch(
|
||||
"mergeable",
|
||||
"91b99de908fcd04772798a31c308a64aea1a5523",
|
||||
1541586052000L,
|
||||
new Person("Douglas Adams",
|
||||
"douglas.adams@hitchhiker.com")
|
||||
)
|
||||
);
|
||||
assertThat(findBranch(branches, "rename")).isEqualTo(
|
||||
normalBranch(
|
||||
"rename",
|
||||
"383b954b27e052db6880d57f1c860dc208795247",
|
||||
1589203061000L,
|
||||
new Person("scmadmin",
|
||||
"scm@admin.com")
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
private Branch findBranch(List<Branch> branches, String mergeable) {
|
||||
return branches.stream().filter(b -> b.getName().equals(mergeable)).findFirst().get();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user