Skip to content

Commit dc0ad31

Browse files
committed
Clone repository into temporary directory if concurrent access is required
1 parent 3d0c86b commit dc0ad31

File tree

1 file changed

+30
-16
lines changed

1 file changed

+30
-16
lines changed

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

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@
3939
import java.util.HashSet;
4040
import java.util.Set;
4141

42+
import static org.apache.jena.ext.com.google.common.io.Files.createTempDir;
43+
4244
/**
4345
* Handles Git related functionality
4446
*/
@@ -63,31 +65,43 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
6365
/**
6466
* Gets a repository, cloning into a local directory or
6567
* @param gitDetails The details of the Git repository
68+
* @param reuseDir Whether the cached repository can be used
6669
* @returns The git object for the repository
6770
*/
68-
public Git getRepository(GitDetails gitDetails)
71+
public Git getRepository(GitDetails gitDetails, boolean reuseDir)
6972
throws GitAPIException {
7073
Git repo = null;
7174
try {
72-
// Base dir from configuration, name from hash of repository URL
73-
File baseDir = new File(gitStorage.toString());
74-
String baseName = DigestUtils.shaHex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
75+
if (reuseDir) {
76+
// Base dir from configuration, name from hash of repository URL
77+
File baseDir = new File(gitStorage.toString());
78+
String baseName = DigestUtils.shaHex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
7579

76-
// Check if folder already exists
77-
File repoDir = new File(baseDir, baseName);
78-
if (repoDir.exists() && repoDir.isDirectory()) {
80+
// Check if folder already exists
81+
File repoDir = new File(baseDir, baseName);
82+
if (repoDir.exists() && repoDir.isDirectory()) {
7983
repo = Git.open(repoDir);
8084
repo.fetch().call();
81-
} else {
82-
// Create a folder and clone repository into it
83-
if (repoDir.mkdir()) {
84-
repo = Git.cloneRepository()
85-
.setCloneSubmodules(cloneSubmodules)
86-
.setURI(gitDetails.getRepoUrl())
87-
.setDirectory(repoDir)
88-
.setCloneAllBranches(true)
89-
.call();
85+
} else {
86+
// Create a folder and clone repository into it
87+
if (repoDir.mkdir()) {
88+
repo = Git.cloneRepository()
89+
.setCloneSubmodules(cloneSubmodules)
90+
.setURI(gitDetails.getRepoUrl())
91+
.setDirectory(repoDir)
92+
.setCloneAllBranches(true)
93+
.call();
94+
}
9095
}
96+
} else {
97+
// Another thread is already using the existing folder
98+
// Must create another temporary one
99+
repo = Git.cloneRepository()
100+
.setCloneSubmodules(cloneSubmodules)
101+
.setURI(gitDetails.getRepoUrl())
102+
.setDirectory(createTempDir())
103+
.setCloneAllBranches(true)
104+
.call();
91105
}
92106

93107
// Checkout the specific branch or commit ID

0 commit comments

Comments
 (0)