Search in sources :

Example 1 with ChangeAlreadyMergedException

use of com.google.gerrit.server.submit.ChangeAlreadyMergedException in project gerrit by GerritCodeReview.

the class MergeUtil method createMergeCommit.

public static CodeReviewCommit createMergeCommit(ObjectInserter inserter, Config repoConfig, RevCommit mergeTip, RevCommit originalCommit, String mergeStrategy, boolean allowConflicts, PersonIdent authorIdent, PersonIdent committerIdent, String commitMsg, CodeReviewRevWalk rw) throws IOException, MergeIdenticalTreeException, MergeConflictException, InvalidMergeStrategyException {
    if (!MergeStrategy.THEIRS.getName().equals(mergeStrategy) && rw.isMergedInto(originalCommit, mergeTip)) {
        throw new ChangeAlreadyMergedException("'" + originalCommit.getName() + "' has already been merged");
    }
    Merger m = newMerger(inserter, repoConfig, mergeStrategy);
    DirCache dc = DirCache.newInCore();
    if (allowConflicts && m instanceof ResolveMerger) {
        // The DirCache must be set on ResolveMerger before calling
        // ResolveMerger#merge(AnyObjectId...) otherwise the entries in DirCache don't get populated.
        ((ResolveMerger) m).setDirCache(dc);
    }
    ObjectId tree;
    ImmutableSet<String> filesWithGitConflicts;
    if (m.merge(false, mergeTip, originalCommit)) {
        filesWithGitConflicts = null;
        tree = m.getResultTreeId();
    } else {
        List<String> conflicts = ImmutableList.of();
        if (m instanceof ResolveMerger) {
            conflicts = ((ResolveMerger) m).getUnmergedPaths();
        }
        if (!allowConflicts) {
            throw new MergeConflictException(createConflictMessage(conflicts));
        }
        // For merging with conflict markers we need a ResolveMerger, double-check that we have one.
        if (!(m instanceof ResolveMerger)) {
            throw new MergeWithConflictsNotSupportedException(MergeStrategy.get(mergeStrategy));
        }
        Map<String, MergeResult<? extends Sequence>> mergeResults = ((ResolveMerger) m).getMergeResults();
        filesWithGitConflicts = mergeResults.entrySet().stream().filter(e -> e.getValue().containsConflicts()).map(Map.Entry::getKey).collect(toImmutableSet());
        tree = mergeWithConflicts(rw, inserter, dc, "TARGET BRANCH", mergeTip, "SOURCE BRANCH", originalCommit, mergeResults);
    }
    CommitBuilder mergeCommit = new CommitBuilder();
    mergeCommit.setTreeId(tree);
    mergeCommit.setParentIds(mergeTip, originalCommit);
    mergeCommit.setAuthor(authorIdent);
    mergeCommit.setCommitter(committerIdent);
    mergeCommit.setMessage(commitMsg);
    CodeReviewCommit commit = rw.parseCommit(inserter.insert(mergeCommit));
    commit.setFilesWithGitConflicts(filesWithGitConflicts);
    return commit;
}
Also used : ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RevisionSyntaxException(org.eclipse.jgit.errors.RevisionSyntaxException) Comparator.naturalOrder(java.util.Comparator.naturalOrder) ObjectInserter(org.eclipse.jgit.lib.ObjectInserter) DynamicItem(com.google.gerrit.extensions.registration.DynamicItem) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) ThreeWayMergeStrategy(org.eclipse.jgit.merge.ThreeWayMergeStrategy) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) Merger(org.eclipse.jgit.merge.Merger) Assisted(com.google.inject.assistedinject.Assisted) FooterKey(org.eclipse.jgit.revwalk.FooterKey) RevWalk(org.eclipse.jgit.revwalk.RevWalk) PatchSetApproval(com.google.gerrit.entities.PatchSetApproval) Config(org.eclipse.jgit.lib.Config) Preconditions.checkArgument(com.google.common.base.Preconditions.checkArgument) InvalidMergeStrategyException(com.google.gerrit.exceptions.InvalidMergeStrategyException) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) FooterLine(org.eclipse.jgit.revwalk.FooterLine) LabelType(com.google.gerrit.entities.LabelType) Map(java.util.Map) AssistedInject(com.google.inject.assistedinject.AssistedInject) UrlFormatter(com.google.gerrit.server.config.UrlFormatter) ImmutableSortedSet.toImmutableSortedSet(com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet) ImmutableSet(com.google.common.collect.ImmutableSet) Sequence(org.eclipse.jgit.diff.Sequence) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) Collection(java.util.Collection) LargeObjectException(org.eclipse.jgit.errors.LargeObjectException) Account(com.google.gerrit.entities.Account) Set(java.util.Set) RevSort(org.eclipse.jgit.revwalk.RevSort) Constants(org.eclipse.jgit.lib.Constants) MethodNotAllowedException(com.google.gerrit.extensions.restapi.MethodNotAllowedException) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ApprovalsUtil(com.google.gerrit.server.approval.ApprovalsUtil) MergeIdenticalTreeException(com.google.gerrit.server.submit.MergeIdenticalTreeException) MergeStrategy(org.eclipse.jgit.merge.MergeStrategy) Preconditions.checkState(com.google.common.base.Preconditions.checkState) Objects(java.util.Objects) PersonIdent(org.eclipse.jgit.lib.PersonIdent) List(java.util.List) Nullable(com.google.gerrit.common.Nullable) BooleanProjectConfig(com.google.gerrit.entities.BooleanProjectConfig) DirCache(org.eclipse.jgit.dircache.DirCache) Optional(java.util.Optional) FluentLogger(com.google.common.flogger.FluentLogger) AmbiguousObjectException(org.eclipse.jgit.errors.AmbiguousObjectException) Iterables(com.google.common.collect.Iterables) LabelId(com.google.gerrit.entities.LabelId) CodeReviewRevWalk(com.google.gerrit.server.git.CodeReviewCommit.CodeReviewRevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit) CommitMergeStatus(com.google.gerrit.server.submit.CommitMergeStatus) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) RevFlag(org.eclipse.jgit.revwalk.RevFlag) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) HashMap(java.util.HashMap) DirCacheBuilder(org.eclipse.jgit.dircache.DirCacheBuilder) MergeBaseFailureReason(org.eclipse.jgit.errors.NoMergeBaseException.MergeBaseFailureReason) ChangeAlreadyMergedException(com.google.gerrit.server.submit.ChangeAlreadyMergedException) ArrayList(java.util.ArrayList) Strings(com.google.common.base.Strings) ImmutableList(com.google.common.collect.ImmutableList) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) ImmutableSet.toImmutableSet(com.google.common.collect.ImmutableSet.toImmutableSet) MergeWithConflictsNotSupportedException(com.google.gerrit.exceptions.MergeWithConflictsNotSupportedException) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) ChangeUtil(com.google.gerrit.server.ChangeUtil) NoMergeBaseException(org.eclipse.jgit.errors.NoMergeBaseException) ImmutableSortedSet(com.google.common.collect.ImmutableSortedSet) TemporaryBuffer(org.eclipse.jgit.util.TemporaryBuffer) Iterator(java.util.Iterator) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) UTF_8(java.nio.charset.StandardCharsets.UTF_8) StorageException(com.google.gerrit.exceptions.StorageException) ProjectState(com.google.gerrit.server.project.ProjectState) ChangeNotes(com.google.gerrit.server.notedb.ChangeNotes) IOException(java.io.IOException) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) ObjectId(org.eclipse.jgit.lib.ObjectId) MergeResult(org.eclipse.jgit.merge.MergeResult) MergeSorter(com.google.gerrit.server.submit.MergeSorter) MergeFormatter(org.eclipse.jgit.merge.MergeFormatter) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) FooterConstants(com.google.gerrit.common.FooterConstants) Collections(java.util.Collections) Repository(org.eclipse.jgit.lib.Repository) InputStream(java.io.InputStream) ObjectId(org.eclipse.jgit.lib.ObjectId) MergeResult(org.eclipse.jgit.merge.MergeResult) CommitBuilder(org.eclipse.jgit.lib.CommitBuilder) Sequence(org.eclipse.jgit.diff.Sequence) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) DirCache(org.eclipse.jgit.dircache.DirCache) MergeConflictException(com.google.gerrit.extensions.restapi.MergeConflictException) DirCacheEntry(org.eclipse.jgit.dircache.DirCacheEntry) Merger(org.eclipse.jgit.merge.Merger) ResolveMerger(org.eclipse.jgit.merge.ResolveMerger) ThreeWayMerger(org.eclipse.jgit.merge.ThreeWayMerger) MergeWithConflictsNotSupportedException(com.google.gerrit.exceptions.MergeWithConflictsNotSupportedException) ChangeAlreadyMergedException(com.google.gerrit.server.submit.ChangeAlreadyMergedException)

