From 5f82569cab3386306bca546b87d3919723449c90 Mon Sep 17 00:00:00 2001 From: "Shawn O. Pearce" Date: Mon, 14 Mar 2011 09:07:53 -0700 Subject: [PATCH] FS: Allow gitPrefix to be set and cached This permits callers to modify the meaning of gitPrefix, which may be useful if their application allows the user to select the location where C Git is installed. Bug: 337101 Change-Id: I07362a5772da4955e01406bdeb8eaf87416be1d6 Signed-off-by: Shawn O. Pearce --- .../src/org/eclipse/jgit/util/FS.java | 34 ++++++++++++++++++- .../src/org/eclipse/jgit/util/FS_POSIX.java | 2 +- .../src/org/eclipse/jgit/util/FS_Win32.java | 31 ++++++----------- 3 files changed, 45 insertions(+), 22 deletions(-) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java index 857b980a3..51b5a45ac 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS.java @@ -101,6 +101,8 @@ public static FS detect(Boolean cygwinUsed) { private final File userHome; + private volatile Holder gitPrefix; + /** * Constructs a file system abstraction. */ @@ -258,7 +260,29 @@ protected static String readPipe(File dir, String[] command, String encoding) { } /** @return the $prefix directory C Git would use. */ - public abstract File gitPrefix(); + public File gitPrefix() { + Holder p = gitPrefix; + if (p == null) { + p = new Holder(discoverGitPrefix()); + gitPrefix = p; + } + return p.value; + } + + /** @return the $prefix directory C Git would use. */ + protected abstract File discoverGitPrefix(); + + /** + * Set the $prefix directory C Git uses. + * + * @param path + * the directory. Null if C Git is not installed. + * @return {@code this} + */ + public FS setGitPrefix(File path) { + gitPrefix = new Holder(path); + return this; + } /** * Initialize a ProcesssBuilder to run a command using the system shell. @@ -273,4 +297,12 @@ protected static String readPipe(File dir, String[] command, String encoding) { * populating directory, environment, and then start the process. */ public abstract ProcessBuilder runInShell(String cmd, String[] args); + + private static class Holder { + final V value; + + Holder(V value) { + this.value = value; + } + } } diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java index 14fac15df..180b016be 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_POSIX.java @@ -52,7 +52,7 @@ abstract class FS_POSIX extends FS { @Override - public File gitPrefix() { + protected File discoverGitPrefix() { String path = SystemReader.getInstance().getenv("PATH"); File gitExe = searchPath(path, "git"); if (gitExe != null) diff --git a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java index 7e1b6434c..5838fed6a 100644 --- a/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java +++ b/org.eclipse.jgit/src/org/eclipse/jgit/util/FS_Win32.java @@ -64,9 +64,6 @@ public String run() { && StringUtils.toLowerCase(osDotName).indexOf("windows") != -1; } - private File gitPrefix; - private boolean gitPrefixEvaluated; - public boolean supportsExecute() { return false; } @@ -85,27 +82,21 @@ public boolean retryFailedLockFileCommit() { } @Override - public File gitPrefix() { - if (gitPrefixEvaluated) - return gitPrefix; - + protected File discoverGitPrefix() { String path = SystemReader.getInstance().getenv("PATH"); File gitExe = searchPath(path, "git.exe", "git.cmd"); if (gitExe != null) - gitPrefix = gitExe.getParentFile().getParentFile(); - else { - // This isn't likely to work, if bash is in $PATH, git should - // also be in $PATH. But its worth trying. - // - String w = readPipe(userHome(), // - new String[] { "bash", "--login", "-c", "which git" }, // - Charset.defaultCharset().name()); - if (w != null) - gitPrefix = new File(w).getParentFile().getParentFile(); - } + return gitExe.getParentFile().getParentFile(); - gitPrefixEvaluated = true; - return gitPrefix; + // This isn't likely to work, if bash is in $PATH, git should + // also be in $PATH. But its worth trying. + // + String w = readPipe(userHome(), // + new String[] { "bash", "--login", "-c", "which git" }, // + Charset.defaultCharset().name()); + if (w != null) + return new File(w).getParentFile().getParentFile(); + return null; } @Override