Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 42 additions & 54 deletions src/main/java/org/commonwl/view/git/GitService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down
56 changes: 42 additions & 14 deletions src/main/java/org/commonwl/view/workflow/WorkflowService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;
Expand All @@ -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 {

Expand Down Expand Up @@ -193,9 +194,23 @@ public Workflow getWorkflow(GitDetails gitInfo) {
*/
public List<WorkflowOverview> getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException {
List<WorkflowOverview> 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();
Expand Down Expand Up @@ -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);

Expand Down