[JGIT PATCH 04/22] Added a "init" command to the git like command line tool.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Signed-off-by: Florian Koeberle <florianskarten@xxxxxx>
---
 .../src/org/spearce/jgit/pgm/InitCommand.java      |   71 ++++++++++++++++++++
 .../src/org/spearce/jgit/pgm/MainProgram.java      |    1 +
 2 files changed, 72 insertions(+), 0 deletions(-)
 create mode 100644 org.spearce.jgit/src/org/spearce/jgit/pgm/InitCommand.java

diff --git a/org.spearce.jgit/src/org/spearce/jgit/pgm/InitCommand.java b/org.spearce.jgit/src/org/spearce/jgit/pgm/InitCommand.java
new file mode 100644
index 0000000..4bc7ca8
--- /dev/null
+++ b/org.spearce.jgit/src/org/spearce/jgit/pgm/InitCommand.java
@@ -0,0 +1,71 @@
+/*
+ *  Copyright (C) 2008 Florian Köberle
+ *
+ *  This library is free software; you can redistribute it and/or
+ *  modify it under the terms of the GNU General Public
+ *  License, version 2, as published by the Free Software Foundation.
+ *
+ *  This library is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ *  General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public
+ *  License along with this library; if not, write to the Free Software
+ *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
+ */
+package org.spearce.jgit.pgm;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.spearce.jgit.lib.Project;
+import org.spearce.jgit.lib.ProjectFactory;
+
+import static java.lang.String.format;
+
+/**
+ * This class is immutable. It's {@link #execute} method is used to create git
+ * repositories.
+ * 
+ * @author Florian Köberle
+ * 
+ */
+public class InitCommand implements Command {
+	/**
+	 * Use this instance instead of creating a new ${link InitCommand}. You
+	 * don't need to create an instance of this class as it is immutable and not
+	 * configurable.
+	 */
+	public static InitCommand INSTANCE = new InitCommand();
+
+	public void execute(String... args) throws IOException {
+		final String currentDirectoryPathString = System
+				.getProperty("user.dir");
+		if (currentDirectoryPathString == null) {
+			throw new IOException("Unable to get current working directory");
+		}
+		final File currentDirectoryPath = new File(currentDirectoryPathString);
+		if (!currentDirectoryPath.isDirectory()) {
+			final String message = format(
+					"No directory found at \"%s\". It was the current working directory at program start.",
+					currentDirectoryPathString);
+			throw new IOException(message);
+		}
+		final ProjectFactory projectFactory = new ProjectFactory();
+
+		final Project project = projectFactory
+				.createProject(currentDirectoryPath);
+		try {
+			System.out.printf("Created a git repository at: %s%n", project
+					.getRepository().getDirectory());
+		} finally {
+			project.closeRepository();
+		}
+
+	}
+
+	public String getShortDescription() {
+		return "Creates an empty git repository.";
+	}
+}
diff --git a/org.spearce.jgit/src/org/spearce/jgit/pgm/MainProgram.java b/org.spearce.jgit/src/org/spearce/jgit/pgm/MainProgram.java
index 69cd96f..7601f31 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/pgm/MainProgram.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/pgm/MainProgram.java
@@ -35,6 +35,7 @@ public class MainProgram {
 
 	static {
 		final Map<String, Command> commands = new HashMap<String, Command>();
+		commands.put("init", InitCommand.INSTANCE);
 		commands.put("help", HelpCommand.INSTANCE);
 		commandNameToObjectMap = Collections.unmodifiableMap(commands);
 	}
-- 
1.5.2.5

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]

  Powered by Linux