Skip to content

Commit 927deed

Browse files
committed
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 <[email protected]>
1 parent cb8924a commit 927deed

File tree

4 files changed

+76
-4
lines changed

4 files changed

+76
-4
lines changed

org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ usage_updateRef=reference to update
432432
usage_updateRemoteRefsFromAnotherRepository=Update remote refs from another repository
433433
usage_useNameInsteadOfOriginToTrackUpstream=use <name> instead of 'origin' to track upstream
434434
usage_checkoutBranchAfterClone=check out named branch instead of remote's HEAD
435-
usage_initialBranch=initial branch in the newly created repository (default 'master')
435+
usage_initialBranch=initial branch of the newly created repository (default 'master', can be configured via config option init.defaultBranch)
436436
usage_viewCommitHistory=View commit history
437437
usage_orphan=Create a new orphan branch. The first commit made on this new branch will have no parents and it will be the root of a new history totally disconnected from other branches and commits.
438438
usernameFor=Username for {0}:

org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,10 @@
2222
import org.eclipse.jgit.errors.NoWorkTreeException;
2323
import org.eclipse.jgit.junit.MockSystemReader;
2424
import org.eclipse.jgit.junit.RepositoryTestCase;
25+
import org.eclipse.jgit.lib.ConfigConstants;
2526
import org.eclipse.jgit.lib.Constants;
2627
import org.eclipse.jgit.lib.Repository;
28+
import org.eclipse.jgit.lib.StoredConfig;
2729
import org.eclipse.jgit.util.SystemReader;
2830
import org.junit.Before;
2931
import org.junit.Test;
@@ -63,6 +65,56 @@ public void testInitRepositoryMainInitialBranch()
6365
}
6466
}
6567

68+
@Test
69+
public void testInitRepositoryCustomDefaultBranch()
70+
throws Exception {
71+
File directory = createTempDirectory("testInitRepository");
72+
InitCommand command = new InitCommand();
73+
command.setDirectory(directory);
74+
MockSystemReader reader = (MockSystemReader) SystemReader.getInstance();
75+
StoredConfig c = reader.getUserConfig();
76+
String old = c.getString(ConfigConstants.CONFIG_INIT_SECTION, null,
77+
ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH);
78+
c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
79+
ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, "main");
80+
try (Git git = command.call()) {
81+
Repository r = git.getRepository();
82+
assertNotNull(r);
83+
assertEquals("refs/heads/main", r.getFullBranch());
84+
} finally {
85+
c.setString(ConfigConstants.CONFIG_INIT_SECTION, null,
86+
ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH, old);
87+
}
88+
}
89+
90+
@Test
91+
public void testInitRepositoryNullInitialBranch() throws Exception {
92+
File directory = createTempDirectory("testInitRepository");
93+
InitCommand command = new InitCommand();
94+
command.setDirectory(directory);
95+
command.setInitialBranch("main");
96+
command.setInitialBranch(null);
97+
try (Git git = command.call()) {
98+
Repository r = git.getRepository();
99+
assertNotNull(r);
100+
assertEquals("refs/heads/master", r.getFullBranch());
101+
}
102+
}
103+
104+
@Test
105+
public void testInitRepositoryEmptyInitialBranch() throws Exception {
106+
File directory = createTempDirectory("testInitRepository");
107+
InitCommand command = new InitCommand();
108+
command.setDirectory(directory);
109+
command.setInitialBranch("main");
110+
command.setInitialBranch("");
111+
try (Git git = command.call()) {
112+
Repository r = git.getRepository();
113+
assertNotNull(r);
114+
assertEquals("refs/heads/master", r.getFullBranch());
115+
}
116+
}
117+
66118
@Test
67119
public void testInitNonEmptyRepository() throws IOException,
68120
JGitInternalException, GitAPIException {

org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,14 @@
1717
import org.eclipse.jgit.api.errors.GitAPIException;
1818
import org.eclipse.jgit.api.errors.InvalidRefNameException;
1919
import org.eclipse.jgit.api.errors.JGitInternalException;
20+
import org.eclipse.jgit.errors.ConfigInvalidException;
2021
import org.eclipse.jgit.internal.JGitText;
22+
import org.eclipse.jgit.lib.ConfigConstants;
2123
import org.eclipse.jgit.lib.Constants;
2224
import org.eclipse.jgit.lib.Repository;
2325
import org.eclipse.jgit.lib.RepositoryBuilder;
2426
import org.eclipse.jgit.util.FS;
27+
import org.eclipse.jgit.util.StringUtils;
2528
import org.eclipse.jgit.util.SystemReader;
2629

2730
/**
@@ -39,7 +42,7 @@ public class InitCommand implements Callable<Git> {
3942

4043
private FS fs;
4144

42-
private String initialBranch = Constants.MASTER;
45+
private String initialBranch;
4346

4447
/**
4548
* {@inheritDoc}
@@ -90,12 +93,16 @@ public Git call() throws GitAPIException {
9093
builder.setWorkTree(new File(dStr));
9194
}
9295
}
93-
builder.setInitialBranch(initialBranch);
96+
builder.setInitialBranch(StringUtils.isEmptyOrNull(initialBranch)
97+
? SystemReader.getInstance().getUserConfig().getString(
98+
ConfigConstants.CONFIG_INIT_SECTION, null,
99+
ConfigConstants.CONFIG_KEY_DEFAULT_BRANCH)
100+
: initialBranch);
94101
Repository repository = builder.build();
95102
if (!repository.getObjectDatabase().exists())
96103
repository.create(bare);
97104
return new Git(repository, true);
98-
} catch (IOException e) {
105+
} catch (IOException | ConfigInvalidException e) {
99106
throw new JGitInternalException(e.getMessage(), e);
100107
}
101108
}

org.eclipse.jgit/src/org/eclipse/jgit/lib/ConfigConstants.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -715,4 +715,17 @@ public final class ConfigConstants {
715715
*/
716716
public static final String CONFIG_KEY_VERSION = "version";
717717

718+
/**
719+
* The "init" section
720+
*
721+
* @since 5.11
722+
*/
723+
public static final String CONFIG_INIT_SECTION = "init";
724+
725+
/**
726+
* The "defaultBranch" key
727+
*
728+
* @since 5.11
729+
*/
730+
public static final String CONFIG_KEY_DEFAULT_BRANCH = "defaultbranch";
718731
}

0 commit comments

Comments
 (0)