use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class ChangeEditModifier method modifyCommit.
private ChangeEdit modifyCommit(Repository repository, ChangeNotes notes, ModificationIntention modificationIntention, CommitModification commitModification) throws AuthException, BadRequestException, IOException, InvalidChangeOperationException, PermissionBackendException, ResourceConflictException {
assertCanEdit(notes);
Optional<ChangeEdit> optionalChangeEdit = lookupChangeEdit(notes);
EditBehavior editBehavior = optionalChangeEdit.<EditBehavior>map(changeEdit -> new ExistingEditBehavior(changeEdit, noteDbEdits)).orElseGet(() -> new NewEditBehavior(noteDbEdits));
ModificationTarget modificationTarget = editBehavior.getModificationTarget(notes, modificationIntention);
RevCommit commitToModify = modificationTarget.getCommit(repository);
ObjectId newTreeId = createNewTree(repository, commitToModify, commitModification.treeModifications());
newTreeId = editBehavior.mergeTreesIfNecessary(repository, newTreeId, commitToModify);
PatchSet basePatchset = modificationTarget.getBasePatchset();
RevCommit basePatchsetCommit = NoteDbEdits.lookupCommit(repository, basePatchset.commitId());
boolean changeIdRequired = projectCache.get(notes.getChange().getProject()).orElseThrow(illegalState(notes.getChange().getProject())).is(BooleanProjectConfig.REQUIRE_CHANGE_ID);
String currentChangeId = notes.getChange().getKey().get();
String newCommitMessage = createNewCommitMessage(changeIdRequired, currentChangeId, editBehavior, commitModification, commitToModify);
newCommitMessage = editBehavior.mergeCommitMessageIfNecessary(newCommitMessage, commitToModify);
Optional<ChangeEdit> unmodifiedEdit = editBehavior.getEditIfNoModification(newTreeId, newCommitMessage);
if (unmodifiedEdit.isPresent()) {
return unmodifiedEdit.get();
}
Instant nowTimestamp = TimeUtil.now();
ObjectId newEditCommit = createCommit(repository, basePatchsetCommit, newTreeId, newCommitMessage, nowTimestamp);
return editBehavior.updateEditInStorage(repository, notes, basePatchset, newEditCommit, nowTimestamp);
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class EventFactory method addDependsOn.
private void addDependsOn(RevWalk rw, ChangeAttribute ca, Change change, PatchSet currentPs) throws IOException {
RevCommit commit = rw.parseCommit(currentPs.commitId());
final List<String> parentNames = new ArrayList<>(commit.getParentCount());
for (RevCommit p : commit.getParents()) {
parentNames.add(p.name());
}
// this patch set's revision.
for (ChangeData cd : queryProvider.get().byProjectCommits(change.getProject(), parentNames)) {
for (PatchSet ps : cd.patchSets()) {
for (String p : parentNames) {
if (!ps.commitId().name().equals(p)) {
continue;
}
ca.dependsOn.add(newDependsOn(requireNonNull(cd.change()), ps));
}
}
}
// Sort by original parent order.
ca.dependsOn.sort(comparing(d -> {
for (int i = 0; i < parentNames.size(); i++) {
if (parentNames.get(i).equals(d.revision)) {
return i;
}
}
return parentNames.size() + 1;
}));
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class EventFactory method asPatchSetAttribute.
/**
* Create a PatchSetAttribute for the given patchset suitable for serialization to JSON.
*/
public PatchSetAttribute asPatchSetAttribute(RevWalk revWalk, Change change, PatchSet patchSet) {
PatchSetAttribute p = new PatchSetAttribute();
p.revision = patchSet.commitId().name();
p.number = patchSet.number();
p.ref = patchSet.refName();
p.uploader = asAccountAttribute(patchSet.uploader());
p.createdOn = patchSet.createdOn().getEpochSecond();
PatchSet.Id pId = patchSet.id();
try {
p.parents = new ArrayList<>();
RevCommit c = revWalk.parseCommit(ObjectId.fromString(p.revision));
for (RevCommit parent : c.getParents()) {
p.parents.add(parent.name());
}
UserIdentity author = emails.toUserIdentity(c.getAuthorIdent());
if (author.getAccount() == null) {
p.author = new AccountAttribute();
p.author.email = author.getEmail();
p.author.name = author.getName();
p.author.username = "";
} else {
p.author = asAccountAttribute(author.getAccount());
}
Map<String, FileDiffOutput> modifiedFiles = diffOperations.listModifiedFilesAgainstParent(change.getProject(), patchSet.commitId(), /* parentNum= */
0, DiffOptions.DEFAULTS);
for (FileDiffOutput fileDiff : modifiedFiles.values()) {
p.sizeDeletions += fileDiff.deletions();
p.sizeInsertions += fileDiff.insertions();
}
p.kind = changeKindCache.getChangeKind(change, patchSet);
} catch (IOException | StorageException e) {
logger.atSevere().withCause(e).log("Cannot load patch set data for %s", patchSet.id());
} catch (DiffNotAvailableException e) {
logger.atSevere().withCause(e).log("Cannot get size information for %s.", pId);
}
return p;
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class StreamEventsApiListener method onPrivateStateChanged.
@Override
public void onPrivateStateChanged(PrivateStateChangedListener.Event ev) {
try {
ChangeNotes notes = getNotes(ev.getChange());
Change change = notes.getChange();
PatchSet patchSet = getPatchSet(notes, ev.getRevision());
PrivateStateChangedEvent event = new PrivateStateChangedEvent(change);
event.change = changeAttributeSupplier(change, notes);
event.changer = accountAttributeSupplier(ev.getWho());
event.patchSet = patchSetAttributeSupplier(change, patchSet);
dispatcher.run(d -> d.postEvent(change, event));
} catch (StorageException e) {
logger.atSevere().withCause(e).log("Failed to dispatch event");
}
}
use of com.google.gerrit.entities.PatchSet in project gerrit by GerritCodeReview.
the class PRED_uploader_1 method exec.
@Override
public Operation exec(Prolog engine) throws PrologException {
engine.setB0();
Term a1 = arg1.dereference();
PatchSet patchSet = StoredValues.getPatchSet(engine);
if (patchSet == null) {
logger.atSevere().log("Failed to load current patch set of change %s", StoredValues.getChange(engine).getChangeId());
return engine.fail();
}
Account.Id uploaderId = patchSet.uploader();
if (!a1.unify(new StructureTerm(user, new IntegerTerm(uploaderId.get())), engine.trail)) {
return engine.fail();
}
return cont;
}
Aggregations