Signed-off-by: Florian Koeberle <florianskarten@xxxxxx> --- .../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 -- 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