|
33 | 33 | import org.springframework.beans.factory.annotation.Value;
|
34 | 34 | import org.springframework.stereotype.Service;
|
35 | 35 |
|
36 |
| -import java.io.File; |
37 | 36 | import java.io.IOException;
|
38 | 37 | import java.net.URI;
|
39 | 38 | import java.net.URISyntaxException;
|
| 39 | +import java.nio.file.Files; |
40 | 40 | import java.nio.file.Path;
|
41 | 41 | import java.util.HashSet;
|
42 | 42 | import java.util.Set;
|
@@ -71,30 +71,28 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
|
71 | 71 | * @returns The git object for the repository
|
72 | 72 | */
|
73 | 73 | public Git getRepository(GitDetails gitDetails, boolean reuseDir)
|
74 |
| - throws GitAPIException { |
| 74 | + throws GitAPIException, IOException { |
75 | 75 | Git repo = null;
|
76 | 76 | while (repo == null) {
|
77 | 77 | try {
|
78 | 78 | if (reuseDir) {
|
79 | 79 | // Base dir from configuration, name from hash of repository URL
|
80 |
| - File baseDir = new File(gitStorage.toString()); |
81 | 80 | String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
|
82 | 81 |
|
83 | 82 | // 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()); |
87 | 86 | repo.fetch().call();
|
88 | 87 | } else {
|
89 | 88 | // 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(); |
98 | 96 | }
|
99 | 97 | } else {
|
100 | 98 | // Another thread is already using the existing folder
|
@@ -126,9 +124,6 @@ public Git getRepository(GitDetails gitDetails, boolean reuseDir)
|
126 | 124 | } else {
|
127 | 125 | throw ex;
|
128 | 126 | }
|
129 |
| - } catch (IOException ex) { |
130 |
| - logger.error("Could not open existing Git repository for '" |
131 |
| - + gitDetails.getRepoUrl() + "'", ex); |
132 | 127 | }
|
133 | 128 | }
|
134 | 129 |
|
|
0 commit comments