|
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; |
27 | 26 | import org.eclipse.jgit.lib.ObjectId;
|
28 | 27 | import org.eclipse.jgit.lib.PersonIdent;
|
29 | 28 | import org.eclipse.jgit.revwalk.RevCommit;
|
@@ -72,60 +71,47 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
|
72 | 71 | */
|
73 | 72 | public Git getRepository(GitDetails gitDetails, boolean reuseDir)
|
74 | 73 | throws GitAPIException, IOException {
|
75 |
| - Git repo = null; |
76 |
| - while (repo == null) { |
77 |
| - try { |
78 |
| - if (reuseDir) { |
79 |
| - // Base dir from configuration, name from hash of repository URL |
80 |
| - String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl())); |
81 |
| - |
82 |
| - // Check if folder already exists |
83 |
| - Path repoDir = gitStorage.resolve(baseName); |
84 |
| - if (Files.isReadable(repoDir) && Files.isDirectory(repoDir)) { |
85 |
| - repo = Git.open(repoDir.toFile()); |
86 |
| - repo.fetch().call(); |
87 |
| - } else { |
88 |
| - // Create a folder and clone repository into it |
89 |
| - Files.createDirectory(repoDir); |
90 |
| - repo = Git.cloneRepository() |
91 |
| - .setCloneSubmodules(cloneSubmodules) |
92 |
| - .setURI(gitDetails.getRepoUrl()) |
93 |
| - .setDirectory(repoDir.toFile()) |
94 |
| - .setCloneAllBranches(true) |
95 |
| - .call(); |
96 |
| - } |
97 |
| - } else { |
98 |
| - // Another thread is already using the existing folder |
99 |
| - // Must create another temporary one |
100 |
| - repo = Git.cloneRepository() |
101 |
| - .setCloneSubmodules(cloneSubmodules) |
102 |
| - .setURI(gitDetails.getRepoUrl()) |
103 |
| - .setDirectory(createTempDir()) |
104 |
| - .setCloneAllBranches(true) |
105 |
| - .call(); |
106 |
| - } |
| 74 | + Git repo; |
| 75 | + if (reuseDir) { |
| 76 | + // Base dir from configuration, name from hash of repository URL |
| 77 | + String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl())); |
| 78 | + |
| 79 | + // Check if folder already exists |
| 80 | + Path repoDir = gitStorage.resolve(baseName); |
| 81 | + if (Files.isReadable(repoDir) && Files.isDirectory(repoDir)) { |
| 82 | + repo = Git.open(repoDir.toFile()); |
| 83 | + repo.fetch().call(); |
| 84 | + } else { |
| 85 | + // Create a folder and clone repository into it |
| 86 | + Files.createDirectory(repoDir); |
| 87 | + repo = Git.cloneRepository() |
| 88 | + .setCloneSubmodules(cloneSubmodules) |
| 89 | + .setURI(gitDetails.getRepoUrl()) |
| 90 | + .setDirectory(repoDir.toFile()) |
| 91 | + .setCloneAllBranches(true) |
| 92 | + .call(); |
| 93 | + } |
| 94 | + } else { |
| 95 | + // Another thread is already using the existing folder |
| 96 | + // Must create another temporary one |
| 97 | + repo = Git.cloneRepository() |
| 98 | + .setCloneSubmodules(cloneSubmodules) |
| 99 | + .setURI(gitDetails.getRepoUrl()) |
| 100 | + .setDirectory(createTempDir()) |
| 101 | + .setCloneAllBranches(true) |
| 102 | + .call(); |
| 103 | + } |
107 | 104 |
|
108 |
| - // Checkout the specific branch or commit ID |
109 |
| - if (repo != null) { |
110 |
| - // Create a new local branch if it does not exist and not a commit ID |
111 |
| - String branchOrCommitId = gitDetails.getBranch(); |
112 |
| - if (!ObjectId.isId(branchOrCommitId)) { |
113 |
| - branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId; |
114 |
| - } |
115 |
| - repo.checkout() |
116 |
| - .setName(branchOrCommitId) |
117 |
| - .call(); |
118 |
| - } |
119 |
| - } catch (RefNotFoundException ex) { |
120 |
| - // Attempt slashes in branch fix |
121 |
| - repo = null; |
122 |
| - GitDetails correctedForSlash = transferPathToBranch(gitDetails); |
123 |
| - if (correctedForSlash != null) { |
124 |
| - gitDetails = correctedForSlash; |
125 |
| - } else { |
126 |
| - throw ex; |
127 |
| - } |
| 105 | + // Checkout the specific branch or commit ID |
| 106 | + if (repo != null) { |
| 107 | + // Create a new local branch if it does not exist and not a commit ID |
| 108 | + String branchOrCommitId = gitDetails.getBranch(); |
| 109 | + if (!ObjectId.isId(branchOrCommitId)) { |
| 110 | + branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId; |
128 | 111 | }
|
| 112 | + repo.checkout() |
| 113 | + .setName(branchOrCommitId) |
| 114 | + .call(); |
129 | 115 | }
|
130 | 116 |
|
131 | 117 | return repo;
|
@@ -190,8 +176,9 @@ public GitDetails transferPathToBranch(GitDetails githubInfo) {
|
190 | 176 | if (firstSlash > 0) {
|
191 | 177 | branch += "/" + path.substring(0, firstSlash);
|
192 | 178 | path = path.substring(firstSlash + 1);
|
193 |
| - return new GitDetails(githubInfo.getRepoUrl(), branch, |
194 |
| - path); |
| 179 | + GitDetails newDetails = new GitDetails(githubInfo.getRepoUrl(), branch, path); |
| 180 | + newDetails.setPackedId(githubInfo.getPackedId()); |
| 181 | + return newDetails; |
195 | 182 | } else {
|
196 | 183 | return null;
|
197 | 184 | }
|
|
0 commit comments