Aggregations

Preconditions.checkArgument (com.google.common.base.Preconditions.checkArgument)1 Preconditions.checkState (com.google.common.base.Preconditions.checkState)1 Strings (com.google.common.base.Strings)1 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableSet (com.google.common.collect.ImmutableSet)1 ImmutableSet.toImmutableSet (com.google.common.collect.ImmutableSet.toImmutableSet)1 ImmutableSortedSet (com.google.common.collect.ImmutableSortedSet)1 ImmutableSortedSet.toImmutableSortedSet (com.google.common.collect.ImmutableSortedSet.toImmutableSortedSet)1 Iterables (com.google.common.collect.Iterables)1 Sets (com.google.common.collect.Sets)1 FluentLogger (com.google.common.flogger.FluentLogger)1 FooterConstants (com.google.gerrit.common.FooterConstants)1 Nullable (com.google.gerrit.common.Nullable)1 Account (com.google.gerrit.entities.Account)1 BooleanProjectConfig (com.google.gerrit.entities.BooleanProjectConfig)1 BranchNameKey (com.google.gerrit.entities.BranchNameKey)1 Change (com.google.gerrit.entities.Change)1 LabelId (com.google.gerrit.entities.LabelId)1 LabelType (com.google.gerrit.entities.LabelType)1 PatchSet (com.google.gerrit.entities.PatchSet)1