Search in sources :

Example 46 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class ChangeIndexer method indexImpl.

private void indexImpl(ChangeData cd) {
    logger.atFine().log("Reindex change %d in index.", cd.getId().get());
    for (Index<?, ChangeData> i : getWriteIndexes()) {
        try (TraceTimer traceTimer = TraceContext.newTimer("Reindexing change in index", Metadata.builder().changeId(cd.getId().get()).patchSetId(cd.currentPatchSet().number()).indexVersion(i.getSchema().getVersion()).build())) {
            if (isFirstInsertForEntry.equals(IsFirstInsertForEntry.YES)) {
                i.insert(cd);
            } else {
                i.replace(cd);
            }
        } catch (RuntimeException e) {
            throw new StorageException(String.format("Failed to reindex change %d in index version %d (current patch set = %d)", cd.getId().get(), i.getSchema().getVersion(), cd.currentPatchSet().number()), e);
        }
    }
    fireChangeIndexedEvent(cd.project().get(), cd.getId().get());
}
Also used : TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) ChangeData(com.google.gerrit.server.query.change.ChangeData) StorageException(com.google.gerrit.exceptions.StorageException)

Example 47 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class IndexedChangeQuery method read.

@Override
public ResultSet<ChangeData> read() {
    final DataSource<ChangeData> currSource = source;
    final ResultSet<ChangeData> rs = currSource.read();
    return new ResultSet<>() {

        @Override
        public Iterator<ChangeData> iterator() {
            return Iterables.transform(rs, cd -> {
                fromSource.put(cd, currSource);
                return cd;
            }).iterator();
        }

        @Override
        public ImmutableList<ChangeData> toList() {
            ImmutableList<ChangeData> r = rs.toList();
            for (ChangeData cd : r) {
                fromSource.put(cd, currSource);
            }
            return r;
        }

        @Override
        public void close() {
            rs.close();
        }
    };
}
Also used : Iterables(com.google.common.collect.Iterables) ImmutableSet(com.google.common.collect.ImmutableSet) IndexConfig(com.google.gerrit.index.IndexConfig) Iterator(java.util.Iterator) QueryOptions(com.google.gerrit.index.QueryOptions) Set(java.util.Set) HashMap(java.util.HashMap) ResultSet(com.google.gerrit.index.query.ResultSet) Preconditions.checkState(com.google.common.base.Preconditions.checkState) IndexedQuery(com.google.gerrit.index.query.IndexedQuery) HashSet(java.util.HashSet) ChangeData(com.google.gerrit.server.query.change.ChangeData) CHANGE(com.google.gerrit.server.index.change.ChangeField.CHANGE) ImmutableList(com.google.common.collect.ImmutableList) PROJECT(com.google.gerrit.server.index.change.ChangeField.PROJECT) QueryParseException(com.google.gerrit.index.query.QueryParseException) Map(java.util.Map) IndexPredicate(com.google.gerrit.index.query.IndexPredicate) Change(com.google.gerrit.entities.Change) DataSource(com.google.gerrit.index.query.DataSource) VisibleForTesting(com.google.common.annotations.VisibleForTesting) ChangeDataSource(com.google.gerrit.server.query.change.ChangeDataSource) Predicate(com.google.gerrit.index.query.Predicate) Matchable(com.google.gerrit.index.query.Matchable) ResultSet(com.google.gerrit.index.query.ResultSet) ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 48 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class StalenessChecker method check.

/**
 * Returns a {@link StalenessCheckResult} with structured information about staleness of the
 * provided {@link com.google.gerrit.entities.Change.Id}.
 */
public StalenessCheckResult check(Change.Id id) {
    ChangeIndex i = indexes.getSearchIndex();
    if (i == null) {
        return StalenessCheckResult.notStale();
    }
    if (!i.getSchema().hasField(ChangeField.REF_STATE) || !i.getSchema().hasField(ChangeField.REF_STATE_PATTERN)) {
        // Index version not new enough for this check.
        return StalenessCheckResult.notStale();
    }
    Optional<ChangeData> result = i.get(id, IndexedChangeQuery.createOptions(indexConfig, 0, 1, FIELDS));
    if (!result.isPresent()) {
        return StalenessCheckResult.stale("Document %s missing from index", id);
    }
    ChangeData cd = result.get();
    return check(repoManager, id, cd.getRefStates(), parsePatterns(cd));
}
Also used : ChangeData(com.google.gerrit.server.query.change.ChangeData)

