Search in sources :

Example 41 with PatchSet

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);
}
Also used : CommitMessageUtil(com.google.gerrit.server.util.CommitMessageUtil) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) ProjectCache(com.google.gerrit.server.project.ProjectCache) Inject(com.google.inject.Inject) RawTextComparator(org.eclipse.jgit.diff.RawTextComparator) ChangeFileContentModification(com.google.gerrit.server.edit.tree.ChangeFileContentModification) ReceiveCommand(org.eclipse.jgit.transport.ReceiveCommand) InvalidPathException(org.eclipse.jgit.dircache.InvalidPathException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) ChangeIndexer(com.google.gerrit.server.index.change.ChangeIndexer) InvalidChangeOperationException(com.google.gerrit.server.project.InvalidChangeOperationException) RefNames(com.google.gerrit.entities.RefNames) AuthException(com.google.gerrit.extensions.restapi.AuthException) RawText(org.eclipse.jgit.diff.RawText) RawInput(com.google.gerrit.extensions.restapi.RawInput) MergeChunk(org.eclipse.jgit.merge.MergeChunk) MergeAlgorithm(org.eclipse.jgit.merge.MergeAlgorithm) TimeZone(java.util.TimeZone) RefUpdate(org.eclipse.jgit.lib.RefUpdate) Instant(java.time.Instant) NullProgressMonitor(org.eclipse.jgit.lib.NullProgressMonitor) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) RevTree(org.eclipse.jgit.revwalk.RevTree) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) BatchRefUpdate(org.eclipse.jgit.lib.BatchRefUpdate) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) Optional(java.util.Optional) Singleton(com.google.inject.Singleton) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) ChangeUtil(com.google.gerrit.server.ChangeUtil) LockFailureException(com.google.gerrit.git.LockFailureException) Charsets(com.google.common.base.Charsets) TreeModification(com.google.gerrit.server.edit.tree.TreeModification) SupportedAlgorithm(org.eclipse.jgit.diff.DiffAlgorithm.SupportedAlgorithm) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) CurrentUser(com.google.gerrit.server.CurrentUser) TreeCreator(com.google.gerrit.server.edit.tree.TreeCreator) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) DiffAlgorithm(org.eclipse.jgit.diff.DiffAlgorithm) DeleteFileModification(com.google.gerrit.server.edit.tree.DeleteFileModification) RenameFileModification(com.google.gerrit.server.edit.tree.RenameFileModification) ObjectId(org.eclipse.jgit.lib.ObjectId) MergeResult(org.eclipse.jgit.merge.MergeResult) RestoreFileModification(com.google.gerrit.server.edit.tree.RestoreFileModification) Provider(com.google.inject.Provider) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) Project(com.google.gerrit.entities.Project) TimeUtil(com.google.gerrit.server.util.time.TimeUtil) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) Instant(java.time.Instant) PatchSet(com.google.gerrit.entities.PatchSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 42 with PatchSet

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;
    }));
}
Also used : SubmitRequirementAttribute(com.google.gerrit.server.data.SubmitRequirementAttribute) ListMultimap(com.google.common.collect.ListMultimap) DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) Inject(com.google.inject.Inject) HumanComment(com.google.gerrit.entities.HumanComment) RefUpdateAttribute(com.google.gerrit.server.data.RefUpdateAttribute) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) LabelTypes(com.google.gerrit.entities.LabelTypes) LegacySubmitRequirement(com.google.gerrit.entities.LegacySubmitRequirement) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) UserIdentity(com.google.gerrit.entities.UserIdentity) LabelType(com.google.gerrit.entities.LabelType) Map(java.util.Map) UrlFormatter(com.google.gerrit.server.config.UrlFormatter) AccountAttribute(com.google.gerrit.server.data.AccountAttribute) PatchSetCommentAttribute(com.google.gerrit.server.data.PatchSetCommentAttribute) Collection(java.util.Collection) Account(com.google.gerrit.entities.Account) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) Set(java.util.Set) ChangeKindCache(com.google.gerrit.server.change.ChangeKindCache) SubmitRecord(com.google.gerrit.entities.SubmitRecord) ApprovalAttribute(com.google.gerrit.server.data.ApprovalAttribute) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) PersonIdent(org.eclipse.jgit.lib.PersonIdent) ChangeData(com.google.gerrit.server.query.change.ChangeData) List(java.util.List) Emails(com.google.gerrit.server.account.Emails) DiffOperations(com.google.gerrit.server.patch.DiffOperations) SubmitRecordAttribute(com.google.gerrit.server.data.SubmitRecordAttribute) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) TrackingIdAttribute(com.google.gerrit.server.data.TrackingIdAttribute) Singleton(com.google.inject.Singleton) AccountCache(com.google.gerrit.server.account.AccountCache) IndexConfig(com.google.gerrit.index.IndexConfig) RevCommit(org.eclipse.jgit.revwalk.RevCommit) ArrayList(java.util.ArrayList) ChangeMessage(com.google.gerrit.entities.ChangeMessage) Lists(com.google.common.collect.Lists) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) Objects.requireNonNull(java.util.Objects.requireNonNull) FilePathAdapter(com.google.gerrit.server.patch.FilePathAdapter) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) Comparator.comparing(java.util.Comparator.comparing) SubmitLabelAttribute(com.google.gerrit.server.data.SubmitLabelAttribute) StorageException(com.google.gerrit.exceptions.StorageException) DependencyAttribute(com.google.gerrit.server.data.DependencyAttribute) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) DiffOptions(com.google.gerrit.server.patch.DiffOptions) Provider(com.google.inject.Provider) MessageAttribute(com.google.gerrit.server.data.MessageAttribute) AccountTemplateUtil(com.google.gerrit.server.util.AccountTemplateUtil) AccountState(com.google.gerrit.server.account.AccountState) GerritPersonIdent(com.google.gerrit.server.GerritPersonIdent) ChangeAttribute(com.google.gerrit.server.data.ChangeAttribute) PatchAttribute(com.google.gerrit.server.data.PatchAttribute) ArrayList(java.util.ArrayList) PatchSet(com.google.gerrit.entities.PatchSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 43 with PatchSet

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;
}
Also used : AccountAttribute(com.google.gerrit.server.data.AccountAttribute) UserIdentity(com.google.gerrit.entities.UserIdentity) PatchSet(com.google.gerrit.entities.PatchSet) IOException(java.io.IOException) FileDiffOutput(com.google.gerrit.server.patch.filediff.FileDiffOutput) DiffNotAvailableException(com.google.gerrit.server.patch.DiffNotAvailableException) PatchSetAttribute(com.google.gerrit.server.data.PatchSetAttribute) StorageException(com.google.gerrit.exceptions.StorageException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 44 with PatchSet

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");
    }
}
Also used : PatchSet(com.google.gerrit.entities.PatchSet) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) Change(com.google.gerrit.entities.Change) StorageException(com.google.gerrit.exceptions.StorageException)

Example 45 with PatchSet

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;
}
Also used : Account(com.google.gerrit.entities.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) PatchSet(com.google.gerrit.entities.PatchSet) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm)

Aggregations

PatchSet (com.google.gerrit.entities.PatchSet)123 Change (com.google.gerrit.entities.Change)61 Test (org.junit.Test)48 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)41 ObjectId (org.eclipse.jgit.lib.ObjectId)35 RevCommit (org.eclipse.jgit.revwalk.RevCommit)29 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)28 Project (com.google.gerrit.entities.Project)25 StorageException (com.google.gerrit.exceptions.StorageException)25 Repository (org.eclipse.jgit.lib.Repository)22 IOException (java.io.IOException)20 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)19 ChangeData (com.google.gerrit.server.query.change.ChangeData)18 HumanComment (com.google.gerrit.entities.HumanComment)16 RevWalk (org.eclipse.jgit.revwalk.RevWalk)16 Inject (com.google.inject.Inject)14 Map (java.util.Map)14 List (java.util.List)13 ImmutableList (com.google.common.collect.ImmutableList)12 AuthException (com.google.gerrit.extensions.restapi.AuthException)11