use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ReviewCommand method addPatchSetId.
@Argument(index = 0, required = true, multiValued = true, metaVar = "{COMMIT | CHANGE,PATCHSET}", usage = "list of commits or patch sets to review")
void addPatchSetId(String token) {
try {
PatchSet ps = psParser.parsePatchSet(token, projectState, branch);
patchSets.add(ps);
} catch (UnloggedFailure e) {
throw new IllegalArgumentException(e.getMessage(), e);
} catch (StorageException e) {
throw new IllegalArgumentException("database error", e);
}
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class PatchSetParser method parsePatchSet.
public PatchSet parsePatchSet(String token, ProjectState projectState, String branch) throws UnloggedFailure {
//
if (token.matches("^([0-9a-fA-F]{4," + ObjectIds.STR_LEN + "})$")) {
InternalChangeQuery query = queryProvider.get();
List<ChangeData> cds;
if (projectState != null) {
Project.NameKey p = projectState.getNameKey();
if (branch != null) {
cds = query.byBranchCommit(p.get(), branch, token);
} else {
cds = query.byProjectCommit(p, token);
}
} else {
cds = query.byCommit(token);
}
List<PatchSet> matches = new ArrayList<>(cds.size());
for (ChangeData cd : cds) {
Change c = cd.change();
if (!(inProject(c, projectState) && inBranch(c, branch))) {
continue;
}
for (PatchSet ps : cd.patchSets()) {
if (ObjectIds.matchesAbbreviation(ps.commitId(), token)) {
matches.add(ps);
}
}
}
switch(matches.size()) {
case 1:
return matches.iterator().next();
case 0:
throw error("\"" + token + "\" no such patch set");
default:
throw error("\"" + token + "\" matches multiple patch sets");
}
}
//
if (token.matches("^[1-9][0-9]*,[1-9][0-9]*$")) {
PatchSet.Id patchSetId;
try {
patchSetId = PatchSet.Id.parse(token);
} catch (IllegalArgumentException e) {
throw error("\"" + token + "\" is not a valid patch set", e);
}
ChangeNotes notes = getNotes(projectState, patchSetId.changeId());
PatchSet patchSet = psUtil.get(notes, patchSetId);
if (patchSet == null) {
throw error("\"" + token + "\" no such patch set");
}
if (projectState != null || branch != null) {
Change change = notes.getChange();
if (!inProject(change, projectState)) {
throw error("change " + change.getId() + " not in project " + projectState.getName());
}
if (!inBranch(change, branch)) {
throw error("change " + change.getId() + " not in branch " + branch);
}
}
return patchSet;
}
throw error("\"" + token + "\" is not a valid patch set");
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ChangeEditIT method rebaseEdit.
@Test
public void rebaseEdit() throws Exception {
PatchSet previousPatchSet = getCurrentPatchSet(changeId2);
createEmptyEditFor(changeId2);
gApi.changes().id(changeId2).edit().modifyFile(FILE_NAME, RawInputUtil.create(CONTENT_NEW));
addNewPatchSet(changeId2);
PatchSet currentPatchSet = getCurrentPatchSet(changeId2);
Optional<EditInfo> originalEdit = getEdit(changeId2);
assertThat(originalEdit).value().baseRevision().isEqualTo(previousPatchSet.commitId().name());
Timestamp beforeRebase = originalEdit.get().commit.committer.date;
gApi.changes().id(changeId2).edit().rebase();
ensureSameBytes(getFileContentOfEdit(changeId2, FILE_NAME), CONTENT_NEW);
ensureSameBytes(getFileContentOfEdit(changeId2, FILE_NAME2), CONTENT_NEW2);
Optional<EditInfo> rebasedEdit = getEdit(changeId2);
assertThat(rebasedEdit).value().baseRevision().isEqualTo(currentPatchSet.commitId().name());
assertThat(rebasedEdit).value().commit().committer().date().isNotEqualTo(beforeRebase);
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ApprovalInference method getForPatchSetWithoutNormalization.
private Collection<PatchSetApproval> getForPatchSetWithoutNormalization(ChangeNotes notes, ProjectState project, PatchSet patchSet, RevWalk rw, Config repoConfig) {
checkState(project.getNameKey().equals(notes.getProjectName()), "project must match %s, %s", project.getNameKey(), notes.getProjectName());
PatchSet.Id psId = patchSet.id();
// Add approvals on the given patch set to the result
Table<String, Account.Id, PatchSetApproval> resultByUser = HashBasedTable.create();
ImmutableList<PatchSetApproval> nonCopiedApprovalsForGivenPatchSet = notes.load().getApprovals().get(patchSet.id());
nonCopiedApprovalsForGivenPatchSet.forEach(psa -> resultByUser.put(psa.label(), psa.accountId(), psa));
// given patch set.
if (psId.get() == 1) {
return resultByUser.values();
}
Map.Entry<PatchSet.Id, PatchSet> priorPatchSet = notes.load().getPatchSets().lowerEntry(psId);
if (priorPatchSet == null) {
return resultByUser.values();
}
ImmutableList<PatchSetApproval> priorApprovalsIncludingCopied = notes.load().getApprovalsWithCopied().get(priorPatchSet.getKey());
// Add labels from the previous patch set to the result in case the label isn't already there
// and settings as well as change kind allow copying.
ChangeKind changeKind = changeKindCache.getChangeKind(project.getNameKey(), rw, repoConfig, priorPatchSet.getValue().commitId(), patchSet.commitId());
logger.atFine().log("change kind for patch set %d of change %d against prior patch set %s is %s", patchSet.id().get(), patchSet.id().changeId().get(), priorPatchSet.getValue().id().changeId(), changeKind);
Map<String, ModifiedFile> baseVsCurrent = null;
Map<String, ModifiedFile> baseVsPrior = null;
Map<String, ModifiedFile> priorVsCurrent = null;
LabelTypes labelTypes = project.getLabelTypes();
for (PatchSetApproval psa : priorApprovalsIncludingCopied) {
if (resultByUser.contains(psa.label(), psa.accountId())) {
continue;
}
Optional<LabelType> type = labelTypes.byLabel(psa.labelId());
// Only compute modified files if there is a relevant label, since this is expensive.
if (baseVsCurrent == null && type.isPresent() && type.get().isCopyAllScoresIfListOfFilesDidNotChange()) {
baseVsCurrent = listModifiedFiles(project, patchSet, rw, repoConfig);
baseVsPrior = listModifiedFiles(project, priorPatchSet.getValue(), rw, repoConfig);
priorVsCurrent = listModifiedFiles(project, priorPatchSet.getValue().commitId(), patchSet.commitId(), rw, repoConfig);
}
if (!type.isPresent()) {
logger.atFine().log("approval %d on label %s of patch set %d of change %d cannot be copied" + " to patch set %d because the label no longer exists on project %s", psa.value(), psa.label(), psa.key().patchSetId().get(), psa.key().patchSetId().changeId().get(), psId.get(), project.getName());
continue;
}
if (!canCopyBasedOnBooleanLabelConfigs(project, psa, patchSet.id(), changeKind, type.get(), baseVsCurrent, baseVsPrior, priorVsCurrent) && !canCopyBasedOnCopyCondition(notes, psa, patchSet, type.get(), changeKind, rw, repoConfig)) {
continue;
}
resultByUser.put(psa.label(), psa.accountId(), psa.copyWithPatchSet(patchSet.id()));
}
return resultByUser.values();
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class CommitUtil method createRevertCommit.
/**
* Creates a revert commit.
*
* @param message Commit message for the revert commit.
* @param notes ChangeNotes of the change being reverted.
* @param user Current User performing the revert.
* @param ts Timestamp of creation for the commit.
* @param oi ObjectInserter for inserting the newly created commit.
* @param revWalk Used for parsing the original commit.
* @param generatedChangeId The changeId for the commit message, can be null since it is not
* needed for commits, only for changes.
* @return ObjectId that represents the newly created commit.
* @throws ResourceConflictException Can't revert the initial commit.
* @throws IOException Thrown in case of I/O errors.
*/
private ObjectId createRevertCommit(String message, ChangeNotes notes, CurrentUser user, Instant ts, ObjectInserter oi, RevWalk revWalk, @Nullable ObjectId generatedChangeId) throws ResourceConflictException, IOException {
PatchSet patch = notes.getCurrentPatchSet();
RevCommit commitToRevert = revWalk.parseCommit(patch.commitId());
if (commitToRevert.getParentCount() == 0) {
throw new ResourceConflictException("Cannot revert initial commit");
}
PersonIdent committerIdent = serverIdent.get();
PersonIdent authorIdent = user.asIdentifiedUser().newCommitterIdent(ts, committerIdent.getTimeZone());
RevCommit parentToCommitToRevert = commitToRevert.getParent(0);
revWalk.parseHeaders(parentToCommitToRevert);
CommitBuilder revertCommitBuilder = new CommitBuilder();
revertCommitBuilder.addParentId(commitToRevert);
revertCommitBuilder.setTreeId(parentToCommitToRevert.getTree());
revertCommitBuilder.setAuthor(authorIdent);
revertCommitBuilder.setCommitter(authorIdent);
Change changeToRevert = notes.getChange();
String subject = changeToRevert.getSubject();
if (subject.length() > 63) {
subject = subject.substring(0, 59) + "...";
}
if (message == null) {
message = MessageFormat.format(ChangeMessages.get().revertChangeDefaultMessage, subject, patch.commitId().name());
}
if (generatedChangeId != null) {
revertCommitBuilder.setMessage(ChangeIdUtil.insertId(message, generatedChangeId, true));
}
ObjectId id = oi.insert(revertCommitBuilder);
oi.flush();
return id;
}
Aggregations