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

@@ -183,6 +183,7 @@ public class GitRepositoryHandler
protected void create(Repository repository, File directory) throws IOException {
try (org.eclipse.jgit.lib.Repository gitRepository = build(directory)) {
gitRepository.create(true);
new GitHeadModifier(this).ensure(repository, config.getDefaultBranch());
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();
public static final String REF_HEAD = "HEAD";
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_OBJETCS = "objects";
private static final String DIRECTORY_REFS = "refs";
@@ -425,7 +425,7 @@ public final class GitUtil {
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) {
return of(master);
}

View File

@@ -24,8 +24,6 @@
package sonia.scm.repository;
//~--- non-JDK imports --------------------------------------------------------
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@@ -36,13 +34,12 @@ import sonia.scm.store.ConfigurationStoreFactory;
import java.io.File;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
//~--- JDK imports ------------------------------------------------------------
/**
* @author Sebastian Sdorra
*/
@@ -112,4 +109,21 @@ public class GitRepositoryHandlerTest extends SimpleRepositoryHandlerTestBase {
File path = repositoryHandler.getDirectory(repository.getId());
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());
assertEquals("master", changesets.getBranchName());
assertEquals("main", changesets.getBranchName());
}
@Test