Merge "Refactor MergeOpRepoManager#openRepo to return the OpenRepo"

This commit is contained in:
David Pursehouse
2016-07-25 00:26:14 +00:00
committed by Gerrit Code Review
3 changed files with 21 additions and 19 deletions

View File

@@ -64,12 +64,12 @@ public class GitModules {
this.submissionId = orm.getSubmissionId(); this.submissionId = orm.getSubmissionId();
Project.NameKey project = branch.getParentKey(); Project.NameKey project = branch.getParentKey();
logDebug("Loading .gitmodules of {} for project {}", branch, project); logDebug("Loading .gitmodules of {} for project {}", branch, project);
OpenRepo or;
try { try {
orm.openRepo(project, false); or = orm.openRepo(project, false);
} catch (NoSuchProjectException e) { } catch (NoSuchProjectException e) {
throw new IOException(e); throw new IOException(e);
} }
OpenRepo or = orm.getRepo(project);
ObjectId id = or.repo.resolve(branch.get()); ObjectId id = or.repo.resolve(branch.get());
if (id == null) { if (id == null) {

View File

@@ -185,16 +185,17 @@ public class MergeOpRepoManager implements AutoCloseable {
return or; return or;
} }
public void openRepo(Project.NameKey project, boolean abortIfOpen) public OpenRepo openRepo(Project.NameKey project, boolean abortIfOpen)
throws NoSuchProjectException, IOException { throws NoSuchProjectException, IOException {
if (abortIfOpen) { if (abortIfOpen) {
checkState(!openRepos.containsKey(project), checkState(!openRepos.containsKey(project),
"repo already opened: %s", project); "repo already opened: %s", project);
} else {
if (openRepos.containsKey(project)) {
return;
}
} }
if (openRepos.containsKey(project)) {
return openRepos.get(project);
}
ProjectState projectState = projectCache.get(project); ProjectState projectState = projectCache.get(project);
if (projectState == null) { if (projectState == null) {
throw new NoSuchProjectException(project); throw new NoSuchProjectException(project);
@@ -203,6 +204,7 @@ public class MergeOpRepoManager implements AutoCloseable {
OpenRepo or = OpenRepo or =
new OpenRepo(repoManager.openRepository(project), projectState); new OpenRepo(repoManager.openRepository(project), projectState);
openRepos.put(project, or); openRepos.put(project, or);
return or;
} catch (RepositoryNotFoundException e) { } catch (RepositoryNotFoundException e) {
throw new NoSuchProjectException(project); throw new NoSuchProjectException(project);
} }

View File

@@ -227,14 +227,15 @@ public class SubmoduleOp {
if (r.matchSource(src.get())) { if (r.matchSource(src.get())) {
if (r.getDestination() == null) { if (r.getDestination() == null) {
// no need to care for wildcard, as we matched already // no need to care for wildcard, as we matched already
OpenRepo or;
try { try {
orm.openRepo(s.getProject(), false); or = orm.openRepo(s.getProject(), false);
} catch (NoSuchProjectException e) { } catch (NoSuchProjectException e) {
// A project listed a non existent project to be allowed // A project listed a non existent project to be allowed
// to subscribe to it. Allow this for now. // to subscribe to it. Allow this for now.
continue; continue;
} }
OpenRepo or = orm.getRepo(s.getProject());
for (Ref ref : or.repo.getRefDatabase().getRefs( for (Ref ref : or.repo.getRefDatabase().getRefs(
RefNames.REFS_HEADS).values()) { RefNames.REFS_HEADS).values()) {
ret.add(new Branch.NameKey(s.getProject(), ref.getName())); ret.add(new Branch.NameKey(s.getProject(), ref.getName()));
@@ -269,8 +270,7 @@ public class SubmoduleOp {
for (Branch.NameKey targetBranch : branches) { for (Branch.NameKey targetBranch : branches) {
Project.NameKey targetProject = targetBranch.getParentKey(); Project.NameKey targetProject = targetBranch.getParentKey();
try { try {
orm.openRepo(targetProject, false); OpenRepo or = orm.openRepo(targetProject, false);
OpenRepo or = orm.getRepo(targetProject);
ObjectId id = or.repo.resolve(targetBranch.get()); ObjectId id = or.repo.resolve(targetBranch.get());
if (id == null) { if (id == null) {
logDebug("The branch " + targetBranch + " doesn't exist."); logDebug("The branch " + targetBranch + " doesn't exist.");
@@ -307,9 +307,9 @@ public class SubmoduleOp {
if (dst.containsKey(project)) { if (dst.containsKey(project)) {
superProjects.add(project); superProjects.add(project);
// get a new BatchUpdate for the super project // get a new BatchUpdate for the super project
orm.openRepo(project, false); OpenRepo or = orm.openRepo(project, false);
for (Branch.NameKey branch : dst.get(project)) { for (Branch.NameKey branch : dst.get(project)) {
addOp(orm.getRepo(project).getUpdate(), branch); addOp(or.getUpdate(), branch);
} }
} }
} }
@@ -325,12 +325,12 @@ public class SubmoduleOp {
*/ */
public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber) public CodeReviewCommit composeGitlinksCommit(final Branch.NameKey subscriber)
throws IOException, SubmoduleException { throws IOException, SubmoduleException {
OpenRepo or;
try { try {
orm.openRepo(subscriber.getParentKey(), false); or = orm.openRepo(subscriber.getParentKey(), false);
} catch (NoSuchProjectException | IOException e) { } catch (NoSuchProjectException | IOException e) {
throw new SubmoduleException("Cannot access superproject", e); throw new SubmoduleException("Cannot access superproject", e);
} }
OpenRepo or = orm.getRepo(subscriber.getParentKey());
CodeReviewCommit currentCommit; CodeReviewCommit currentCommit;
Ref r = or.repo.exactRef(subscriber.get()); Ref r = or.repo.exactRef(subscriber.get());
@@ -381,12 +381,12 @@ public class SubmoduleOp {
public CodeReviewCommit composeGitlinksCommit( public CodeReviewCommit composeGitlinksCommit(
final Branch.NameKey subscriber, CodeReviewCommit currentCommit) final Branch.NameKey subscriber, CodeReviewCommit currentCommit)
throws IOException, SubmoduleException { throws IOException, SubmoduleException {
OpenRepo or;
try { try {
orm.openRepo(subscriber.getParentKey(), false); or = orm.openRepo(subscriber.getParentKey(), false);
} catch (NoSuchProjectException | IOException e) { } catch (NoSuchProjectException | IOException e) {
throw new SubmoduleException("Cannot access superproject", e); throw new SubmoduleException("Cannot access superproject", e);
} }
OpenRepo or = orm.getRepo(subscriber.getParentKey());
StringBuilder msgbuf = new StringBuilder(""); StringBuilder msgbuf = new StringBuilder("");
DirCache dc = readTree(or.rw, currentCommit); DirCache dc = readTree(or.rw, currentCommit);
@@ -420,12 +420,12 @@ public class SubmoduleOp {
private RevCommit updateSubmodule(DirCache dc, DirCacheEditor ed, private RevCommit updateSubmodule(DirCache dc, DirCacheEditor ed,
StringBuilder msgbuf, final SubmoduleSubscription s) StringBuilder msgbuf, final SubmoduleSubscription s)
throws SubmoduleException, IOException { throws SubmoduleException, IOException {
OpenRepo subOr;
try { try {
orm.openRepo(s.getSubmodule().getParentKey(), false); subOr = orm.openRepo(s.getSubmodule().getParentKey(), false);
} catch (NoSuchProjectException | IOException e) { } catch (NoSuchProjectException | IOException e) {
throw new SubmoduleException("Cannot access submodule", e); throw new SubmoduleException("Cannot access submodule", e);
} }
OpenRepo subOr = orm.getRepo(s.getSubmodule().getParentKey());
DirCacheEntry dce = dc.getEntry(s.getPath()); DirCacheEntry dce = dc.getEntry(s.getPath());
RevCommit oldCommit = null; RevCommit oldCommit = null;