mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-15 09:46:16 +01:00
Set HEAD to correct default branch (#1929)
Sets the HEAD for new Git repositories to the default branch (this is 'main' for the default configuration). This change led to errors in unit tests, that depended on the 'master' branch. Co-authored-by: Eduard Heimbuch <eduard.heimbuch@cloudogu.com>
This commit is contained in:
2
gradle/changelog/init_git_head_on_creation.yaml
Normal file
2
gradle/changelog/init_git_head_on_creation.yaml
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
- type: fixed
|
||||||
|
description: Set HEAD to correct branch in new git repositories ([#1929](https://github.com/scm-manager/scm-manager/pull/1929))
|
||||||
@@ -49,7 +49,7 @@ class MergeDetectionITCase {
|
|||||||
private static final Person ARTHUR = new Person("arthur", "arthur@hitchhiker.com");
|
private static final Person ARTHUR = new Person("arthur", "arthur@hitchhiker.com");
|
||||||
|
|
||||||
RepositoryClient client;
|
RepositoryClient client;
|
||||||
String masterFile;
|
String mainFile;
|
||||||
String developFile;
|
String developFile;
|
||||||
|
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
@@ -58,10 +58,10 @@ class MergeDetectionITCase {
|
|||||||
|
|
||||||
client = RepositoryUtil.createRepositoryClient("git", tempDir.toFile());
|
client = RepositoryUtil.createRepositoryClient("git", tempDir.toFile());
|
||||||
|
|
||||||
masterFile = createFile(tempDir, "hg2g.md");
|
mainFile = createFile(tempDir, "hg2g.md");
|
||||||
developFile = createFile(tempDir, "how_to_make_tea.md");
|
developFile = createFile(tempDir, "how_to_make_tea.md");
|
||||||
|
|
||||||
client.getAddCommand().add(masterFile);
|
client.getAddCommand().add(mainFile);
|
||||||
client.getCommitCommand().commit(ARTHUR, "Add base file");
|
client.getCommitCommand().commit(ARTHUR, "Add base file");
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
|
|
||||||
@@ -87,10 +87,10 @@ class MergeDetectionITCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDetectSimpleMergeAsMerged() throws IOException {
|
void shouldDetectSimpleMergeAsMerged() throws IOException {
|
||||||
client.getCheckoutCommand().checkout("master");
|
client.getCheckoutCommand().checkout("main");
|
||||||
client.getMergeCommand().noFf().merge("develop");
|
client.getMergeCommand().noFf().merge("develop");
|
||||||
|
|
||||||
initializeMergeDetection("master", "develop");
|
initializeMergeDetection("main", "develop");
|
||||||
|
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
|
|
||||||
@@ -100,10 +100,10 @@ class MergeDetectionITCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDetectFastForwardAsMerged() throws IOException {
|
void shouldDetectFastForwardAsMerged() throws IOException {
|
||||||
client.getCheckoutCommand().checkout("master");
|
client.getCheckoutCommand().checkout("main");
|
||||||
client.getMergeCommand().merge("develop");
|
client.getMergeCommand().merge("develop");
|
||||||
|
|
||||||
initializeMergeDetection("master", "develop");
|
initializeMergeDetection("main", "develop");
|
||||||
|
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
|
|
||||||
@@ -113,11 +113,11 @@ class MergeDetectionITCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
void shouldDetectMergeWhenBranchHasBeenDeletedAsMerged() throws IOException {
|
void shouldDetectMergeWhenBranchHasBeenDeletedAsMerged() throws IOException {
|
||||||
client.getCheckoutCommand().checkout("master");
|
client.getCheckoutCommand().checkout("main");
|
||||||
client.getMergeCommand().merge("develop");
|
client.getMergeCommand().merge("develop");
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
|
|
||||||
initializeMergeDetection("master", "develop");
|
initializeMergeDetection("main", "develop");
|
||||||
|
|
||||||
client.getDeleteRemoteBranchCommand().delete("develop");
|
client.getDeleteRemoteBranchCommand().delete("develop");
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
@@ -133,7 +133,7 @@ class MergeDetectionITCase {
|
|||||||
client.getAddCommand().add(developFile);
|
client.getAddCommand().add(developFile);
|
||||||
client.getCommitCommand().commit(ARTHUR, "simple commit");
|
client.getCommitCommand().commit(ARTHUR, "simple commit");
|
||||||
|
|
||||||
initializeMergeDetection("master", "develop");
|
initializeMergeDetection("main", "develop");
|
||||||
|
|
||||||
client.getPushCommand().push();
|
client.getPushCommand().push();
|
||||||
|
|
||||||
|
|||||||
@@ -26,12 +26,16 @@ package sonia.scm.it.utils;
|
|||||||
|
|
||||||
import io.restassured.response.ValidatableResponse;
|
import io.restassured.response.ValidatableResponse;
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
|
import org.eclipse.jgit.errors.ConfigInvalidException;
|
||||||
|
import org.eclipse.jgit.lib.StoredConfig;
|
||||||
|
import org.eclipse.jgit.util.SystemReader;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.json.Json;
|
import javax.json.Json;
|
||||||
import javax.json.JsonObjectBuilder;
|
import javax.json.JsonObjectBuilder;
|
||||||
|
import java.io.IOException;
|
||||||
import java.net.URI;
|
import java.net.URI;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
@@ -245,6 +249,14 @@ public class TestData {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void cleanupConfig() {
|
private static void cleanupConfig() {
|
||||||
|
try {
|
||||||
|
StoredConfig config = SystemReader.getInstance().getUserConfig();
|
||||||
|
config.setString("init", null, "defaultBranch", "main");
|
||||||
|
config.save();
|
||||||
|
} catch (ConfigInvalidException | IOException e) {
|
||||||
|
LOG.error("could not set default branch for git to 'main'", e);
|
||||||
|
}
|
||||||
|
|
||||||
given(VndMediaType.CONFIG).accept("application/json")
|
given(VndMediaType.CONFIG).accept("application/json")
|
||||||
.when()
|
.when()
|
||||||
.body("{\n" +
|
.body("{\n" +
|
||||||
|
|||||||
@@ -183,6 +183,7 @@ public class GitRepositoryHandler
|
|||||||
protected void create(Repository repository, File directory) throws IOException {
|
protected void create(Repository repository, File directory) throws IOException {
|
||||||
try (org.eclipse.jgit.lib.Repository gitRepository = build(directory)) {
|
try (org.eclipse.jgit.lib.Repository gitRepository = build(directory)) {
|
||||||
gitRepository.create(true);
|
gitRepository.create(true);
|
||||||
|
new GitHeadModifier(this).ensure(repository, config.getDefaultBranch());
|
||||||
new GitConfigHelper().createScmmConfig(repository, gitRepository);
|
new GitConfigHelper().createScmmConfig(repository, gitRepository);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -84,7 +84,7 @@ public final class GitUtil {
|
|||||||
private static final GitUserAgentProvider GIT_USER_AGENT_PROVIDER = new GitUserAgentProvider();
|
private static final GitUserAgentProvider GIT_USER_AGENT_PROVIDER = new GitUserAgentProvider();
|
||||||
public static final String REF_HEAD = "HEAD";
|
public static final String REF_HEAD = "HEAD";
|
||||||
public static final String REF_HEAD_PREFIX = "refs/heads/";
|
public static final String REF_HEAD_PREFIX = "refs/heads/";
|
||||||
public static final String REF_MASTER = "master";
|
public static final String REF_MAIN = "main";
|
||||||
private static final String DIRECTORY_DOTGIT = ".git";
|
private static final String DIRECTORY_DOTGIT = ".git";
|
||||||
private static final String DIRECTORY_OBJETCS = "objects";
|
private static final String DIRECTORY_OBJETCS = "objects";
|
||||||
private static final String DIRECTORY_REFS = "refs";
|
private static final String DIRECTORY_REFS = "refs";
|
||||||
@@ -425,7 +425,7 @@ public final class GitUtil {
|
|||||||
return of(refHead.getTarget());
|
return of(refHead.getTarget());
|
||||||
}
|
}
|
||||||
|
|
||||||
Ref master = refs.get(REF_HEAD_PREFIX + REF_MASTER);
|
Ref master = refs.get(REF_HEAD_PREFIX + REF_MAIN);
|
||||||
if (master != null) {
|
if (master != null) {
|
||||||
return of(master);
|
return of(master);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -24,8 +24,6 @@
|
|||||||
|
|
||||||
package sonia.scm.repository;
|
package sonia.scm.repository;
|
||||||
|
|
||||||
//~--- non-JDK imports --------------------------------------------------------
|
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.junit.runner.RunWith;
|
import org.junit.runner.RunWith;
|
||||||
@@ -36,13 +34,12 @@ import sonia.scm.store.ConfigurationStoreFactory;
|
|||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertTrue;
|
import static org.junit.Assert.assertTrue;
|
||||||
import static org.mockito.ArgumentMatchers.any;
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
import static org.mockito.Mockito.when;
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
//~--- JDK imports ------------------------------------------------------------
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @author Sebastian Sdorra
|
* @author Sebastian Sdorra
|
||||||
*/
|
*/
|
||||||
@@ -112,4 +109,21 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
|
|||||||
File path = repositoryHandler.getDirectory(repository.getId());
|
File path = repositoryHandler.getDirectory(repository.getId());
|
||||||
assertEquals(repoPath.toString() + File.separator + RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
|
assertEquals(repoPath.toString() + File.separator + RepositoryDirectoryHandler.REPOSITORIES_NATIVE_DIRECTORY, path.getAbsolutePath());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void shouldSetHeadToDefaultRepository() {
|
||||||
|
GitRepositoryHandler repositoryHandler = new GitRepositoryHandler(factory,
|
||||||
|
scheduler, locationResolver, gitWorkingCopyFactory, null);
|
||||||
|
|
||||||
|
GitConfig config = new GitConfig();
|
||||||
|
config.setDefaultBranch("other");
|
||||||
|
|
||||||
|
repositoryHandler.setConfig(config);
|
||||||
|
|
||||||
|
File nativeRepoDirectory = initRepository();
|
||||||
|
|
||||||
|
repositoryHandler.create(repository);
|
||||||
|
|
||||||
|
assertThat(new File(nativeRepoDirectory, "HEAD")).hasContent("ref: refs/heads/other");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -295,7 +295,7 @@ public class GitLogCommandTest extends AbstractGitCommandTestBase
|
|||||||
|
|
||||||
ChangesetPagingResult changesets = createCommand().getChangesets(new LogCommandRequest());
|
ChangesetPagingResult changesets = createCommand().getChangesets(new LogCommandRequest());
|
||||||
|
|
||||||
assertEquals("master", changesets.getBranchName());
|
assertEquals("main", changesets.getBranchName());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
|||||||
Binary file not shown.
Reference in New Issue
Block a user