Skip to content

Commit 4333dbd

Browse files
committed
Caching for repositories supporting commit sha
1 parent 1e1b488 commit 4333dbd

File tree

3 files changed

+48
-11
lines changed

3 files changed

+48
-11
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -170,7 +170,7 @@ public String getRawUrl() {
170170
* @param url The URL to be normalised
171171
* @return The normalised URL
172172
*/
173-
private String normaliseUrl(String url) {
173+
public static String normaliseUrl(String url) {
174174
return url.replace("http://", "")
175175
.replace("https://", "")
176176
.replace("ssh://", "")

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

Lines changed: 45 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@
1919

2020
package org.commonwl.view.git;
2121

22+
import org.apache.commons.codec.digest.DigestUtils;
2223
import org.eclipse.jgit.api.Git;
2324
import org.eclipse.jgit.api.errors.GitAPIException;
25+
import org.slf4j.Logger;
26+
import org.slf4j.LoggerFactory;
2427
import org.springframework.beans.factory.annotation.Autowired;
2528
import org.springframework.beans.factory.annotation.Value;
2629
import org.springframework.stereotype.Service;
2730

31+
import java.io.File;
32+
import java.io.IOException;
2833
import java.nio.file.Path;
2934

30-
import static java.util.Collections.singleton;
31-
import static org.apache.jena.ext.com.google.common.io.Files.createTempDir;
32-
3335
/**
3436
* Handles Git related functionality
3537
*/
3638
@Service
3739
public class GitService {
3840

41+
private final Logger logger = LoggerFactory.getLogger(this.getClass());
42+
3943
// Location to check out git repositories into
4044
private Path gitStorage;
4145

@@ -55,13 +59,44 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
5559
*/
5660
public Git cloneRepository(GitDetails gitDetails)
5761
throws GitAPIException {
58-
return Git.cloneRepository()
59-
.setCloneSubmodules(cloneSubmodules)
60-
.setURI(gitDetails.getRepoUrl())
61-
.setDirectory(createTempDir())
62-
.setBranchesToClone(singleton("refs/heads/" + gitDetails.getBranch()))
63-
.setBranch("refs/heads/" + gitDetails.getBranch())
64-
.call();
62+
Git repo = null;
63+
try {
64+
// Get base directory based on configuration
65+
File baseDir = new File(gitStorage.toString());
66+
String baseName = DigestUtils.shaHex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
67+
68+
// Check if folder already exists
69+
File repoDir = new File(baseDir, baseName);
70+
if (repoDir.exists() && repoDir.isDirectory()) {
71+
repo = Git.open(repoDir);
72+
repo.fetch().call();
73+
} else {
74+
// Create a folder and clone repository into it
75+
if (repoDir.mkdir()) {
76+
repo = Git.cloneRepository()
77+
.setCloneSubmodules(cloneSubmodules)
78+
.setURI(gitDetails.getRepoUrl())
79+
.setDirectory(repoDir)
80+
.setCloneAllBranches(true)
81+
.call();
82+
}
83+
}
84+
85+
// Checkout the specific branch or commit ID
86+
if (repo != null) {
87+
repo.checkout()
88+
.setName(gitDetails.getBranch())
89+
.call();
90+
if (repo.getRepository().getFullBranch() != null) {
91+
repo.pull().call();
92+
}
93+
}
94+
} catch (IOException ex) {
95+
logger.error("Could not open existing Git repository for '"
96+
+ gitDetails.getRepoUrl() + "'", ex);
97+
}
98+
99+
return repo;
65100
}
66101

67102

src/main/java/org/commonwl/view/workflow/WorkflowController.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public ModelAndView newWorkflowFromGithubURL(@Valid WorkflowForm workflowForm, B
122122
workflow = workflowService.createQueuedWorkflow(gitInfo).getTempRepresentation();
123123
} catch (GitAPIException ex) {
124124
bindingResult.rejectValue("url", "git.retrievalError");
125+
logger.error("Github API Error", ex);
125126
return new ModelAndView("index");
126127
} catch (WorkflowNotFoundException ex) {
127128
bindingResult.rejectValue("url", "git.pathTraversal");
@@ -451,6 +452,7 @@ private ModelAndView getWorkflow(GitDetails gitDetails, RedirectAttributes redir
451452
queued = workflowService.createQueuedWorkflow(gitDetails);
452453
} catch (GitAPIException ex) {
453454
errors.rejectValue("url", "git.retrievalError", "The workflow could not be retrieved from the Git repository using the details given");
455+
logger.error("Github API Error", ex);
454456
} catch (WorkflowNotFoundException ex) {
455457
errors.rejectValue("url", "git.pathTraversal", "The path given did not resolve to a location within the repository");
456458
} catch (IOException ex) {

0 commit comments

Comments
 (0)