|
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,59 +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 | | - GitDetails correctedForSlash = transferPathToBranch(gitDetails); |
122 | | - if (correctedForSlash != null) { |
123 | | - gitDetails = correctedForSlash; |
124 | | - } else { |
125 | | - throw ex; |
126 | | - } |
| 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; |
127 | 111 | } |
| 112 | + repo.checkout() |
| 113 | + .setName(branchOrCommitId) |
| 114 | + .call(); |
128 | 115 | } |
129 | 116 |
|
130 | 117 | return repo; |
@@ -189,8 +176,9 @@ public GitDetails transferPathToBranch(GitDetails githubInfo) { |
189 | 176 | if (firstSlash > 0) { |
190 | 177 | branch += "/" + path.substring(0, firstSlash); |
191 | 178 | path = path.substring(firstSlash + 1); |
192 | | - return new GitDetails(githubInfo.getRepoUrl(), branch, |
193 | | - path); |
| 179 | + GitDetails newDetails = new GitDetails(githubInfo.getRepoUrl(), branch, path); |
| 180 | + newDetails.setPackedId(githubInfo.getPackedId()); |
| 181 | + return newDetails; |
194 | 182 | } else { |
195 | 183 | return null; |
196 | 184 | } |
|
0 commit comments