Skip to content

Commit a3b9437

Browse files
committed
Use Path and Files.createDirectory for more robust error handling
1 parent 9cd2b49 commit a3b9437

File tree

1 file changed

+12
-17
lines changed

1 file changed

+12
-17
lines changed

src/main/java/org/commonwl/view/git/GitService.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@
3333
import org.springframework.beans.factory.annotation.Value;
3434
import org.springframework.stereotype.Service;
3535

36-
import java.io.File;
3736
import java.io.IOException;
3837
import java.net.URI;
3938
import java.net.URISyntaxException;
39+
import java.nio.file.Files;
4040
import java.nio.file.Path;
4141
import java.util.HashSet;
4242
import java.util.Set;
@@ -71,30 +71,28 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
7171
* @returns The git object for the repository
7272
*/
7373
public Git getRepository(GitDetails gitDetails, boolean reuseDir)
74-
throws GitAPIException {
74+
throws GitAPIException, IOException {
7575
Git repo = null;
7676
while (repo == null) {
7777
try {
7878
if (reuseDir) {
7979
// Base dir from configuration, name from hash of repository URL
80-
File baseDir = new File(gitStorage.toString());
8180
String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
8281

8382
// Check if folder already exists
84-
File repoDir = new File(baseDir, baseName);
85-
if (repoDir.exists() && repoDir.isDirectory()) {
86-
repo = Git.open(repoDir);
83+
Path repoDir = gitStorage.resolve(baseName);
84+
if (Files.isReadable(repoDir) && Files.isDirectory(repoDir)) {
85+
repo = Git.open(repoDir.toFile());
8786
repo.fetch().call();
8887
} else {
8988
// Create a folder and clone repository into it
90-
if (repoDir.mkdir()) {
91-
repo = Git.cloneRepository()
92-
.setCloneSubmodules(cloneSubmodules)
93-
.setURI(gitDetails.getRepoUrl())
94-
.setDirectory(repoDir)
95-
.setCloneAllBranches(true)
96-
.call();
97-
}
89+
Files.createDirectory(repoDir);
90+
repo = Git.cloneRepository()
91+
.setCloneSubmodules(cloneSubmodules)
92+
.setURI(gitDetails.getRepoUrl())
93+
.setDirectory(repoDir.toFile())
94+
.setCloneAllBranches(true)
95+
.call();
9896
}
9997
} else {
10098
// Another thread is already using the existing folder
@@ -126,9 +124,6 @@ public Git getRepository(GitDetails gitDetails, boolean reuseDir)
126124
} else {
127125
throw ex;
128126
}
129-
} catch (IOException ex) {
130-
logger.error("Could not open existing Git repository for '"
131-
+ gitDetails.getRepoUrl() + "'", ex);
132127
}
133128
}
134129

0 commit comments

Comments
 (0)