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 <spearce@spearce.org>tags/v0.12.1
private final File userHome; | private final File userHome; | ||||
private volatile Holder<File> gitPrefix; | |||||
/** | /** | ||||
* Constructs a file system abstraction. | * Constructs a file system abstraction. | ||||
*/ | */ | ||||
} | } | ||||
/** @return the $prefix directory C Git would use. */ | /** @return the $prefix directory C Git would use. */ | ||||
public abstract File gitPrefix(); | |||||
public File gitPrefix() { | |||||
Holder<File> p = gitPrefix; | |||||
if (p == null) { | |||||
p = new Holder<File>(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<File>(path); | |||||
return this; | |||||
} | |||||
/** | /** | ||||
* Initialize a ProcesssBuilder to run a command using the system shell. | * Initialize a ProcesssBuilder to run a command using the system shell. | ||||
* populating directory, environment, and then start the process. | * populating directory, environment, and then start the process. | ||||
*/ | */ | ||||
public abstract ProcessBuilder runInShell(String cmd, String[] args); | public abstract ProcessBuilder runInShell(String cmd, String[] args); | ||||
private static class Holder<V> { | |||||
final V value; | |||||
Holder(V value) { | |||||
this.value = value; | |||||
} | |||||
} | |||||
} | } |
abstract class FS_POSIX extends FS { | abstract class FS_POSIX extends FS { | ||||
@Override | @Override | ||||
public File gitPrefix() { | |||||
protected File discoverGitPrefix() { | |||||
String path = SystemReader.getInstance().getenv("PATH"); | String path = SystemReader.getInstance().getenv("PATH"); | ||||
File gitExe = searchPath(path, "git"); | File gitExe = searchPath(path, "git"); | ||||
if (gitExe != null) | if (gitExe != null) |
&& StringUtils.toLowerCase(osDotName).indexOf("windows") != -1; | && StringUtils.toLowerCase(osDotName).indexOf("windows") != -1; | ||||
} | } | ||||
private File gitPrefix; | |||||
private boolean gitPrefixEvaluated; | |||||
public boolean supportsExecute() { | public boolean supportsExecute() { | ||||
return false; | return false; | ||||
} | } | ||||
} | } | ||||
@Override | @Override | ||||
public File gitPrefix() { | |||||
if (gitPrefixEvaluated) | |||||
return gitPrefix; | |||||
protected File discoverGitPrefix() { | |||||
String path = SystemReader.getInstance().getenv("PATH"); | String path = SystemReader.getInstance().getenv("PATH"); | ||||
File gitExe = searchPath(path, "git.exe", "git.cmd"); | File gitExe = searchPath(path, "git.exe", "git.cmd"); | ||||
if (gitExe != null) | 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 | @Override |