mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-01-23 07:52:13 +01:00
merge
This commit is contained in:
@@ -66,7 +66,7 @@ public final class Branch implements Serializable
|
||||
* This constructor should only be called from JAXB.
|
||||
*
|
||||
*/
|
||||
public Branch() {}
|
||||
Branch() {}
|
||||
|
||||
/**
|
||||
* Constructs a new branch.
|
||||
@@ -75,10 +75,19 @@ public final class Branch implements Serializable
|
||||
* @param name name of the branch
|
||||
* @param revision latest revision of the branch
|
||||
*/
|
||||
public Branch(String name, String revision)
|
||||
Branch(String name, String revision, boolean defaultBranch)
|
||||
{
|
||||
this.name = name;
|
||||
this.revision = revision;
|
||||
this.defaultBranch = defaultBranch;
|
||||
}
|
||||
|
||||
public static Branch normalBranch(String name, String revision) {
|
||||
return new Branch(name, revision, false);
|
||||
}
|
||||
|
||||
public static Branch defaultBranch(String name, String revision) {
|
||||
return new Branch(name, revision, true);
|
||||
}
|
||||
|
||||
//~--- methods --------------------------------------------------------------
|
||||
@@ -107,7 +116,8 @@ public final class Branch implements Serializable
|
||||
final Branch other = (Branch) obj;
|
||||
|
||||
return Objects.equal(name, other.name)
|
||||
&& Objects.equal(revision, other.revision);
|
||||
&& Objects.equal(revision, other.revision)
|
||||
&& Objects.equal(defaultBranch, other.defaultBranch);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -162,6 +172,10 @@ public final class Branch implements Serializable
|
||||
return revision;
|
||||
}
|
||||
|
||||
public boolean isDefaultBranch() {
|
||||
return defaultBranch;
|
||||
}
|
||||
|
||||
//~--- fields ---------------------------------------------------------------
|
||||
|
||||
/** name of the branch */
|
||||
@@ -169,4 +183,6 @@ public final class Branch implements Serializable
|
||||
|
||||
/** Field description */
|
||||
private String revision;
|
||||
|
||||
private boolean defaultBranch;
|
||||
}
|
||||
|
||||
@@ -34,11 +34,14 @@ package sonia.scm.repository.spi;
|
||||
|
||||
//~--- non-JDK imports --------------------------------------------------------
|
||||
|
||||
import com.google.common.base.Function;
|
||||
import com.google.common.collect.Lists;
|
||||
import com.google.common.annotations.VisibleForTesting;
|
||||
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.Ref;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.repository.Branch;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
@@ -46,6 +49,8 @@ import sonia.scm.repository.Repository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
//~--- JDK imports ------------------------------------------------------------
|
||||
|
||||
@@ -53,17 +58,10 @@ import java.util.List;
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
*/
|
||||
public class GitBranchesCommand extends AbstractGitCommand
|
||||
implements BranchesCommand
|
||||
{
|
||||
public class GitBranchesCommand extends AbstractGitCommand implements BranchesCommand {
|
||||
|
||||
private static final Logger LOG = LoggerFactory.getLogger(GitBranchesCommand.class);
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
*
|
||||
* @param context
|
||||
* @param repository
|
||||
*/
|
||||
public GitBranchesCommand(GitContext context, Repository repository)
|
||||
{
|
||||
super(context, repository);
|
||||
@@ -73,38 +71,54 @@ public class GitBranchesCommand extends AbstractGitCommand
|
||||
|
||||
@Override
|
||||
public List<Branch> getBranches() throws IOException {
|
||||
List<Branch> branches = null;
|
||||
Git git = createGit();
|
||||
|
||||
Git git = new Git(open());
|
||||
String defaultBranchName = determineDefaultBranchName(git);
|
||||
|
||||
try
|
||||
{
|
||||
List<Ref> refs = git.branchList().call();
|
||||
|
||||
branches = Lists.transform(refs, new Function<Ref, Branch>()
|
||||
{
|
||||
|
||||
@Override
|
||||
public Branch apply(Ref ref)
|
||||
{
|
||||
Branch branch = null;
|
||||
String branchName = GitUtil.getBranch(ref);
|
||||
|
||||
if (branchName != null)
|
||||
{
|
||||
branch = new Branch(branchName, GitUtil.getId(ref.getObjectId()));
|
||||
}
|
||||
|
||||
return branch;
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
try {
|
||||
return git
|
||||
.branchList()
|
||||
.call()
|
||||
.stream()
|
||||
.map(ref -> createBranchObject(defaultBranchName, ref))
|
||||
.collect(Collectors.toList());
|
||||
} catch (GitAPIException ex) {
|
||||
throw new InternalRepositoryException(repository, "could not read branches", ex);
|
||||
}
|
||||
}
|
||||
|
||||
return branches;
|
||||
@VisibleForTesting
|
||||
Git createGit() throws IOException {
|
||||
return new Git(open());
|
||||
}
|
||||
|
||||
@Nullable
|
||||
private Branch createBranchObject(String defaultBranchName, Ref ref) {
|
||||
String branchName = GitUtil.getBranch(ref);
|
||||
|
||||
if (branchName == null) {
|
||||
LOG.warn("could not determine branch name for branch name {} at revision {}", ref.getName(), ref.getObjectId());
|
||||
return null;
|
||||
} else {
|
||||
if (branchName.equals(defaultBranchName)) {
|
||||
return Branch.defaultBranch(branchName, GitUtil.getId(ref.getObjectId()));
|
||||
} else {
|
||||
return Branch.normalBranch(branchName, GitUtil.getId(ref.getObjectId()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String determineDefaultBranchName(Git git) {
|
||||
String defaultBranchName = context.getConfig().getDefaultBranch();
|
||||
if (Strings.isNullOrEmpty(defaultBranchName)) {
|
||||
return getRepositoryHeadRef(git).map(GitUtil::getBranch).orElse(null);
|
||||
} else {
|
||||
return defaultBranchName;
|
||||
}
|
||||
}
|
||||
|
||||
Optional<Ref> getRepositoryHeadRef(Git git) {
|
||||
return GitUtil.getRepositoryHeadRef(git.getRepository());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -72,7 +72,7 @@ public class GitBranchCommand implements BranchCommand
|
||||
try
|
||||
{
|
||||
Ref ref = git.branchCreate().setName(name).call();
|
||||
return new Branch(name, GitUtil.getId(ref.getObjectId()));
|
||||
return Branch.normalBranch(name, GitUtil.getId(ref.getObjectId()));
|
||||
}
|
||||
catch (GitAPIException ex)
|
||||
{
|
||||
|
||||
@@ -0,0 +1,117 @@
|
||||
package sonia.scm.repository.spi;
|
||||
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.ListBranchCommand;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import sonia.scm.repository.Branch;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.Repository;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static java.util.Collections.emptyList;
|
||||
import static java.util.Optional.of;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.lenient;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
class GitBranchesCommandTest {
|
||||
|
||||
@Mock
|
||||
GitContext context;
|
||||
@Mock
|
||||
Git git;
|
||||
@Mock
|
||||
ListBranchCommand listBranchCommand;
|
||||
@Mock
|
||||
GitRepositoryConfig gitRepositoryConfig;
|
||||
|
||||
GitBranchesCommand branchesCommand;
|
||||
private Ref master;
|
||||
|
||||
@BeforeEach
|
||||
void initContext() {
|
||||
when(context.getConfig()).thenReturn(gitRepositoryConfig);
|
||||
}
|
||||
|
||||
@BeforeEach
|
||||
void initCommand() {
|
||||
master = createRef("master", "0000");
|
||||
branchesCommand = new GitBranchesCommand(context, new Repository("1", "git", "space", "X")) {
|
||||
@Override
|
||||
Git createGit() {
|
||||
return git;
|
||||
}
|
||||
|
||||
@Override
|
||||
Optional<Ref> getRepositoryHeadRef(Git git) {
|
||||
return of(master);
|
||||
}
|
||||
};
|
||||
when(git.branchList()).thenReturn(listBranchCommand);
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldCreateEmptyListWithoutBranches() throws IOException, GitAPIException {
|
||||
when(listBranchCommand.call()).thenReturn(emptyList());
|
||||
|
||||
List<Branch> branches = branchesCommand.getBranches();
|
||||
|
||||
assertThat(branches).isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMapNormalBranch() throws IOException, GitAPIException {
|
||||
Ref branch = createRef("branch", "1337");
|
||||
when(listBranchCommand.call()).thenReturn(asList(branch));
|
||||
|
||||
List<Branch> branches = branchesCommand.getBranches();
|
||||
|
||||
assertThat(branches).containsExactly(Branch.normalBranch("branch", "1337"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMarkMasterBranchWithMasterFromConfig() throws IOException, GitAPIException {
|
||||
Ref branch = createRef("branch", "1337");
|
||||
when(listBranchCommand.call()).thenReturn(asList(branch));
|
||||
when(gitRepositoryConfig.getDefaultBranch()).thenReturn("branch");
|
||||
|
||||
List<Branch> branches = branchesCommand.getBranches();
|
||||
|
||||
assertThat(branches).containsExactlyInAnyOrder(Branch.defaultBranch("branch", "1337"));
|
||||
}
|
||||
|
||||
@Test
|
||||
void shouldMarkMasterBranchWithMasterFromHead() throws IOException, GitAPIException {
|
||||
Ref branch = createRef("branch", "1337");
|
||||
when(listBranchCommand.call()).thenReturn(asList(branch, master));
|
||||
|
||||
List<Branch> branches = branchesCommand.getBranches();
|
||||
|
||||
assertThat(branches).containsExactlyInAnyOrder(
|
||||
Branch.normalBranch("branch", "1337"),
|
||||
Branch.defaultBranch("master", "0000")
|
||||
);
|
||||
}
|
||||
|
||||
private Ref createRef(String branchName, String revision) {
|
||||
Ref ref = mock(Ref.class);
|
||||
lenient().when(ref.getName()).thenReturn("refs/heads/" + branchName);
|
||||
ObjectId objectId = mock(ObjectId.class);
|
||||
lenient().when(objectId.name()).thenReturn(revision);
|
||||
lenient().when(ref.getObjectId()).thenReturn(objectId);
|
||||
return ref;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
mock-maker-inline
|
||||
@@ -53,6 +53,8 @@ public class HgBranchesCommand extends AbstractCommand
|
||||
implements BranchesCommand
|
||||
{
|
||||
|
||||
private static final String DEFAULT_BRANCH_NAME = "default";
|
||||
|
||||
/**
|
||||
* Constructs ...
|
||||
*
|
||||
@@ -88,7 +90,11 @@ public class HgBranchesCommand extends AbstractCommand
|
||||
node = changeset.getNode();
|
||||
}
|
||||
|
||||
return new Branch(hgBranch.getName(), node);
|
||||
if (DEFAULT_BRANCH_NAME.equals(hgBranch.getName())) {
|
||||
return Branch.defaultBranch(hgBranch.getName(), node);
|
||||
} else {
|
||||
return Branch.normalBranch(hgBranch.getName(), node);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
@@ -53,7 +53,7 @@ public class HgBranchCommand implements BranchCommand
|
||||
public Branch branch(String name) throws IOException
|
||||
{
|
||||
com.aragost.javahg.commands.BranchCommand.on(repository).set(name);
|
||||
return new Branch(name, repository.tip().getNode());
|
||||
return Branch.normalBranch(name, repository.tip().getNode());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ public class BranchDto extends HalRepresentation {
|
||||
|
||||
private String name;
|
||||
private String revision;
|
||||
private boolean defaultBranch;
|
||||
|
||||
BranchDto(Links links, Embedded embedded) {
|
||||
super(links, embedded);
|
||||
|
||||
@@ -60,7 +60,7 @@ public abstract class DefaultChangesetToChangesetDtoMapper extends HalAppenderMa
|
||||
}
|
||||
if (repositoryService.isSupported(Command.BRANCHES)) {
|
||||
embeddedBuilder.with("branches", branchCollectionToDtoMapper.getBranchDtoList(namespace, name,
|
||||
getListOfObjects(source.getBranches(), branchName -> new Branch(branchName, source.getId()))));
|
||||
getListOfObjects(source.getBranches(), branchName -> Branch.normalBranch(branchName, source.getId()))));
|
||||
}
|
||||
}
|
||||
embeddedBuilder.with("parents", getListOfObjects(source.getParents(), parent -> changesetToParentDtoMapper.map(new Changeset(parent, 0L, null), repository)));
|
||||
|
||||
@@ -125,7 +125,7 @@ public class BranchRootResourceTest extends RepositoryTestBase {
|
||||
|
||||
@Test
|
||||
public void shouldFindExistingBranch() throws Exception {
|
||||
when(branchesCommandBuilder.getBranches()).thenReturn(new Branches(new Branch("master", "revision")));
|
||||
when(branchesCommandBuilder.getBranches()).thenReturn(new Branches(Branch.normalBranch("master", "revision")));
|
||||
|
||||
MockHttpRequest request = MockHttpRequest.get(BRANCH_URL);
|
||||
MockHttpResponse response = new MockHttpResponse();
|
||||
@@ -153,7 +153,7 @@ public class BranchRootResourceTest extends RepositoryTestBase {
|
||||
when(logCommandBuilder.setBranch(anyString())).thenReturn(logCommandBuilder);
|
||||
when(logCommandBuilder.getChangesets()).thenReturn(changesetPagingResult);
|
||||
Branches branches = mock(Branches.class);
|
||||
List<Branch> branchList = Lists.newArrayList(new Branch("master",id));
|
||||
List<Branch> branchList = Lists.newArrayList(Branch.normalBranch("master",id));
|
||||
when(branches.getBranches()).thenReturn(branchList);
|
||||
when(branchesCommandBuilder.getBranches()).thenReturn(branches);
|
||||
MockHttpRequest request = MockHttpRequest.get(BRANCH_URL + "/changesets/");
|
||||
|
||||
@@ -33,7 +33,7 @@ class BranchToBranchDtoMapperTest {
|
||||
});
|
||||
mapper.setRegistry(registry);
|
||||
|
||||
Branch branch = new Branch("master", "42");
|
||||
Branch branch = Branch.normalBranch("master", "42");
|
||||
|
||||
BranchDto dto = mapper.map(branch, new NamespaceAndName("hitchhiker", "heart-of-gold"));
|
||||
assertThat(dto.getLinks().getLinkBy("ka").get().getHref()).isEqualTo("http://hitchhiker/heart-of-gold/master");
|
||||
|
||||
Reference in New Issue
Block a user