mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-03-02 10:20:51 +01:00
Sets the default branch for a new git repository from an import to the HEAD reference of the imported repository.
Committed-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
committed by
SCM-Manager
parent
1842a49c77
commit
0361ae3c6c
@@ -27,9 +27,10 @@ package sonia.scm.repository.api;
|
||||
import lombok.Value;
|
||||
|
||||
/**
|
||||
* The {@link PullResponse} is the result of the
|
||||
* {@link PullCommandBuilder#pull(sonia.scm.repository.Repository)} method and
|
||||
* contains informations over the executed pull command.
|
||||
* The {@link PullResponse} is the result of the methods
|
||||
* {@link PullCommandBuilder#pull(sonia.scm.repository.Repository)} and
|
||||
* {@link PullCommandBuilder#pull(String)} and
|
||||
* contains information for the executed pull command.
|
||||
*
|
||||
* @author Sebastian Sdorra
|
||||
* @since 1.31
|
||||
|
||||
@@ -49,6 +49,15 @@ public class GitRepositoryConfigStoreProvider {
|
||||
return getFromStore(createStore(repositoryId));
|
||||
}
|
||||
|
||||
public void setDefaultBranch(Repository repository, String newDefaultBranch) {
|
||||
ConfigurationStore<GitRepositoryConfig> configStore = get(repository);
|
||||
GitRepositoryConfig gitRepositoryConfig = configStore
|
||||
.getOptional()
|
||||
.orElse(new GitRepositoryConfig());
|
||||
gitRepositoryConfig.setDefaultBranch(newDefaultBranch);
|
||||
configStore.set(gitRepositoryConfig);
|
||||
}
|
||||
|
||||
private static GitRepositoryConfig getFromStore(ConfigurationStore<GitRepositoryConfig> store) {
|
||||
return store.getOptional().orElse(new GitRepositoryConfig());
|
||||
}
|
||||
|
||||
@@ -51,7 +51,6 @@ import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.GitChangesetConverter;
|
||||
import sonia.scm.repository.GitChangesetConverterFactory;
|
||||
import sonia.scm.repository.GitHeadModifier;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.GitWorkingCopyFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Tag;
|
||||
@@ -61,7 +60,6 @@ import sonia.scm.repository.api.MirrorFilter;
|
||||
import sonia.scm.repository.api.MirrorFilter.Result;
|
||||
import sonia.scm.repository.api.UsernamePasswordCredential;
|
||||
import sonia.scm.repository.spi.LfsLoader.LfsLoaderLogger;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import java.io.IOException;
|
||||
@@ -241,10 +239,7 @@ public class GitMirrorCommand extends AbstractGitCommand implements MirrorComman
|
||||
}
|
||||
|
||||
gitHeadModifier.ensure(repository, newDefaultBranch);
|
||||
ConfigurationStore<GitRepositoryConfig> configStore = storeProvider.get(repository);
|
||||
GitRepositoryConfig gitRepositoryConfig = configStore.get();
|
||||
gitRepositoryConfig.setDefaultBranch(newDefaultBranch);
|
||||
configStore.set(gitRepositoryConfig);
|
||||
storeProvider.setDefaultBranch(repository, newDefaultBranch);
|
||||
}
|
||||
|
||||
private Collection<String> generatePushRefSpecs() {
|
||||
|
||||
@@ -35,12 +35,10 @@ import sonia.scm.ConcurrentModificationException;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.NoChangesMadeException;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.GitWorkingCopyFactory;
|
||||
import sonia.scm.repository.InternalRepositoryException;
|
||||
import sonia.scm.repository.Repository;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
import sonia.scm.web.lfs.LfsBlobStoreFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
@@ -129,13 +127,7 @@ public class GitModifyCommand extends AbstractGitCommand implements ModifyComman
|
||||
}
|
||||
|
||||
private void setBranchInConfig(String branch) {
|
||||
ConfigurationStore<GitRepositoryConfig> store = gitRepositoryConfigStoreProvider
|
||||
.get(repository);
|
||||
GitRepositoryConfig gitRepositoryConfig = store
|
||||
.getOptional()
|
||||
.orElse(new GitRepositoryConfig());
|
||||
gitRepositoryConfig.setDefaultBranch(branch);
|
||||
store.set(gitRepositoryConfig);
|
||||
gitRepositoryConfigStoreProvider.setDefaultBranch(repository, branch);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@@ -30,6 +30,7 @@ import com.google.common.collect.Iterables;
|
||||
import org.eclipse.jgit.api.Git;
|
||||
import org.eclipse.jgit.api.errors.GitAPIException;
|
||||
import org.eclipse.jgit.lib.ObjectId;
|
||||
import org.eclipse.jgit.lib.Ref;
|
||||
import org.eclipse.jgit.revwalk.RevCommit;
|
||||
import org.eclipse.jgit.transport.FetchResult;
|
||||
import org.eclipse.jgit.transport.TrackingRefUpdate;
|
||||
@@ -37,6 +38,7 @@ import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.ContextEntry;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitRepositoryHandler;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.Repository;
|
||||
@@ -59,17 +61,20 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
private final PostReceiveRepositoryHookEventFactory postReceiveRepositoryHookEventFactory;
|
||||
private final LfsLoader lfsLoader;
|
||||
private final PullHttpConnectionProvider pullHttpConnectionProvider;
|
||||
private final GitRepositoryConfigStoreProvider storeProvider;
|
||||
|
||||
@Inject
|
||||
public GitPullCommand(GitRepositoryHandler handler,
|
||||
GitContext context,
|
||||
PostReceiveRepositoryHookEventFactory postReceiveRepositoryHookEventFactory,
|
||||
LfsLoader lfsLoader,
|
||||
PullHttpConnectionProvider pullHttpConnectionProvider) {
|
||||
PullHttpConnectionProvider pullHttpConnectionProvider,
|
||||
GitRepositoryConfigStoreProvider storeProvider) {
|
||||
super(handler, context);
|
||||
this.postReceiveRepositoryHookEventFactory = postReceiveRepositoryHookEventFactory;
|
||||
this.lfsLoader = lfsLoader;
|
||||
this.pullHttpConnectionProvider = pullHttpConnectionProvider;
|
||||
this.storeProvider = storeProvider;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -190,6 +195,9 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
if (request.isFetchLfs()) {
|
||||
fetchLfs(request, git, lfsLoaderLogger);
|
||||
}
|
||||
|
||||
configureDefaultBranch(result);
|
||||
|
||||
response = convert(git, result, lfsLoaderLogger);
|
||||
} catch
|
||||
(GitAPIException ex) {
|
||||
@@ -205,6 +213,14 @@ public class GitPullCommand extends AbstractGitPushOrPullCommand
|
||||
return response;
|
||||
}
|
||||
|
||||
private void configureDefaultBranch(FetchResult result) {
|
||||
Ref head = result.getAdvertisedRef("HEAD").getLeaf();
|
||||
if (head.getName().startsWith("refs/heads/")) {
|
||||
String newDefaultBranch = head.getName().substring("refs/heads/".length());
|
||||
storeProvider.setDefaultBranch(repository, newDefaultBranch);
|
||||
}
|
||||
}
|
||||
|
||||
private void fetchLfs(PullCommandRequest request, Git git, LfsLoader.LfsLoaderLogger lfsLoaderLogger) throws IOException {
|
||||
open().getRefDatabase().getRefs().forEach(
|
||||
ref -> lfsLoader.inspectTree(
|
||||
|
||||
@@ -52,6 +52,7 @@ public class GitIncomingCommandTest
|
||||
|
||||
private final LfsLoader lfsLoader = mock(LfsLoader.class);
|
||||
private final PullHttpConnectionProvider pullHttpConnectionProvider = mock(PullHttpConnectionProvider.class);
|
||||
private final GitRepositoryConfigStoreProvider storeProvider = mock(GitRepositoryConfigStoreProvider.class);
|
||||
|
||||
/**
|
||||
* Method description
|
||||
@@ -105,7 +106,8 @@ public class GitIncomingCommandTest
|
||||
context,
|
||||
postReceiveRepositoryHookEventFactory,
|
||||
lfsLoader,
|
||||
pullHttpConnectionProvider);
|
||||
pullHttpConnectionProvider,
|
||||
storeProvider);
|
||||
PullCommandRequest req = new PullCommandRequest();
|
||||
req.setRemoteRepository(outgoingRepository);
|
||||
pull.pull(req);
|
||||
|
||||
@@ -47,7 +47,6 @@ import sonia.scm.net.HttpURLConnectionFactory;
|
||||
import sonia.scm.repository.GitChangesetConverterFactory;
|
||||
import sonia.scm.repository.GitConfig;
|
||||
import sonia.scm.repository.GitHeadModifier;
|
||||
import sonia.scm.repository.GitRepositoryConfig;
|
||||
import sonia.scm.repository.GitUtil;
|
||||
import sonia.scm.repository.api.MirrorCommandResult;
|
||||
import sonia.scm.repository.api.MirrorFilter;
|
||||
@@ -56,7 +55,6 @@ import sonia.scm.repository.work.NoneCachingWorkingCopyPool;
|
||||
import sonia.scm.repository.work.SimpleWorkingCopyFactory;
|
||||
import sonia.scm.repository.work.WorkdirProvider;
|
||||
import sonia.scm.security.GPG;
|
||||
import sonia.scm.store.ConfigurationStore;
|
||||
import sonia.scm.store.InMemoryConfigurationStoreFactory;
|
||||
import sonia.scm.util.IOUtil;
|
||||
|
||||
@@ -76,13 +74,11 @@ import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.junit.Assert.assertThrows;
|
||||
import static org.mockito.AdditionalMatchers.not;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.argThat;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doAnswer;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static sonia.scm.repository.api.MirrorCommandResult.ResultType.FAILED;
|
||||
import static sonia.scm.repository.api.MirrorCommandResult.ResultType.OK;
|
||||
import static sonia.scm.repository.api.MirrorCommandResult.ResultType.REJECTED_UPDATES;
|
||||
@@ -107,11 +103,8 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
|
||||
private final GitHeadModifier gitHeadModifier = mock(GitHeadModifier.class);
|
||||
|
||||
private final GitRepositoryConfigStoreProvider storeProvider = mock(GitRepositoryConfigStoreProvider.class);
|
||||
private final ConfigurationStore<GitRepositoryConfig> configurationStore = mock(ConfigurationStore.class);
|
||||
private final LfsLoader lfsLoader = mock(LfsLoader.class);
|
||||
|
||||
private final GitRepositoryConfig gitRepositoryConfig = new GitRepositoryConfig();
|
||||
|
||||
@Before
|
||||
public void bendContextToNewRepository() throws IOException, GitAPIException {
|
||||
clone = tempFolder.newFolder();
|
||||
@@ -146,12 +139,6 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
|
||||
lfsLoader);
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initializeStores() {
|
||||
when(storeProvider.get(repository)).thenReturn(configurationStore);
|
||||
when(configurationStore.get()).thenReturn(gitRepositoryConfig);
|
||||
}
|
||||
|
||||
@After
|
||||
public void cleanupWorkdir() {
|
||||
if (workdirAfterClose != null) {
|
||||
@@ -834,10 +821,7 @@ public class GitMirrorCommandTest extends AbstractGitCommandTestBase {
|
||||
assertThat(update.getBranchName()).isEqualTo("master");
|
||||
assertThat(update.getNewRevision()).isEmpty();
|
||||
});
|
||||
verify(configurationStore).set(argThat(argument -> {
|
||||
assertThat(argument.getDefaultBranch()).isNotEqualTo("master");
|
||||
return true;
|
||||
}));
|
||||
verify(storeProvider).setDefaultBranch(eq(repository), not(eq("master")));
|
||||
}
|
||||
|
||||
@Test
|
||||
|
||||
@@ -33,6 +33,7 @@ import org.junit.runner.RunWith;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.Mockito;
|
||||
import org.mockito.junit.MockitoJUnitRunner;
|
||||
import sonia.scm.api.v2.resources.GitRepositoryConfigStoreProvider;
|
||||
import sonia.scm.repository.GitConfig;
|
||||
import sonia.scm.repository.Modifications;
|
||||
|
||||
@@ -54,6 +55,8 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
|
||||
private LfsLoader lfsLoader;
|
||||
@Mock
|
||||
private PullHttpConnectionProvider pullHttpConnectionProvider;
|
||||
@Mock
|
||||
private GitRepositoryConfigStoreProvider storeProvider;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
@@ -179,7 +182,8 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
|
||||
context,
|
||||
postReceiveRepositoryHookEventFactory,
|
||||
lfsLoader,
|
||||
pullHttpConnectionProvider);
|
||||
pullHttpConnectionProvider,
|
||||
storeProvider);
|
||||
PullCommandRequest pullRequest = new PullCommandRequest();
|
||||
pullRequest.setRemoteRepository(incomingRepository);
|
||||
pullCommand.pull(pullRequest);
|
||||
|
||||
Reference in New Issue
Block a user