[EGIT PATCH 19/23] Repository search for command line tools

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

 



Introducing some simple search for git repository in parent directories
of current directory.

Previous version was annoying: we had to be in directory that contains
.git/ or specify it explicitly by --git-dir.

Signed-off-by: Marek Zawirski <marek.zawirski@xxxxxxxxx>
---
 .../src/org/spearce/jgit/pgm/Main.java             |   24 +++++++++++++++++--
 1 files changed, 21 insertions(+), 3 deletions(-)

diff --git a/org.spearce.jgit/src/org/spearce/jgit/pgm/Main.java b/org.spearce.jgit/src/org/spearce/jgit/pgm/Main.java
index 44f8a42..8afd0d7 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/pgm/Main.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/pgm/Main.java
@@ -85,12 +85,12 @@ public class Main {
 
 	private static void execute(final String[] argv) throws Exception {
 		int argi = 0;
-		String gitdir = ".git";
 
+		File gitdir = null;
 		for (; argi < argv.length; argi++) {
 			final String arg = argv[argi];
 			if (arg.startsWith("--git-dir="))
-				gitdir = arg.substring("--git-dir=".length());
+				gitdir = new File(arg.substring("--git-dir=".length()));
 			else if (arg.equals("--show-stack-trace"))
 				showStackTrace = true;
 			else if (arg.startsWith("--"))
@@ -101,8 +101,15 @@ public class Main {
 
 		if (argi == argv.length)
 			usage();
+		if (gitdir == null)
+			gitdir = findGitDir();
+		if (gitdir == null || !gitdir.isDirectory()) {
+			System.err.println("error: can't find git directory");
+			System.exit(1);
+		}
+
 		final TextBuiltin cmd = createCommand(argv[argi++]);
-		cmd.db = new Repository(new File(gitdir));
+		cmd.db = new Repository(gitdir);
 		try {
 			cmd.execute(subarray(argv, argi));
 		} finally {
@@ -111,6 +118,17 @@ public class Main {
 		}
 	}
 
+	private static File findGitDir() {
+		File current = new File(".").getAbsoluteFile();
+		while (current != null) {
+			final File gitDir = new File(current, ".git");
+			if (gitDir.isDirectory())
+				return gitDir;
+			current = current.getParentFile();
+		}
+		return null;
+	}
+
 	private static String[] subarray(final String[] argv, final int i) {
 		return Arrays.asList(argv).subList(i, argv.length).toArray(
 				new String[0]);
-- 
1.5.5.3

--
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