Skip to content

Commit f9f9287

Browse files
author
Charles Overbeck
committed
Support for Git tags #191
If we get a RefNotFoundException, it might be a tag.
1 parent 614b1ca commit f9f9287

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

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

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.commonwl.view.researchobject.HashableAgent;
2424
import org.eclipse.jgit.api.Git;
2525
import org.eclipse.jgit.api.errors.GitAPIException;
26+
import org.eclipse.jgit.api.errors.RefNotFoundException;
2627
import org.eclipse.jgit.lib.ObjectId;
2728
import org.eclipse.jgit.lib.PersonIdent;
2829
import org.eclipse.jgit.revwalk.RevCommit;
@@ -67,7 +68,7 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
6768
* Gets a repository, cloning into a local directory or
6869
* @param gitDetails The details of the Git repository
6970
* @param reuseDir Whether the cached repository can be used
70-
* @returns The git object for the repository
71+
* @return The git object for the repository
7172
*/
7273
public Git getRepository(GitDetails gitDetails, boolean reuseDir)
7374
throws GitAPIException, IOException {
@@ -106,12 +107,33 @@ public Git getRepository(GitDetails gitDetails, boolean reuseDir)
106107
if (repo != null) {
107108
// Create a new local branch if it does not exist and not a commit ID
108109
String branchOrCommitId = gitDetails.getBranch();
109-
if (!ObjectId.isId(branchOrCommitId)) {
110+
final boolean isId = ObjectId.isId(branchOrCommitId);
111+
if (!isId) {
110112
branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId;
111113
}
112-
repo.checkout()
113-
.setName(branchOrCommitId)
114-
.call();
114+
try {
115+
repo.checkout()
116+
.setName(branchOrCommitId)
117+
.call();
118+
}
119+
catch (Exception ex) {
120+
// Maybe it was a tag
121+
if (!isId && ex instanceof RefNotFoundException) {
122+
final String tag = gitDetails.getBranch();
123+
try {
124+
repo.checkout()
125+
.setName(tag)
126+
.call();
127+
}
128+
catch (Exception ex2) {
129+
// Throw the first exception, to keep the same behavior as before.
130+
throw ex;
131+
}
132+
}
133+
else {
134+
throw ex;
135+
}
136+
}
115137
}
116138

117139
return repo;

0 commit comments

Comments
 (0)