Example 49 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData 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;
}
Also used : Arrays(java.util.Arrays) ListMultimap(com.google.common.collect.ListMultimap) ProjectCache(com.google.gerrit.server.project.ProjectCache) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Inject(com.google.inject.Inject) PermissionBackend(com.google.gerrit.server.permissions.PermissionBackend) UpdateException(com.google.gerrit.server.update.UpdateException) RestModifyView(com.google.gerrit.extensions.restapi.RestModifyView) RevWalk(org.eclipse.jgit.revwalk.RevWalk) Config(org.eclipse.jgit.lib.Config) SubmitInput(com.google.gerrit.extensions.api.changes.SubmitInput) Map(java.util.Map) AuthException(com.google.gerrit.extensions.restapi.AuthException) UiAction(com.google.gerrit.extensions.webui.UiAction) EnumSet(java.util.EnumSet) GerritServerConfig(com.google.gerrit.server.config.GerritServerConfig) ImmutableMap(com.google.common.collect.ImmutableMap) SubmitType(com.google.gerrit.extensions.client.SubmitType) Collection(java.util.Collection) Set(java.util.Set) Collectors(java.util.stream.Collectors) Collectors.joining(java.util.stream.Collectors.joining) Sets(com.google.common.collect.Sets) BranchNameKey(com.google.gerrit.entities.BranchNameKey) SubmitTypeRecord(com.google.gerrit.entities.SubmitTypeRecord) ChangeData(com.google.gerrit.server.query.change.ChangeData) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) ChangeJson(com.google.gerrit.server.change.ChangeJson) FluentLogger(com.google.common.flogger.FluentLogger) Singleton(com.google.inject.Singleton) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) RevCommit(org.eclipse.jgit.revwalk.RevCommit) UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) ChangePermission(com.google.gerrit.server.permissions.ChangePermission) HashMap(java.util.HashMap) Response(com.google.gerrit.extensions.restapi.Response) HashSet(java.util.HashSet) Strings(com.google.common.base.Strings) MergeOp(com.google.gerrit.server.submit.MergeOp) ChangeResource(com.google.gerrit.server.change.ChangeResource) ChangeInfo(com.google.gerrit.extensions.common.ChangeInfo) UsedAt(com.google.gerrit.common.UsedAt) Change(com.google.gerrit.entities.Change) PatchSet(com.google.gerrit.entities.PatchSet) RestApiException(com.google.gerrit.extensions.restapi.RestApiException) ChangeUtil(com.google.gerrit.server.ChangeUtil) ProjectCache.illegalState(com.google.gerrit.server.project.ProjectCache.illegalState) CurrentUser(com.google.gerrit.server.CurrentUser) MergeSuperSet(com.google.gerrit.server.submit.MergeSuperSet) AccountResolver(com.google.gerrit.server.account.AccountResolver) ObjectIds.abbreviateName(com.google.gerrit.git.ObjectIds.abbreviateName) ChangeSet(com.google.gerrit.server.submit.ChangeSet) StorageException(com.google.gerrit.exceptions.StorageException) ProjectState(com.google.gerrit.server.project.ProjectState) MoreObjects(com.google.common.base.MoreObjects) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) ObjectId(org.eclipse.jgit.lib.ObjectId) Provider(com.google.inject.Provider) GitRepositoryManager(com.google.gerrit.server.git.GitRepositoryManager) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) RevisionResource(com.google.gerrit.server.change.RevisionResource) ProjectUtil(com.google.gerrit.server.ProjectUtil) Project(com.google.gerrit.entities.Project) PatchSetUtil(com.google.gerrit.server.PatchSetUtil) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) ChangeData(com.google.gerrit.server.query.change.ChangeData) BranchNameKey(com.google.gerrit.entities.BranchNameKey) ObjectId(org.eclipse.jgit.lib.ObjectId) HashSet(java.util.HashSet) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 50 with ChangeData

use of com.google.gerrit.server.query.change.ChangeData in project gerrit by GerritCodeReview.

the class Submit method findCommits.

private HashMap<Change.Id, RevCommit> findCommits(Collection<ChangeData> changes, Project.NameKey project) throws IOException {
    HashMap<Change.Id, RevCommit> commits = new HashMap<>();
    try (Repository repo = repoManager.openRepository(project);
        RevWalk walk = new RevWalk(repo)) {
        for (ChangeData change : changes) {
            RevCommit commit = walk.parseCommit(psUtil.current(change.notes()).commitId());
            commits.put(change.getId(), commit);
        }
    }
    return commits;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) HashMap(java.util.HashMap) ObjectId(org.eclipse.jgit.lib.ObjectId) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

ChangeData (com.google.gerrit.server.query.change.ChangeData)208 Test (org.junit.Test)75 Change (com.google.gerrit.entities.Change)58 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)57 RevCommit (org.eclipse.jgit.revwalk.RevCommit)53 ObjectId (org.eclipse.jgit.lib.ObjectId)45 ArrayList (java.util.ArrayList)41 IOException (java.io.IOException)33 PushOneCommit (com.google.gerrit.acceptance.PushOneCommit)27 PatchSet (com.google.gerrit.entities.PatchSet)26 StorageException (com.google.gerrit.exceptions.StorageException)25 Inject (com.google.inject.Inject)25 HashMap (java.util.HashMap)25 Map (java.util.Map)24 ChangeNotes (com.google.gerrit.server.notedb.ChangeNotes)23 List (java.util.List)23 Project (com.google.gerrit.entities.Project)21 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)21 OrmException (com.google.gwtorm.server.OrmException)20 Repository (org.eclipse.jgit.lib.Repository)20