use of com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo in project gerrit by GerritCodeReview.
the class LocalMergeSuperSetComputation method completeWithoutTopic.
@Override
public ChangeSet completeWithoutTopic(MergeOpRepoManager orm, ChangeSet changeSet, CurrentUser user) throws IOException {
Collection<ChangeData> visibleChanges = new ArrayList<>();
Collection<ChangeData> nonVisibleChanges = new ArrayList<>();
// For each target branch we run a separate rev walk to find open changes
// reachable from changes already in the merge super set.
ImmutableSet<BranchNameKey> branches = byBranch(Iterables.concat(changeSet.changes(), changeSet.nonVisibleChanges())).keySet();
ImmutableListMultimap<BranchNameKey, ChangeData> visibleChangesPerBranch = byBranch(changeSet.changes());
ImmutableListMultimap<BranchNameKey, ChangeData> nonVisibleChangesPerBranch = byBranch(changeSet.nonVisibleChanges());
for (BranchNameKey branchNameKey : branches) {
OpenRepo or = getRepo(orm, branchNameKey.project());
List<RevCommit> visibleCommits = new ArrayList<>();
List<RevCommit> nonVisibleCommits = new ArrayList<>();
for (ChangeData cd : visibleChangesPerBranch.get(branchNameKey)) {
if (submitType(cd) == SubmitType.CHERRY_PICK) {
visibleChanges.add(cd);
} else {
visibleCommits.add(or.rw.parseCommit(cd.currentPatchSet().commitId()));
}
}
for (ChangeData cd : nonVisibleChangesPerBranch.get(branchNameKey)) {
if (submitType(cd) == SubmitType.CHERRY_PICK) {
nonVisibleChanges.add(cd);
} else {
nonVisibleCommits.add(or.rw.parseCommit(cd.currentPatchSet().commitId()));
}
}
Set<String> visibleHashes = walkChangesByHashes(visibleCommits, Collections.emptySet(), or, branchNameKey);
Set<String> nonVisibleHashes = walkChangesByHashes(nonVisibleCommits, visibleHashes, or, branchNameKey);
ChangeSet partialSet = byCommitsOnBranchNotMerged(or, branchNameKey, visibleHashes, nonVisibleHashes, user);
Iterables.addAll(visibleChanges, partialSet.changes());
Iterables.addAll(nonVisibleChanges, partialSet.nonVisibleChanges());
}
return new ChangeSet(visibleChanges, nonVisibleChanges);
}
use of com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo in project gerrit by GerritCodeReview.
the class MergeOp method integrateIntoHistory.
private void integrateIntoHistory(ChangeSet cs, SubmissionExecutor submissionExecutor) throws RestApiException, UpdateException {
checkArgument(!cs.furtherHiddenChanges(), "cannot integrate hidden changes into history");
logger.atFine().log("Beginning merge attempt on %s", cs);
Map<BranchNameKey, BranchBatch> toSubmit = new HashMap<>();
ListMultimap<BranchNameKey, ChangeData> cbb;
try {
cbb = cs.changesByBranch();
} catch (StorageException e) {
throw new StorageException("Error reading changes to submit", e);
}
Set<BranchNameKey> branches = cbb.keySet();
for (BranchNameKey branch : branches) {
OpenRepo or = openRepo(branch.project());
if (or != null) {
toSubmit.put(branch, validateChangeList(or, cbb.get(branch)));
}
}
// Done checks that don't involve running submit strategies.
commitStatus.maybeFailVerbose();
try {
SubscriptionGraph subscriptionGraph = subscriptionGraphFactory.compute(branches, orm);
SubmoduleCommits submoduleCommits = submoduleCommitsFactory.create(orm);
UpdateOrderCalculator updateOrderCalculator = new UpdateOrderCalculator(subscriptionGraph);
List<SubmitStrategy> strategies = getSubmitStrategies(toSubmit, updateOrderCalculator, submoduleCommits, subscriptionGraph, dryrun);
this.allProjects = updateOrderCalculator.getProjectsInOrder();
List<BatchUpdate> batchUpdates = orm.batchUpdates(allProjects);
// Group batch updates by project
Map<Project.NameKey, BatchUpdate> batchUpdatesByProject = batchUpdates.stream().collect(Collectors.toMap(b -> b.getProject(), Function.identity()));
for (Map.Entry<Change.Id, ChangeData> entry : cs.changesById().entrySet()) {
Project.NameKey project = entry.getValue().project();
Change.Id changeId = entry.getKey();
ChangeData cd = entry.getValue();
batchUpdatesByProject.get(project).addOp(changeId, storeSubmitRequirementsOpFactory.create(cd.submitRequirementsIncludingLegacy().values(), cd));
}
try {
submissionExecutor.setAdditionalBatchUpdateListeners(ImmutableList.of(new SubmitStrategyListener(submitInput, strategies, commitStatus)));
submissionExecutor.execute(batchUpdates);
} finally {
// If the BatchUpdate fails it can be that merging some of the changes was actually
// successful. This is why we must to collect the updated changes also when an
// exception was thrown.
strategies.forEach(s -> updatedChanges.putAll(s.getUpdatedChanges()));
// Do not leave executed BatchUpdates in the OpenRepos
if (!dryrun) {
orm.resetUpdates(ImmutableSet.copyOf(this.allProjects));
}
}
} catch (NoSuchProjectException e) {
throw new ResourceNotFoundException(e.getMessage());
} catch (IOException e) {
throw new StorageException(e);
} catch (SubmoduleConflictException e) {
throw new IntegrationConflictException(e.getMessage(), e);
} catch (UpdateException e) {
if (e.getCause() instanceof LockFailureException) {
// as to be unnoticeable, assuming RetryHelper is retrying sufficiently.
throw e;
}
// inner IntegrationConflictException to a ResourceConflictException.
if (e.getCause() instanceof IntegrationConflictException) {
throw (IntegrationConflictException) e.getCause();
}
throw new MergeUpdateException(genericMergeError(cs), e);
}
}
use of com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo in project gerrit by GerritCodeReview.
the class SubmoduleCommits method updateSubmodule.
private RevCommit updateSubmodule(DirCache dc, DirCacheEditor ed, StringBuilder msgbuf, SubmoduleSubscription s) throws SubmoduleConflictException, IOException {
logger.atFine().log("Updating gitlink for %s", s);
OpenRepo subOr;
try {
subOr = orm.getRepo(s.getSubmodule().project());
} catch (NoSuchProjectException | IOException e) {
throw new StorageException("Cannot access submodule", e);
}
DirCacheEntry dce = dc.getEntry(s.getPath());
RevCommit oldCommit = null;
if (dce != null) {
if (!dce.getFileMode().equals(FileMode.GITLINK)) {
String errMsg = "Requested to update gitlink " + s.getPath() + " in " + s.getSubmodule().project().get() + " but entry " + "doesn't have gitlink file mode.";
throw new SubmoduleConflictException(errMsg);
}
// making things worse by updating the gitlink to something else.
try {
oldCommit = subOr.getCodeReviewRevWalk().parseCommit(dce.getObjectId());
} catch (IOException e) {
// Broken gitlink; sanity check failed. Warn and continue so the submit operation can
// proceed, it will just skip this gitlink update.
logger.atSevere().withCause(e).log("Failed to read commit %s", dce.getObjectId().name());
return null;
}
}
Optional<CodeReviewCommit> maybeNewCommit = branchTips.getTip(s.getSubmodule(), subOr);
if (!maybeNewCommit.isPresent()) {
// For whatever reason, this submodule was not updated as part of this submit batch, but the
// superproject is still subscribed to this branch. Re-read the ref to see if anything has
// changed since the last time the gitlink was updated, and roll that update into the same
// commit as all other submodule updates.
ed.add(new DeletePath(s.getPath()));
return null;
}
CodeReviewCommit newCommit = maybeNewCommit.get();
if (Objects.equals(newCommit, oldCommit)) {
// gitlink have already been updated for this submodule
return null;
}
ed.add(new PathEdit(s.getPath()) {
@Override
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(newCommit.getId());
}
});
if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
createSubmoduleCommitMsg(msgbuf, s, subOr, newCommit, oldCommit);
}
subOr.getCodeReviewRevWalk().parseBody(newCommit);
return newCommit;
}
use of com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo in project gerrit by GerritCodeReview.
the class SubmoduleCommits method composeGitlinksCommit.
/**
* Create a separate gitlink commit
*
* @param subscriber superproject (and branch)
* @param subscriptions subprojects the superproject is subscribed to
* @return a new commit on top of subscriber with gitlinks update to the tips of the subprojects;
* empty if nothing has changed. Subproject tips are read from the cached branched tips
* (defaulting to the mergeOpRepoManager).
*/
Optional<CodeReviewCommit> composeGitlinksCommit(BranchNameKey subscriber, Collection<SubmoduleSubscription> subscriptions) throws IOException, SubmoduleConflictException {
OpenRepo or;
try {
or = orm.getRepo(subscriber.project());
} catch (NoSuchProjectException | IOException e) {
throw new StorageException("Cannot access superproject", e);
}
CodeReviewCommit currentCommit = branchTips.getTip(subscriber, or).orElseThrow(() -> new SubmoduleConflictException("The branch was probably deleted from the subscriber repository"));
StringBuilder msgbuf = new StringBuilder();
PersonIdent author = null;
DirCache dc = readTree(or.getCodeReviewRevWalk(), currentCommit);
DirCacheEditor ed = dc.editor();
int count = 0;
for (SubmoduleSubscription s : sortByPath(subscriptions)) {
if (count > 0) {
msgbuf.append("\n\n");
}
RevCommit newCommit = updateSubmodule(dc, ed, msgbuf, s);
count++;
if (newCommit != null) {
PersonIdent newCommitAuthor = newCommit.getAuthorIdent();
if (author == null) {
author = new PersonIdent(newCommitAuthor, myIdent.getWhen());
} else if (!author.getName().equals(newCommitAuthor.getName()) || !author.getEmailAddress().equals(newCommitAuthor.getEmailAddress())) {
author = myIdent;
}
}
}
ed.finish();
ObjectId newTreeId = dc.writeTree(or.ins);
// Gitlinks are already in the branch, return null
if (newTreeId.equals(currentCommit.getTree())) {
return Optional.empty();
}
CommitBuilder commit = new CommitBuilder();
commit.setTreeId(newTreeId);
commit.setParentId(currentCommit);
StringBuilder commitMsg = new StringBuilder("Update git submodules\n\n");
if (verboseSuperProject != VerboseSuperprojectUpdate.FALSE) {
commitMsg.append(msgbuf);
}
commit.setMessage(commitMsg.toString());
commit.setAuthor(author);
commit.setCommitter(myIdent);
ObjectId id = or.ins.insert(commit);
return Optional.of(or.getCodeReviewRevWalk().parseCommit(id));
}
use of com.google.gerrit.server.submit.MergeOpRepoManager.OpenRepo in project gerrit by GerritCodeReview.
the class SubmoduleCommitsTest method directUpdateSubmodule.
private CodeReviewCommit directUpdateSubmodule(Project.NameKey project, String refName, Project.NameKey path, AnyObjectId id) throws Exception {
OpenRepo or = mergeOpRepoManager.getRepo(project);
Repository serverRepo = or.repo;
ObjectInserter ins = or.ins;
CodeReviewRevWalk rw = or.rw;
Ref ref = serverRepo.exactRef(refName);
assertWithMessage(refName).that(ref).isNotNull();
ObjectId oldCommitId = ref.getObjectId();
DirCache dc = DirCache.newInCore();
DirCacheBuilder b = dc.builder();
b.addTree(new byte[0], DirCacheEntry.STAGE_0, rw.getObjectReader(), rw.parseTree(oldCommitId));
b.finish();
DirCacheEditor e = dc.editor();
e.add(new PathEdit(path.get()) {
@Override
public void apply(DirCacheEntry ent) {
ent.setFileMode(FileMode.GITLINK);
ent.setObjectId(id);
}
});
e.finish();
CommitBuilder cb = new CommitBuilder();
cb.addParentId(oldCommitId);
cb.setTreeId(dc.writeTree(ins));
cb.setAuthor(ident);
cb.setCommitter(ident);
cb.setMessage("Direct update submodule " + path);
ObjectId newCommitId = ins.insert(cb);
ins.flush();
RefUpdate ru = serverRepo.updateRef(refName);
ru.setExpectedOldObjectId(oldCommitId);
ru.setNewObjectId(newCommitId);
assertThat(ru.update()).isEqualTo(RefUpdate.Result.FAST_FORWARD);
return rw.parseCommit(newCommitId);
}
Aggregations