[JGIT PATCH v2 07/24] Added findWorkTree method to Repository class.
From: Florian Koeberle <hidden>
Date: 2016-06-15 22:44:36
Subsystem:
the rest · Maintainer:
Linus Torvalds
Signed-off-by: Florian Koeberle <redacted> --- .../src/org/spearce/jgit/lib/Repository.java | 90 ++++++++++++++++++++ 1 files changed, 90 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 3186fe2..ed5bd89 100644
--- a/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java
+++ b/org.spearce.jgit/src/org/spearce/jgit/lib/Repository.java@@ -16,6 +16,11 @@ */ package org.spearce.jgit.lib; +import static org.spearce.jgit.lib.Constants.HEAD_FILE_NAME; +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;
@@ -1242,4 +1247,89 @@ public class Repository { } } + /** + * Find the git repository for the current working directory. + * + * @return a {@link Repository}. + * @throws IOException + * if the system property user.dir isn't set or if it is + * invalid. + */ + public static WorkTree findWorkTree() throws IOException { + final String workingDirectoryPath = System.getProperty("user.dir"); + if (workingDirectoryPath == null) { + throw new IOException("unable to get working directory"); + } + final File workingDirectoryFile = new File(workingDirectoryPath); + if (!workingDirectoryFile.exists()) { + throw new IOException("Working directory path is invalid"); + } + return findWorkTree(workingDirectoryFile); + } + + /** + * 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_FILE_NAME); + if (!hasValidateHeadRef(head)) { + return false; + } + + return true; + } + + /** + * Checks for example if a path is a valid HEAD file. Should do the same + * like validate_headref from path.c. + * + * @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 stub. view int validate_headref(const char + // *path) at path.c + } + + private static WorkTree findWorkTree(File directory) throws IOException { + File currentDirectory = directory.getAbsoluteFile(); + while (true) { + 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(); + if (currentDirectory == null) { + throw new IOException("Can't find git repository"); + } + } + + } + }
--
1.5.4.3