Skip to content

Commit 74d43db

Browse files
authored
Merge branch 'master' into rdf-permalinks
2 parents 67ea196 + 7c99558 commit 74d43db

File tree

2 files changed

+75
-59
lines changed

2 files changed

+75
-59
lines changed

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

Lines changed: 42 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
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;
2726
import org.eclipse.jgit.lib.ObjectId;
2827
import org.eclipse.jgit.lib.PersonIdent;
2928
import org.eclipse.jgit.revwalk.RevCommit;
@@ -72,59 +71,47 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
7271
*/
7372
public Git getRepository(GitDetails gitDetails, boolean reuseDir)
7473
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+
}
107104

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;
127111
}
112+
repo.checkout()
113+
.setName(branchOrCommitId)
114+
.call();
128115
}
129116

130117
return repo;
@@ -189,8 +176,9 @@ public GitDetails transferPathToBranch(GitDetails githubInfo) {
189176
if (firstSlash > 0) {
190177
branch += "/" + path.substring(0, firstSlash);
191178
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;
194182
} else {
195183
return null;
196184
}

src/main/java/org/commonwl/view/workflow/WorkflowService.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.commonwl.view.researchobject.ROBundleNotFoundException;
3131
import org.eclipse.jgit.api.Git;
3232
import org.eclipse.jgit.api.errors.GitAPIException;
33+
import org.eclipse.jgit.api.errors.RefNotFoundException;
3334
import org.slf4j.Logger;
3435
import org.slf4j.LoggerFactory;
3536
import org.springframework.beans.factory.annotation.Autowired;
@@ -201,9 +202,23 @@ public Workflow getWorkflow(GitDetails gitInfo) {
201202
*/
202203
public List<WorkflowOverview> getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException {
203204
List<WorkflowOverview> workflowsInDir = new ArrayList<>();
204-
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
205205
try {
206-
Git repo = gitService.getRepository(gitInfo, safeToAccess);
206+
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
207+
Git repo = null;
208+
while (repo == null) {
209+
try {
210+
repo = gitService.getRepository(gitInfo, safeToAccess);
211+
} catch (RefNotFoundException ex) {
212+
// Attempt slashes in branch fix
213+
GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo);
214+
if (correctedForSlash != null) {
215+
gitInfo = correctedForSlash;
216+
} else {
217+
throw ex;
218+
}
219+
}
220+
}
221+
207222
Path localPath = repo.getRepository().getWorkTree().toPath();
208223
Path pathToDirectory = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath();
209224
Path root = Paths.get("/").toAbsolutePath();
@@ -275,10 +290,23 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
275290
throws GitAPIException, WorkflowNotFoundException, IOException {
276291
QueuedWorkflow queuedWorkflow;
277292

278-
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
279293
try {
280-
Git repo = gitService.getRepository(gitInfo, safeToAccess);
281-
Path localPath = repo.getRepository().getWorkTree().toPath();
294+
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
295+
Git repo = null;
296+
while (repo == null) {
297+
try {
298+
repo = gitService.getRepository(gitInfo, safeToAccess);
299+
} catch (RefNotFoundException ex) {
300+
// Attempt slashes in branch fix
301+
GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo);
302+
if (correctedForSlash != null) {
303+
gitInfo = correctedForSlash;
304+
} else {
305+
throw ex;
306+
}
307+
}
308+
}
309+
File localPath = repo.getRepository().getWorkTree();
282310
String latestCommit = gitService.getCurrentCommitID(repo);
283311

284312
Path workflowFile = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath();

0 commit comments

Comments
 (0)