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:
René Pfeuffer
2022-01-20 10:19:27 +01:00
committed by GitHub
parent 0f01bb82c6
commit 6e555a855a
8 changed files with 46 additions and 17 deletions

View 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))

View File

@@ -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();

View File

@@ -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" +

View File

@@ -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);
} }
} }

View File

@@ -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);
} }

View File

@@ -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");
}
} }

View File

@@ -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