aboutsummaryrefslogtreecommitdiffstats
path: root/org.eclipse.jgit.test
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2021-01-25 02:43:18 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2021-02-22 23:11:45 +0100
commit927deed5a569bd09ad5d23e815bcc3d68d14de91 (patch)
treed6facb1e1a66895d900da5ca665971dc436a5a1d /org.eclipse.jgit.test
parentcb8924a80d9e07182a056c01acd418a6beddcc0f (diff)
downloadjgit-927deed5a569bd09ad5d23e815bcc3d68d14de91.tar.gz
jgit-927deed5a569bd09ad5d23e815bcc3d68d14de91.zip
init: add config option to set default for the initial branch name
We introduced the option --initial-branch=<branch-name> to allow initializing a new repository with a different initial branch. To allow users to override the initial branch name more permanently (i.e. without having to specify the name manually for each 'git init'), introduce the 'init.defaultBranch' option. This option was added to git in 2.28.0. See https://git-scm.com/docs/git-config#Documentation/git-config.txt-initdefaultBranch Bug: 564794 Change-Id: I679b14057a54cd3d19e44460c4a5bd3a368ec848 Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
Diffstat (limited to 'org.eclipse.jgit.test')
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java52
1 files changed, 52 insertions, 0 deletions
diff --git a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java
index 14c52c207a..48d835ed26 100644
--- a/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java
+++ b/org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java
@@ -22,8 +22,10 @@ import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.errors.NoWorkTreeException;
import org.eclipse.jgit.junit.MockSystemReader;
import org.eclipse.jgit.junit.RepositoryTestCase;
+import org.eclipse.jgit.lib.ConfigConstants;
import org.eclipse.jgit.lib.Constants;
import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.lib.StoredConfig;
import org.eclipse.jgit.util.SystemReader;
import org.junit.Before;
import org.junit.Test;
@@ -64,6 +66,56 @@ public class InitCommandTest extends RepositoryTestCase {
}
@Test
+ public void testInitRepositoryCustomDefaultBranch()
+ throws Exception {
+ File directory = createTempDirectory("testInitRepository");
+ InitCommand command = new InitCommand();
+ command.setDirectory(directory);
+ MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
+ StoredConfig c = reader.getUserConfig();
+ String old = c.getString(ConfigConstants.CONFIG_INIT_SECTION, null,
+ ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH);
+ c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
+ ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, "main");
+ try (Git git = command.call()) {
+ Repository r = git.getRepository();
+ assertNotNull(r);
+ assertEquals("refs/heads/main", r.getFullBranch());
+ } finally {
+ c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
+ ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, old);
+ }
+ }
+
+ @Test
+ public void testInitRepositoryNullInitialBranch() throws Exception {
+ File directory = createTempDirectory("testInitRepository");
+ InitCommand command = new InitCommand();
+ command.setDirectory(directory);
+ command.setInitialBranch("main");
+ command.setInitialBranch(null);
+ try (Git git = command.call()) {
+ Repository r = git.getRepository();
+ assertNotNull(r);
+ assertEquals("refs/heads/master", r.getFullBranch());
+ }
+ }
+
+ @Test
+ public void testInitRepositoryEmptyInitialBranch() throws Exception {
+ File directory = createTempDirectory("testInitRepository");
+ InitCommand command = new InitCommand();
+ command.setDirectory(directory);
+ command.setInitialBranch("main");
+ command.setInitialBranch("");
+ try (Git git = command.call()) {
+ Repository r = git.getRepository();
+ assertNotNull(r);
+ assertEquals("refs/heads/master", r.getFullBranch());
+ }
+ }
+
+ @Test
public void testInitNonEmptyRepository() throws IOException,
JGitInternalException, GitAPIException {
File directory = createTempDirectory("testInitRepository2");