Skip to contentSkip to conversation

Added support for creating bare repositories

PATCH In review Constantine Plotnikov 2 participants 3 messages Jun 15, 2016

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Added support for creating bare repositories

Now it is possible to create bare repositories. The difference from normal repository creation is that directory where repository is created might exists and that core.bare property is set to true.

---
 .../src/org/spearce/jgit/pgm/Init.java             |    8 ++++-
 .../src/org/spearce/jgit/lib/Repository.java       |   28 ++++++++++++++++---
 2 files changed, 29 insertions(+), 7 deletions(-)
org.spearce.jgit.pgm/src/org/spearce/jgit/pgm/Init.java +6 -2
index 197864d..3ab1599 100644
@@ -39,10 +39,14 @@
 
 import java.io.File;
 
+import org.kohsuke.args4j.Option;
 import org.spearce.jgit.lib.Repository;
 
 @Command(common = true, usage = "Create an empty git repository")
 class Init extends TextBuiltin {
+	@Option(name = "--bare", usage = "Create a bare repository")
+	private boolean bare;
+
 	@Override
 	protected final boolean requiresRepository() {
 		return false;
@@ -51,9 +55,9 @@ protected final boolean requiresRepository() {
 	@Override
 	protected void run() throws Exception {
 		if (gitdir == null)
-			gitdir = new File(".git");
+			gitdir = new File(bare ? "." : ".git");
 		db = new Repository(gitdir);
-		db.create();
+		db.create(bare);
 		out.println("Initialized empty Git repository in "
 				+ gitdir.getAbsolutePath());
 	}
org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java +23 -5
index cfd92b8..f2b0b3f 100644
@@ -161,16 +161,30 @@ public Repository(final File d) throws IOException {
 
 	/**
 	 * Create a new Git repository initializing the necessary files and
-	 * directories.
+	 * directories. Repository with working tree is created using this method.
 	 *
 	 * @throws IOException
+	 * @see #create(boolean)
 	 */
 	public synchronized void create() throws IOException {
-		if (gitDir.exists()) {
+		create(false);
+	}
+
+	/**
+	 * Create a new Git repository initializing the necessary files and
+	 * directories.
+	 * 
+	 * @param bare
+	 *            if true, a bare repository is created.
+	 * 
+	 * @throws IOException
+	 *             in case of IO problem
+	 */
+	public void create(boolean bare) throws IOException {
+		if ((bare ? new File(gitDir, "config") : gitDir).exists()) {
 			throw new IllegalStateException("Repository already exists: "
 					+ gitDir);
 		}
-
 		gitDir.mkdirs();
 		refs.create();
 
@@ -183,8 +197,12 @@ public synchronized void create() throws IOException {
 		final String master = Constants.R_HEADS + Constants.MASTER;
 		refs.link(Constants.HEAD, master);
 
-		getConfig().create();
-		getConfig().save();
+		RepositoryConfig cfg = getConfig();
+		cfg.create();
+		if (bare) {
+			cfg.setString("core", null, "bare", "true");
+		}
+		cfg.save();
 	}
 
 	private synchronized File[] objectsDirs(){
Signature · 1 line
1.6.1.2
#1 lore raw

fredag 24 april 2009 17:36:57 skrev Constantine Plotnikov <[email protected]>:

Quoted 4 lines

Now it is possible to create bare repositories. The difference from normal repository creation is that directory where repository is created might exists and that core.bare property is set to true.

I know C Git doesn't check if a directory is a non-bare repo, but I think that's wrong, so if I try to create a bare repo in a non-bare repo that should be an error. For now I'll accept it anyway since the C version allows it and it is useful.

-- robin

#2 lore raw
Quoted 11 lines · Robin Rosenberg

fredag 24 april 2009 17:36:57 skrev Constantine Plotnikov <[email protected]>:

Quoted 4 lines

Now it is possible to create bare repositories. The difference from normal repository creation is that directory where repository is created might exists and that core.bare property is set to true.

I know C Git doesn't check if a directory is a non-bare repo, but I think that's wrong, so if I try to create a bare repo in a non-bare repo that should be an error. For now I'll accept it anyway since the C version allows it and it is useful.

I'm not sure that this should be an error. We have a bare git repository with a test data inside the non-bare repository. And such feature might be nice to have to other git related tools as well. %)

Constantine