git/list[1] front-page[2] threads[3] people[4] search[5] about
 

[JGIT PATCH v4 21/24] Added findWorkTree method to Repository class.

From
FKFlorian Koeberle <florianskarten@web.de>
Date
Jun 13, 2008, 18:35 UTC
Message-ID
<1213382121-19786-21-git-send-email-florianskarten@web.de>
In-Reply-To
<4852BCCA.4030404@web.de>
Signed-off-by: Florian Koeberle <florianskarten@web.de>
---
 .../src/org/spearce/jgit/lib/Repository.java       |   80 ++++++++++++++++++++
 1 files changed, 80 insertions(+), 0 deletions(-)
diff --git a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
index d7c3b13..ee7bbe4 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
@@ -39,6 +39,11 @@
 
 package org.spearce.jgit.lib;
 
+import static org.spearce.jgit.lib.Constants.HEAD;
+import static org.spearce.jgit.lib.Constants.OBJECTS_DIRECTORY_NAME;
+import static org.spearce.jgit.lib.Constants.REFS_DIRECTORY_NAME;
+import static org.spearce.jgit.lib.Constants.REPOSITORY_DIRECTORY_NAME;
+
 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -51,6 +56,7 @@ import java.util.HashMap;
 import java.util.Map;
 
 import org.spearce.jgit.errors.IncorrectObjectTypeException;
+import org.spearce.jgit.errors.NoGitRepositoryFoundException;
 import org.spearce.jgit.errors.RevisionSyntaxException;
 import org.spearce.jgit.stgit.StGitPatch;
 import org.spearce.jgit.util.FS;
@@ -1052,4 +1058,78 @@ public class Repository {
 		}
 	}
 
+	/**
+	 * Find the git repository for the current working directory.
+	 *
+	 * @return a {@link Repository}.
+	 * @throws NoGitRepositoryFoundException
+	 *             if no git repository could be found for the current
+	 *             directory.
+	 * @throws IOException
+	 *             if not able to determine the absolute path of the current
+	 *             working directory.
+	 */
+	public static WorkTree findWorkTree() throws NoGitRepositoryFoundException,
+			IOException {
+		return findWorkTree(new File("."));
+	}
+
+	/**
+	 * Checks if a path is a valid git repository. Works similar like the method
+	 * is_git_directory from the original setup.c file.
+	 *
+	 * @param directory
+	 *            the path which should be checked.
+	 * @return true if the path is a valid git repository.
+	 */
+	private static boolean isRepository(File directory) {
+		if (!directory.isDirectory()) {
+			return false;
+		}
+
+		final File objectDirectory = new File(directory, OBJECTS_DIRECTORY_NAME);
+		if (!objectDirectory.isDirectory()) {
+			return false;
+		}
+
+		final File refsDirectory = new File(directory, REFS_DIRECTORY_NAME);
+		if (!refsDirectory.isDirectory()) {
+			return false;
+		}
+
+		final File head = new File(directory, HEAD);
+		if (!hasValidateHeadRef(head)) {
+			return false;
+		}
+
+		return true;
+	}
+
+	/**
+	 * Checks for example if a path is a valid HEAD file.
+	 *
+	 * @param path
+	 *            is the path of the HEAD file.
+	 * @return true if it has a valid head reference.
+	 */
+	private static final boolean hasValidateHeadRef(File path) {
+		return true; // TODO implement this method
+	}
+
+	private static WorkTree findWorkTree(final File directory)
+			throws IOException, NoGitRepositoryFoundException {
+		File currentDirectory = directory.getAbsoluteFile();
+		while (currentDirectory != null) {
+			final File commonGitDirectory = new File(directory,
+					REPOSITORY_DIRECTORY_NAME);
+			if (isRepository(commonGitDirectory))
+				return new WorkTree(currentDirectory, new Repository(
+						commonGitDirectory));
+			if (isRepository(currentDirectory))
+				return new WorkTree(null, new Repository(currentDirectory));
+			currentDirectory = currentDirectory.getParentFile();
+		}
+		throw new NoGitRepositoryFoundException(directory);
+	}
+
 }
-- 
1.5.4.3
Previous: Florian KoeberleNext: Florian Koeberle
Message 22 of 26 in “Implementation of fnmatch and the ignore rules”
  1. Implementation of fnmatch and the ignore rulesFlorian Köberle, Jun 13, 2008
  2. 01/24 Added the package fnmatch and two exceptions.Florian Koeberle, Jun 13, 2008
  3. 02/24 Added the interface FilePattern.Florian Koeberle, Jun 13, 2008
  4. 03/24 Added the class Rule.Florian Koeberle, Jun 13, 2008
  5. 04/24 Added the iterface Rules.Florian Koeberle, Jun 13, 2008
  6. 05/24 Added the class FileNamePattern.Florian Koeberle, Jun 13, 2008
  7. 06/24 Added the class FilePathPattern.Florian Koeberle, Jun 13, 2008
  8. 07/24 Added the class IgnoreRuleListFactory.Florian Koeberle, Jun 13, 2008
  9. 08/24 Added a Rules interface implementation and a factory for it.Florian Koeberle, Jun 13, 2008
  10. 09/24 Added test class OverallIgnoreRulestest.Florian Koeberle, Jun 13, 2008
  11. 10/24 Added the class PathNotInProjectDirectoryException.Florian Koeberle, Jun 13, 2008
  12. 11/24 Added the class AddRuleListFactory.Florian Koeberle, Jun 13, 2008
  13. 12/24 Formatted Constants class.Florian Koeberle, Jun 13, 2008
  14. 13/24 Added constant REPOSITORY_DIRECTORY_NAME to Constants class.Florian Koeberle, Jun 13, 2008
  15. 14/24 Added class AddRulesFactory.Florian Koeberle, Jun 13, 2008
  16. 15/24 Added the class LightFileTreeIterator and a test for it.Florian Koeberle, Jun 13, 2008
  17. 16/24 Added class LightFileTreeIterable.Florian Koeberle, Jun 13, 2008
  18. 17/24 Added path related constants to the Constants class.Florian Koeberle, Jun 13, 2008
  19. 18/24 Added WorkTree class which can be constructed over Repository.Florian Koeberle, Jun 13, 2008
  20. 19/24 Added the class NoGitRepositoryFoundException.Florian Koeberle, Jun 13, 2008
  21. 20/24 Formatted Repository class.Florian Koeberle, Jun 13, 2008
  22. 21/24 Added findWorkTree method to Repository class.Florian Koeberle, Jun 13, 2008
  23. 22/24 Formatted RepositoryTestCase.Florian Koeberle, Jun 13, 2008
  24. 23/24 Added a super class for RepositoryTestCase with a createTemporaryDirectory() method.Florian Koeberle, Jun 13, 2008
  25. 24/24 Added the test class AddCommandIterationTest.Florian Koeberle, Jun 13, 2008
  26. Robin RosenbergJun 18, 2008

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.