Skip to content

Commit 8bf8b14

Browse files
committed
Move slash in branch correction to GitService
1 parent c6b2918 commit 8bf8b14

File tree

2 files changed

+81
-100
lines changed

2 files changed

+81
-100
lines changed

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

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
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;
2627
import org.eclipse.jgit.lib.ObjectId;
2728
import org.eclipse.jgit.lib.PersonIdent;
2829
import org.eclipse.jgit.revwalk.RevCommit;
@@ -72,53 +73,63 @@ public GitService(@Value("${gitStorage}") Path gitStorage,
7273
public Git getRepository(GitDetails gitDetails, boolean reuseDir)
7374
throws GitAPIException {
7475
Git repo = null;
75-
try {
76-
if (reuseDir) {
77-
// Base dir from configuration, name from hash of repository URL
78-
File baseDir = new File(gitStorage.toString());
79-
String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
80-
81-
// Check if folder already exists
82-
File repoDir = new File(baseDir, baseName);
83-
if (repoDir.exists() && repoDir.isDirectory()) {
84-
repo = Git.open(repoDir);
85-
repo.fetch().call();
86-
} else {
87-
// Create a folder and clone repository into it
88-
if (repoDir.mkdir()) {
89-
repo = Git.cloneRepository()
90-
.setCloneSubmodules(cloneSubmodules)
91-
.setURI(gitDetails.getRepoUrl())
92-
.setDirectory(repoDir)
93-
.setCloneAllBranches(true)
94-
.call();
76+
while (repo == null) {
77+
try {
78+
if (reuseDir) {
79+
// Base dir from configuration, name from hash of repository URL
80+
File baseDir = new File(gitStorage.toString());
81+
String baseName = DigestUtils.sha1Hex(GitDetails.normaliseUrl(gitDetails.getRepoUrl()));
82+
83+
// Check if folder already exists
84+
File repoDir = new File(baseDir, baseName);
85+
if (repoDir.exists() && repoDir.isDirectory()) {
86+
repo = Git.open(repoDir);
87+
repo.fetch().call();
88+
} else {
89+
// Create a folder and clone repository into it
90+
if (repoDir.mkdir()) {
91+
repo = Git.cloneRepository()
92+
.setCloneSubmodules(cloneSubmodules)
93+
.setURI(gitDetails.getRepoUrl())
94+
.setDirectory(repoDir)
95+
.setCloneAllBranches(true)
96+
.call();
97+
}
9598
}
99+
} else {
100+
// Another thread is already using the existing folder
101+
// Must create another temporary one
102+
repo = Git.cloneRepository()
103+
.setCloneSubmodules(cloneSubmodules)
104+
.setURI(gitDetails.getRepoUrl())
105+
.setDirectory(createTempDir())
106+
.setCloneAllBranches(true)
107+
.call();
96108
}
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-
}
107109

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;
110+
// Checkout the specific branch or commit ID
111+
if (repo != null) {
112+
// Create a new local branch if it does not exist and not a commit ID
113+
String branchOrCommitId = gitDetails.getBranch();
114+
if (!ObjectId.isId(branchOrCommitId)) {
115+
branchOrCommitId = "refs/remotes/origin/" + branchOrCommitId;
116+
}
117+
repo.checkout()
118+
.setName(branchOrCommitId)
119+
.call();
120+
}
121+
} catch (RefNotFoundException ex) {
122+
// Attempt slashes in branch fix
123+
GitDetails correctedForSlash = transferPathToBranch(gitDetails);
124+
if (correctedForSlash != null) {
125+
gitDetails = correctedForSlash;
126+
} else {
127+
throw ex;
114128
}
115-
repo.checkout()
116-
.setName(branchOrCommitId)
117-
.call();
129+
} catch (IOException ex) {
130+
logger.error("Could not open existing Git repository for '"
131+
+ gitDetails.getRepoUrl() + "'", ex);
118132
}
119-
} catch (IOException ex) {
120-
logger.error("Could not open existing Git repository for '"
121-
+ gitDetails.getRepoUrl() + "'", ex);
122133
}
123134

124135
return repo;
@@ -168,4 +179,26 @@ public Set<HashableAgent> getAuthors(Git repo, String path) throws GitAPIExcepti
168179
return fileAuthors;
169180
}
170181

182+
/**
183+
* Transfers part of the path to the branch to fix / in branch names
184+
* @param githubInfo The current Github info possibly with
185+
* part of the branch name in the path
186+
* @return A potentially corrected set of Github details,
187+
* or null if there are no slashes in the path
188+
*/
189+
public GitDetails transferPathToBranch(GitDetails githubInfo) {
190+
String path = githubInfo.getPath();
191+
String branch = githubInfo.getBranch();
192+
193+
int firstSlash = path.indexOf("/");
194+
if (firstSlash > 0) {
195+
branch += "/" + path.substring(0, firstSlash);
196+
path = path.substring(firstSlash + 1);
197+
return new GitDetails(githubInfo.getRepoUrl(), branch,
198+
path);
199+
} else {
200+
return null;
201+
}
202+
}
203+
171204
}

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

Lines changed: 6 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
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;
3433
import org.slf4j.Logger;
3534
import org.slf4j.LoggerFactory;
3635
import org.springframework.beans.factory.annotation.Autowired;
@@ -133,7 +132,7 @@ public QueuedWorkflow getQueuedWorkflow(GitDetails githubInfo) {
133132
// Slash in branch fix
134133
boolean slashesInPath = true;
135134
while (queued == null && slashesInPath) {
136-
GitDetails correctedForSlash = transferPathToBranch(githubInfo);
135+
GitDetails correctedForSlash = gitService.transferPathToBranch(githubInfo);
137136
if (correctedForSlash != null) {
138137
githubInfo = correctedForSlash;
139138
queued = queuedWorkflowRepository.findByRetrievedFrom(githubInfo);
@@ -157,7 +156,7 @@ public Workflow getWorkflow(GitDetails gitInfo) {
157156
// Slash in branch fix
158157
boolean slashesInPath = true;
159158
while (workflow == null && slashesInPath) {
160-
GitDetails correctedForSlash = transferPathToBranch(gitInfo);
159+
GitDetails correctedForSlash = gitService.transferPathToBranch(gitInfo);
161160
if (correctedForSlash != null) {
162161
gitInfo = correctedForSlash;
163162
workflow = workflowRepository.findByRetrievedFrom(gitInfo);
@@ -194,24 +193,9 @@ public Workflow getWorkflow(GitDetails gitInfo) {
194193
*/
195194
public List<WorkflowOverview> getWorkflowsFromDirectory(GitDetails gitInfo) throws IOException, GitAPIException {
196195
List<WorkflowOverview> workflowsInDir = new ArrayList<>();
196+
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
197197
try {
198-
Git repo = null;
199-
while (repo == null) {
200-
try {
201-
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
202-
repo = gitService.getRepository(gitInfo, safeToAccess);
203-
} catch (RefNotFoundException ex) {
204-
// Attempt slashes in branch fix
205-
GitDetails correctedForSlash = transferPathToBranch(gitInfo);
206-
if (correctedForSlash != null) {
207-
gitSemaphore.release(gitInfo.getRepoUrl());
208-
gitInfo = correctedForSlash;
209-
} else {
210-
throw ex;
211-
}
212-
}
213-
}
214-
198+
Git repo = gitService.getRepository(gitInfo, safeToAccess);
215199
Path localPath = repo.getRepository().getWorkTree().toPath();
216200
Path pathToDirectory = localPath.resolve(gitInfo.getPath()).normalize().toAbsolutePath();
217201
Path root = Paths.get("/").toAbsolutePath();
@@ -283,23 +267,9 @@ public QueuedWorkflow createQueuedWorkflow(GitDetails gitInfo)
283267
throws GitAPIException, WorkflowNotFoundException, IOException {
284268
QueuedWorkflow queuedWorkflow;
285269

270+
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
286271
try {
287-
Git repo = null;
288-
while (repo == null) {
289-
try {
290-
boolean safeToAccess = gitSemaphore.acquire(gitInfo.getRepoUrl());
291-
repo = gitService.getRepository(gitInfo, safeToAccess);
292-
} catch (RefNotFoundException ex) {
293-
// Attempt slashes in branch fix
294-
GitDetails correctedForSlash = transferPathToBranch(gitInfo);
295-
if (correctedForSlash != null) {
296-
gitSemaphore.release(gitInfo.getRepoUrl());
297-
gitInfo = correctedForSlash;
298-
} else {
299-
throw ex;
300-
}
301-
}
302-
}
272+
Git repo = gitService.getRepository(gitInfo, safeToAccess);
303273
File localPath = repo.getRepository().getWorkTree();
304274
String latestCommit = gitService.getCurrentCommitID(repo);
305275

@@ -469,26 +439,4 @@ private boolean cacheExpired(Workflow workflow) {
469439
}
470440
return false;
471441
}
472-
473-
/**
474-
* Transfers part of the path to the branch to fix / in branch names
475-
* @param githubInfo The current Github info possibly with
476-
* part of the branch name in the path
477-
* @return A potentially corrected set of Github details,
478-
* or null if there are no slashes in the path
479-
*/
480-
private GitDetails transferPathToBranch(GitDetails githubInfo) {
481-
String path = githubInfo.getPath();
482-
String branch = githubInfo.getBranch();
483-
484-
int firstSlash = path.indexOf("/");
485-
if (firstSlash > 0) {
486-
branch += "/" + path.substring(0, firstSlash);
487-
path = path.substring(firstSlash + 1);
488-
return new GitDetails(githubInfo.getRepoUrl(), branch,
489-
path);
490-
} else {
491-
return null;
492-
}
493-
}
494442
}

0 commit comments

Comments
 (0)