23
23
import org .commonwl .view .researchobject .HashableAgent ;
24
24
import org .eclipse .jgit .api .Git ;
25
25
import org .eclipse .jgit .api .errors .GitAPIException ;
26
+ import org .eclipse .jgit .api .errors .RefNotFoundException ;
26
27
import org .eclipse .jgit .lib .ObjectId ;
27
28
import org .eclipse .jgit .lib .PersonIdent ;
28
29
import org .eclipse .jgit .revwalk .RevCommit ;
32
33
import org .springframework .beans .factory .annotation .Value ;
33
34
import org .springframework .stereotype .Service ;
34
35
36
+ import java .io .File ;
35
37
import java .io .IOException ;
36
38
import java .net .URI ;
37
39
import java .net .URISyntaxException ;
@@ -67,7 +69,7 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
67
69
* Gets a repository, cloning into a local directory or
68
70
* @param gitDetails The details of the Git repository
69
71
* @param reuseDir Whether the cached repository can be used
70
- * @returns The git object for the repository
72
+ * @return The git object for the repository
71
73
*/
72
74
public Git getRepository (GitDetails gitDetails , boolean reuseDir )
73
75
throws GitAPIException , IOException {
@@ -84,34 +86,45 @@ public Git getRepository(GitDetails gitDetails, boolean reuseDir)
84
86
} else {
85
87
// Create a folder and clone repository into it
86
88
Files .createDirectory (repoDir );
87
- repo = Git .cloneRepository ()
88
- .setCloneSubmodules (cloneSubmodules )
89
- .setURI (gitDetails .getRepoUrl ())
90
- .setDirectory (repoDir .toFile ())
91
- .setCloneAllBranches (true )
92
- .call ();
89
+ repo = cloneRepo (gitDetails .getRepoUrl (), repoDir .toFile ());
93
90
}
94
91
} else {
95
92
// Another thread is already using the existing folder
96
93
// Must create another temporary one
97
- repo = Git .cloneRepository ()
98
- .setCloneSubmodules (cloneSubmodules )
99
- .setURI (gitDetails .getRepoUrl ())
100
- .setDirectory (createTempDir ())
101
- .setCloneAllBranches (true )
102
- .call ();
94
+ repo = cloneRepo (gitDetails .getRepoUrl (), createTempDir ());
103
95
}
104
96
105
97
// Checkout the specific branch or commit ID
106
98
if (repo != null ) {
107
99
// Create a new local branch if it does not exist and not a commit ID
108
100
String branchOrCommitId = gitDetails .getBranch ();
109
- if (!ObjectId .isId (branchOrCommitId )) {
101
+ final boolean isId = ObjectId .isId (branchOrCommitId );
102
+ if (!isId ) {
110
103
branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId ;
111
104
}
112
- repo .checkout ()
113
- .setName (branchOrCommitId )
114
- .call ();
105
+ try {
106
+ repo .checkout ()
107
+ .setName (branchOrCommitId )
108
+ .call ();
109
+ }
110
+ catch (Exception ex ) {
111
+ // Maybe it was a tag
112
+ if (!isId && ex instanceof RefNotFoundException ) {
113
+ final String tag = gitDetails .getBranch ();
114
+ try {
115
+ repo .checkout ()
116
+ .setName (tag )
117
+ .call ();
118
+ }
119
+ catch (Exception ex2 ) {
120
+ // Throw the first exception, to keep the same behavior as before.
121
+ throw ex ;
122
+ }
123
+ }
124
+ else {
125
+ throw ex ;
126
+ }
127
+ }
115
128
}
116
129
117
130
return repo ;
@@ -184,4 +197,19 @@ public GitDetails transferPathToBranch(GitDetails githubInfo) {
184
197
}
185
198
}
186
199
200
+ /**
201
+ * Clones a Git repository
202
+ * @param repoUrl the url of the Git repository
203
+ * @param directory the directory to clone the repo into
204
+ * @return a Git instance
205
+ * @throws GitAPIException if any error occurs cloning the repo
206
+ */
207
+ protected Git cloneRepo (String repoUrl , File directory ) throws GitAPIException {
208
+ return Git .cloneRepository ()
209
+ .setCloneSubmodules (cloneSubmodules )
210
+ .setURI (repoUrl )
211
+ .setDirectory (directory )
212
+ .setCloneAllBranches (true )
213
+ .call ();
214
+ }
187
215
}
0 commit comments