39
39
import java .util .HashSet ;
40
40
import java .util .Set ;
41
41
42
+ import static org .apache .jena .ext .com .google .common .io .Files .createTempDir ;
43
+
42
44
/**
43
45
* Handles Git related functionality
44
46
*/
@@ -63,31 +65,43 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
63
65
/**
64
66
* Gets a repository, cloning into a local directory or
65
67
* @param gitDetails The details of the Git repository
68
+ * @param reuseDir Whether the cached repository can be used
66
69
* @returns The git object for the repository
67
70
*/
68
- public Git getRepository (GitDetails gitDetails )
71
+ public Git getRepository (GitDetails gitDetails , boolean reuseDir )
69
72
throws GitAPIException {
70
73
Git repo = null ;
71
74
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 ()));
75
79
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 ()) {
79
83
repo = Git .open (repoDir );
80
84
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
+ }
90
95
}
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 ();
91
105
}
92
106
93
107
// Checkout the specific branch or commit ID
0 commit comments