diff --git a/src/main/java/org/commonwl/view/git/GitService.java b/src/main/java/org/commonwl/view/git/GitService.java index 510cdf71..73397dae 100644 --- a/src/main/java/org/commonwl/view/git/GitService.java +++ b/src/main/java/org/commonwl/view/git/GitService.java @@ -23,7 +23,6 @@ import org.commonwl.view.researchobject.HashableAgent; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; -import org.eclipse.jgit.api.errors.RefNotFoundException; import org.eclipse.jgit.lib.ObjectId; import org.eclipse.jgit.lib.PersonIdent; import org.eclipse.jgit.revwalk.RevCommit; @@ -72,59 +71,47 @@ public GitService(@Value("${gitStorage}") Path gitStorage, */ public Git getRepository(GitDetails gitDetails, boolean reuseDir) throws GitAPIException, IOException { - Git repo = null; - while (repo == null) { - try { - if (reuseDir) { - // Base dir from configuration, name from hash of repository URL - String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl())); - - // Check if folder already exists - Path repoDir = gitStorage.resolve(baseName); - if (Files.isReadable(repoDir) && Files.isDirectory(repoDir)) { - repo = Git.open(repoDir.toFile()); - repo.fetch().call(); - } else { - // Create a folder and clone repository into it - Files.createDirectory(repoDir); - repo = Git.cloneRepository() - .setCloneSubmodules(cloneSubmodules) - .setURI(gitDetails.getRepoUrl()) - .setDirectory(repoDir.toFile()) - .setCloneAllBranches(true) - .call(); - } - } else { - // Another thread is already using the existing folder - // Must create another temporary one - repo = Git.cloneRepository() - .setCloneSubmodules(cloneSubmodules) - .setURI(gitDetails.getRepoUrl()) - .setDirectory(createTempDir()) - .setCloneAllBranches(true) - .call(); - } + Git repo; + if (reuseDir) { + // Base dir from configuration, name from hash of repository URL + String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl())); + + // Check if folder already exists + Path repoDir = gitStorage.resolve(baseName); + if (Files.isReadable(repoDir) && Files.isDirectory(repoDir)) { + repo = Git.open(repoDir.toFile()); + repo.fetch().call(); + } else { + // Create a folder and clone repository into it + Files.createDirectory(repoDir); + repo = Git.cloneRepository() + .setCloneSubmodules(cloneSubmodules) + .setURI(gitDetails.getRepoUrl()) + .setDirectory(repoDir.toFile()) + .setCloneAllBranches(true) + .call(); + } + } else { + // Another thread is already using the existing folder + // Must create another temporary one + repo = Git.cloneRepository() + .setCloneSubmodules(cloneSubmodules) + .setURI(gitDetails.getRepoUrl()) + .setDirectory(createTempDir()) + .setCloneAllBranches(true) + .call(); + } - // Checkout the specific branch or commit ID - if (repo != null) { - // Create a new local branch if it does not exist and not a commit ID - String branchOrCommitId = gitDetails.getBranch(); - if (!ObjectId.isId(branchOrCommitId)) { - branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId; - } - repo.checkout() - .setName(branchOrCommitId) - .call(); - } - } catch (RefNotFoundException ex) { - // Attempt slashes in branch fix - GitDetails correctedForSlash = transferPathToBranch(gitDetails); - if (correctedForSlash != null) { - gitDetails = correctedForSlash; - } else { - throw ex; - } + // Checkout the specific branch or commit ID + if (repo != null) { + // Create a new local branch if it does not exist and not a commit ID + String branchOrCommitId = gitDetails.getBranch(); + if (!ObjectId.isId(branchOrCommitId)) { + branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId; } + repo.checkout() + .setName(branchOrCommitId) + .call(); } return repo; @@ -189,8 +176,9 @@ public GitDetails transferPathToBranch(GitDetails githubInfo) { if (firstSlash > 0) { branch += "/" + path.substring(0, firstSlash); path = path.substring(firstSlash + 1); - return new GitDetails(githubInfo.getRepoUrl(), branch, - path); + GitDetails newDetails = new GitDetails(githubInfo.getRepoUrl(), branch, path); + newDetails.setPackedId(githubInfo.getPackedId()); + return newDetails; } else { return null; } diff --git a/src/main/java/org/commonwl/view/workflow/WorkflowService.java b/src/main/java/org/commonwl/view/workflow/WorkflowService.java index 52141f15..1a8f1f27 100644 --- a/src/main/java/org/commonwl/view/workflow/WorkflowService.java +++ b/src/main/java/org/commonwl/view/workflow/WorkflowService.java @@ -19,16 +19,6 @@ package org.commonwl.view.workflow; -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; -import java.util.ArrayList; -import java.util.Calendar; -import java.util.Date; -import java.util.List; - import org.commonwl.view.cwl.CWLService; import org.commonwl.view.cwl.CWLToolRunner; import org.commonwl.view.cwl.CWLToolStatus; @@ -40,6 +30,7 @@ import org.commonwl.view.researchobject.ROBundleNotFoundException; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; +import org.eclipse.jgit.api.errors.RefNotFoundException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -48,6 +39,16 @@ import org.springframework.data.domain.Pageable; import org.springframework.stereotype.Service; +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Calendar; +import java.util.Date; +import java.util.List; + @Service public class WorkflowService { @@ -193,9 +194,23 @@ public Workflow getWorkflow(GitDetails gitInfo) { */ public List getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException { List workflowsInDir = new ArrayList<>(); - boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); try { - Git repo = gitService.getRepository(gitInfo, safeToAccess); + boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); + Git repo = null; + while (repo == null) { + try { + repo = gitService.getRepository(gitInfo, safeToAccess); + } catch (RefNotFoundException ex) { + // Attempt slashes in branch fix + GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo); + if (correctedForSlash != null) { + gitInfo = correctedForSlash; + } else { + throw ex; + } + } + } + Path localPath = repo.getRepository().getWorkTree().toPath(); Path pathToDirectory = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath(); Path root = Paths.get("/").toAbsolutePath(); @@ -267,9 +282,22 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo) throws GitAPIException, WorkflowNotFoundException, IOException { QueuedWorkflow queuedWorkflow; - boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); try { - Git repo = gitService.getRepository(gitInfo, safeToAccess); + boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl()); + Git repo = null; + while (repo == null) { + try { + repo = gitService.getRepository(gitInfo, safeToAccess); + } catch (RefNotFoundException ex) { + // Attempt slashes in branch fix + GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo); + if (correctedForSlash != null) { + gitInfo = correctedForSlash; + } else { + throw ex; + } + } + } File localPath = repo.getRepository().getWorkTree(); String latestCommit = gitService.getCurrentCommitID(repo);