use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.
the class SubmitResolvingMergeCommitIT method assertChangeSetMergeable.
private void assertChangeSetMergeable(ChangeData change, boolean expected) throws MissingObjectException, IncorrectObjectTypeException, IOException, PermissionBackendException {
ChangeSet cs = mergeSuperSet.get().completeChangeSet(change.change(), user(admin), /* includingTopicClosure= */
false);
assertThat(submit.unmergeableChanges(cs).isEmpty()).isEqualTo(expected);
}
use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.
the class Submit method unmergeableChanges.
public Collection<ChangeData> unmergeableChanges(ChangeSet cs) throws IOException {
Set<ChangeData> mergeabilityMap = new HashSet<>();
Set<ObjectId> outDatedPatchsets = new HashSet<>();
for (ChangeData change : cs.changes()) {
mergeabilityMap.add(change);
// Add all the patchsets commit ids except the current patchset.
outDatedPatchsets.addAll(change.notes().getPatchSets().values().stream().map(p -> p.commitId()).collect(Collectors.toSet()));
outDatedPatchsets.remove(change.currentPatchSet().commitId());
}
ListMultimap<BranchNameKey, ChangeData> cbb = cs.changesByBranch();
for (BranchNameKey branch : cbb.keySet()) {
Collection<ChangeData> targetBranch = cbb.get(branch);
HashMap<Change.Id, RevCommit> commits = findCommits(targetBranch, branch.project());
Set<ObjectId> allParents = Sets.newHashSetWithExpectedSize(cs.size());
for (RevCommit commit : commits.values()) {
for (RevCommit parent : commit.getParents()) {
allParents.add(parent.getId());
}
}
for (ChangeData change : targetBranch) {
RevCommit commit = commits.get(change.getId());
boolean isMergeCommit = commit.getParentCount() > 1;
boolean isLastInChain = !allParents.contains(commit.getId());
if (Arrays.stream(commit.getParents()).anyMatch(c -> outDatedPatchsets.contains(c.getId())) && !isCherryPickSubmit(change)) {
// cherry-pick.
continue;
}
// Recheck mergeability rather than using value stored in the index,
// which may be stale.
// TODO(dborowitz): This is ugly; consider providing a way to not read
// stored fields from the index in the first place.
change.setMergeable(null);
Boolean mergeable = change.isMergeable();
if (mergeable == null) {
// Skip whole check, cannot determine if mergeable
return null;
}
if (mergeable) {
mergeabilityMap.remove(change);
}
if (isLastInChain && isMergeCommit && mergeable) {
for (ChangeData c : targetBranch) {
mergeabilityMap.remove(c);
}
break;
}
}
}
return mergeabilityMap;
}
use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.
the class SubmittedTogether method applyInfo.
public SubmittedTogetherInfo applyInfo(ChangeResource resource) throws AuthException, IOException, PermissionBackendException {
Change c = resource.getChange();
try {
List<ChangeData> cds;
int hidden;
if (c.isNew()) {
ChangeSet cs = mergeSuperSet.get().completeChangeSet(c, resource.getUser(), options.contains(TOPIC_CLOSURE));
cds = ensureRequiredDataIsLoaded(cs.changes().asList());
hidden = cs.nonVisibleChanges().size();
} else if (c.isMerged()) {
cds = queryProvider.get().bySubmissionId(c.getSubmissionId());
hidden = 0;
} else {
cds = Collections.emptyList();
hidden = 0;
}
if (hidden != 0 && !options.contains(NON_VISIBLE_CHANGES)) {
throw new AuthException("change would be submitted with a change that you cannot see");
}
cds = sort(cds, hidden);
SubmittedTogetherInfo info = new SubmittedTogetherInfo();
info.changes = json.create(jsonOpt).format(cds);
info.nonVisibleChanges = hidden;
return info;
} catch (StorageException | IOException e) {
logger.atSevere().withCause(e).log("Error on getting a ChangeSet");
throw e;
}
}
use of com.google.gerrit.server.submit.ChangeSet in project gerrit by GerritCodeReview.
the class Submit method getDescription.
@Override
public UiAction.Description getDescription(RevisionResource resource) throws IOException, PermissionBackendException {
Change change = resource.getChange();
if (!change.isNew() || !resource.isCurrent()) {
// submit not visible
return null;
}
if (!projectCache.get(resource.getProject()).map(ProjectState::statePermitsWrite).orElse(false)) {
// submit not visible
return null;
}
ChangeData cd = resource.getChangeResource().getChangeData();
try {
MergeOp.checkSubmitRequirements(cd);
} catch (ResourceConflictException e) {
// submit not visible
return null;
}
ChangeSet cs = mergeSuperSet.get().completeChangeSet(cd.change(), resource.getUser(), /*includingTopicClosure= */
false);
String topic = change.getTopic();
int topicSize = 0;
if (!Strings.isNullOrEmpty(topic)) {
topicSize = queryProvider.get().noFields().byTopicOpen(topic).size();
}
boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
if (submitProblems != null) {
return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
}
// Recheck mergeability rather than using value stored in the index, which may be stale.
// TODO(dborowitz): This is ugly; consider providing a way to not read stored fields from the
// index in the first place.
// cd.setMergeable(null);
// That was done in unmergeableChanges which was called by problemsForSubmittingChangeset, so
// now it is safe to read from the cache, as it yields the same result.
Boolean enabled = cd.isMergeable();
if (treatWithTopic) {
Map<String, String> params = ImmutableMap.of("topicSize", String.valueOf(topicSize), "submitSize", String.valueOf(cs.size()));
return new UiAction.Description().setLabel(submitTopicLabel).setTitle(Strings.emptyToNull(submitTopicTooltip.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().number()), "branch", change.getDest().shortName(), "commit", abbreviateName(resource.getPatchSet().commitId()), "submitSize", String.valueOf(cs.size()));
ParameterizedString tp = cs.size() > 1 ? titlePatternWithAncestors : titlePattern;
return new UiAction.Description().setLabel(cs.size() > 1 ? labelWithParents : label).setTitle(Strings.emptyToNull(tp.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Aggregations