Skip to content
Permalink
Browse files

Cache the value of root() in GitRepository

Reviewed-by: rwestberg
  • Loading branch information
Erik Helin
Erik Helin committed Aug 16, 2019
1 parent f9979b0 commit 8f81e3277ff8cda1a563a254d77c71b104cf2e71
Showing with 16 additions and 2 deletions.
  1. +16 −2 vcs/src/main/java/org/openjdk/skara/vcs/git/GitRepository.java
@@ -40,6 +40,7 @@
public class GitRepository implements Repository {
private final Path dir;
private final Logger log = Logger.getLogger("org.openjdk.skara.vcs.git");
private Path cachedRoot = null;

private java.lang.Process start(String... cmd) throws IOException {
return start(Arrays.asList(cmd));
@@ -275,6 +276,8 @@ public boolean isHealthy() throws IOException {

@Override
public void clean() throws IOException {
cachedRoot = null;

try (var p = capture("git", "clean", "-x", "-d", "--force", "--force")) {
await(p);
}
@@ -290,6 +293,8 @@ public void clean() throws IOException {

@Override
public Repository reinitialize() throws IOException {
cachedRoot = null;

Files.walk(dir)
.map(Path::toFile)
.sorted(Comparator.reverseOrder())
@@ -337,6 +342,8 @@ public void checkout(Branch b, boolean force) throws IOException {

@Override
public Repository init() throws IOException {
cachedRoot = null;

if (!Files.exists(dir)) {
Files.createDirectories(dir);
}
@@ -401,6 +408,10 @@ public boolean exists() throws IOException {

@Override
public Path root() throws IOException {
if (cachedRoot != null) {
return cachedRoot;
}

try (var p = capture("git", "rev-parse", "--show-toplevel")) {
var res = await(p);
if (res.stdout().size() != 1) {
@@ -410,10 +421,13 @@ public Path root() throws IOException {
if (res2.stdout().size() != 1) {
throw new IOException("Unexpected output\n" + res2);
}
return dir.resolve(Path.of(res2.stdout().get(0)));
cachedRoot = dir.resolve(Path.of(res2.stdout().get(0)));
return cachedRoot;
}
}
return Path.of(res.stdout().get(0));

cachedRoot = Path.of(res.stdout().get(0));
return cachedRoot;
}
}

0 comments on commit 8f81e32

Please sign in to comment.
You can’t perform that action at this time.