aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Sohn <matthias.sohn@sap.com>2021-01-25 01:54:03 +0100
committerMatthias Sohn <matthias.sohn@sap.com>2021-02-22 23:11:45 +0100
commitcb8924a80d9e07182a056c01acd418a6beddcc0f (patch)
tree2263bc474b1524894d9321c7e667d664c9cb60f0
parent64cb7148ac64855feb7f7649d1d168d7c6d37860 (diff)
downloadjgit-cb8924a80d9e07182a056c01acd418a6beddcc0f.tar.gz
jgit-cb8924a80d9e07182a056c01acd418a6beddcc0f.zip
init: allow specifying the initial branch name for the new repository
Add option --initial-branch/-b to InitCommand and the CLI init command. This is the first step to implement support for the new option init.defaultBranch. Both were added to git in release 2.28. See https://git-scm.com/docs/git-init#Documentation/git-init.txt--bltbranch-namegt Bug: 564794 Change-Id: Ia383b3f90b5549db80f99b2310450a7faf6bce4c Signed-off-by: Matthias Sohn <matthias.sohn@sap.com>
-rw-r--r--org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java21
-rw-r--r--org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties1
-rw-r--r--org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java8
-rw-r--r--org.eclipse.jgit.test/tst/org/eclipse/jgit/api/InitCommandTest.java36
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java23
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java2
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java42
-rw-r--r--org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java13
8 files changed, 144 insertions, 2 deletions
diff --git a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java
index 84474e33cd..88789d3383 100644
--- a/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java
+++ b/org.eclipse.jgit.pgm.test/tst/org/eclipse/jgit/pgm/InitTest.java
@@ -11,11 +11,14 @@
package org.eclipse.jgit.pgm;
import static org.junit.Assert.assertArrayEquals;
+import static org.junit.Assert.assertEquals;
import java.io.File;
+import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.CLIRepositoryTestCase;
import org.eclipse.jgit.lib.Constants;
+import org.eclipse.jgit.lib.Repository;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
@@ -54,4 +57,22 @@ public class InitTest extends CLIRepositoryTestCase {
assertArrayEquals(expecteds, result);
}
+ @Test
+ public void testInitDirectoryInitialBranch() throws Exception {
+ File workDirectory = tempFolder.getRoot();
+ File gitDirectory = new File(workDirectory, Constants.DOT_GIT);
+
+ String[] result = execute(
+ "git init -b main '" + workDirectory.getCanonicalPath() + "'");
+
+ String[] expecteds = new String[] {
+ "Initialized empty Git repository in "
+ + gitDirectory.getCanonicalPath(),
+ "" };
+ assertArrayEquals(expecteds, result);
+
+ try (Repository repo = new FileRepository(gitDirectory)) {
+ assertEquals("refs/heads/main", repo.getFullBranch());
+ }
+ }
}
diff --git a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
index df55eb0776..a5142802d8 100644
--- a/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
+++ b/org.eclipse.jgit.pgm/resources/org/eclipse/jgit/pgm/internal/CLIText.properties
@@ -432,6 +432,7 @@ usage_updateRef=reference to update
usage_updateRemoteRefsFromAnotherRepository=Update remote refs from another repository
usage_useNameInsteadOfOriginToTrackUpstream=use <name> instead of 'origin' to track upstream
usage_checkoutBranchAfterClone=check out named branch instead of remote's HEAD
+usage_initialBranch=initial branch in the newly created repository (default 'master')
usage_viewCommitHistory=View commit history
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.
usernameFor=Username for {0}:
diff --git a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java
index 7f59ef43dc..7a0d96d419 100644
--- a/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java
+++ b/org.eclipse.jgit.pgm/src/org/eclipse/jgit/pgm/Init.java
@@ -24,6 +24,7 @@ import org.eclipse.jgit.api.InitCommand;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.pgm.internal.CLIText;
+import org.eclipse.jgit.util.StringUtils;
import org.kohsuke.args4j.Argument;
import org.kohsuke.args4j.Option;
@@ -32,6 +33,10 @@ class Init extends TextBuiltin {
@Option(name = "--bare", usage = "usage_CreateABareRepository")
private boolean bare;
+ @Option(name = "--initial-branch", aliases = { "-b" },
+ metaVar = "metaVar_branchName", usage = "usage_initialBranch")
+ private String branch;
+
@Argument(index = 0, metaVar = "metaVar_directory")
private String directory;
@@ -54,6 +59,9 @@ class Init extends TextBuiltin {
}
Repository repository;
try {
+ if (!StringUtils.isEmptyOrNull(branch)) {
+ command.setInitialBranch(branch);
+ }
repository = command.call().getRepository();
outw.println(MessageFormat.format(
CLIText.get().initializedEmptyGitRepositoryIn,
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 1c18b5a8b1..14c52c207a 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
@@ -9,6 +9,7 @@
*/
package org.eclipse.jgit.api;
+import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
@@ -42,7 +43,23 @@ public class InitCommandTest extends RepositoryTestCase {
InitCommand command = new InitCommand();
command.setDirectory(directory);
try (Git git = command.call()) {
- assertNotNull(git.getRepository());
+ Repository r = git.getRepository();
+ assertNotNull(r);
+ assertEquals("refs/heads/master", r.getFullBranch());
+ }
+ }
+
+ @Test
+ public void testInitRepositoryMainInitialBranch()
+ throws IOException, JGitInternalException, GitAPIException {
+ File directory = createTempDirectory("testInitRepository");
+ InitCommand command = new InitCommand();
+ command.setDirectory(directory);
+ command.setInitialBranch("main");
+ try (Git git = command.call()) {
+ Repository r = git.getRepository();
+ assertNotNull(r);
+ assertEquals("refs/heads/main", r.getFullBranch());
}
}
@@ -72,6 +89,23 @@ public class InitCommandTest extends RepositoryTestCase {
Repository repository = git.getRepository();
assertNotNull(repository);
assertTrue(repository.isBare());
+ assertEquals("refs/heads/master", repository.getFullBranch());
+ }
+ }
+
+ @Test
+ public void testInitBareRepositoryMainInitialBranch()
+ throws IOException, JGitInternalException, GitAPIException {
+ File directory = createTempDirectory("testInitBareRepository");
+ InitCommand command = new InitCommand();
+ command.setDirectory(directory);
+ command.setBare(true);
+ command.setInitialBranch("main");
+ try (Git git = command.call()) {
+ Repository repository = git.getRepository();
+ assertNotNull(repository);
+ assertTrue(repository.isBare());
+ assertEquals("refs/heads/main", repository.getFullBranch());
}
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java b/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java
index 41fcf29ed0..b2f7354ed9 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/api/InitCommand.java
@@ -15,6 +15,7 @@ import java.text.MessageFormat;
import java.util.concurrent.Callable;
import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.api.errors.JGitInternalException;
import org.eclipse.jgit.internal.JGitText;
import org.eclipse.jgit.lib.Constants;
@@ -38,6 +39,8 @@ public class InitCommand implements Callable<Git> {
private FS fs;
+ private String initialBranch = Constants.MASTER;
+
/**
* {@inheritDoc}
* <p>
@@ -87,6 +90,7 @@ public class InitCommand implements Callable<Git> {
builder.setWorkTree(new File(dStr));
}
}
+ builder.setInitialBranch(initialBranch);
Repository repository = builder.build();
if (!repository.getObjectDatabase().exists())
repository.create(bare);
@@ -184,4 +188,23 @@ public class InitCommand implements Callable<Git> {
this.fs = fs;
return this;
}
+
+ /**
+ * Set the initial branch of the new repository. If not specified
+ * ({@code null} or empty), fall back to the default name (currently
+ * master).
+ *
+ * @param branch
+ * initial branch name of the new repository
+ * @return {@code this}
+ * @throws InvalidRefNameException
+ * if the branch name is not valid
+ *
+ * @since 5.11
+ */
+ public InitCommand setInitialBranch(String branch)
+ throws InvalidRefNameException {
+ this.initialBranch = branch;
+ return this;
+ }
}
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
index 51ee9e9d10..fecced1ae6 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/internal/storage/file/FileRepository.java
@@ -243,7 +243,7 @@ public class FileRepository extends Repository {
RefUpdate head = updateRef(Constants.HEAD);
head.disableRefLog();
- head.link(Constants.R_HEADS + Constants.MASTER);
+ head.link(Constants.R_HEADS + getInitialBranch());
final boolean fileMode;
if (getFS().supportsExecute()) {
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
index e51995f93d..b2242a11ca 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/BaseRepositoryBuilder.java
@@ -28,6 +28,8 @@ import java.util.Collection;
import java.util.LinkedList;
import java.util.List;
+import org.eclipse.jgit.annotations.NonNull;
+import org.eclipse.jgit.api.errors.InvalidRefNameException;
import org.eclipse.jgit.errors.ConfigInvalidException;
import org.eclipse.jgit.errors.RepositoryNotFoundException;
import org.eclipse.jgit.internal.JGitText;
@@ -38,6 +40,7 @@ import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.util.FS;
import org.eclipse.jgit.util.IO;
import org.eclipse.jgit.util.RawParseUtils;
+import org.eclipse.jgit.util.StringUtils;
import org.eclipse.jgit.util.SystemReader;
/**
@@ -107,6 +110,8 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re
private File workTree;
+ private String initialBranch = Constants.MASTER;
+
/** Directories limiting the search for a Git repository. */
private List<File> ceilingDirectories;
@@ -350,6 +355,43 @@ public class BaseRepositoryBuilder<B extends BaseRepositoryBuilder, R extends Re
}
/**
+ * Set the initial branch of the new repository. If not specified
+ * ({@code null} or empty), fall back to the default name (currently
+ * master).
+ *
+ * @param branch
+ * initial branch name of the new repository. If {@code null} or
+ * empty the configured default branch will be used.
+ * @return {@code this}
+ * @throws InvalidRefNameException
+ * if the branch name is not valid
+ *
+ * @since 5.11
+ */
+ public B setInitialBranch(String branch) throws InvalidRefNameException {
+ if (StringUtils.isEmptyOrNull(branch)) {
+ this.initialBranch = Constants.MASTER;
+ } else {
+ if (!Repository.isValidRefName(Constants.R_HEADS + branch)) {
+ throw new InvalidRefNameException(MessageFormat
+ .format(JGitText.get().branchNameInvalid, branch));
+ }
+ this.initialBranch = branch;
+ }
+ return self();
+ }
+
+ /**
+ * Get the initial branch of the new repository.
+ *
+ * @return the initial branch of the new repository.
+ * @since 5.11
+ */
+ public @NonNull String getInitialBranch() {
+ return initialBranch;
+ }
+
+ /**
* Read standard Git environment variables and configure from those.
* <p>
* This method tries to read the standard Git environment variables, such as
diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
index a7a832c1aa..1e8a6c9175 100644
--- a/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
+++ b/org.eclipse.jgit/src/org/eclipse/jgit/lib/Repository.java
@@ -127,6 +127,8 @@ public abstract class Repository implements AutoCloseable {
/** If not bare, the index file caching the working file states. */
private final File indexFile;
+ private final String initialBranch;
+
/**
* Initialize a new repository instance.
*
@@ -138,6 +140,7 @@ public abstract class Repository implements AutoCloseable {
fs = options.getFS();
workTree = options.getWorkTree();
indexFile = options.getIndexFile();
+ initialBranch = options.getInitialBranch();
}
/**
@@ -1034,6 +1037,16 @@ public abstract class Repository implements AutoCloseable {
}
/**
+ * Get the initial branch name of a new repository
+ *
+ * @return the initial branch name of a new repository
+ * @since 5.11
+ */
+ protected @NonNull String getInitialBranch() {
+ return initialBranch;
+ }
+
+ /**
* Objects known to exist but not expressed by {@link #getAllRefs()}.
* <p>
* When a repository borrows objects from another repository, it can