Search in sources :

Example 1 with TagMatcher

use of com.google.gerrit.server.git.TagMatcher in project gerrit by GerritCodeReview.

the class DefaultRefFilter method filter.

/**
 * Filters given refs and tags by visibility.
 */
ImmutableList<Ref> filter(Collection<Ref> refs, Repository repo, RefFilterOptions opts) throws PermissionBackendException {
    visibleChangesCache = visibleChangesCacheFactory.create(projectControl, repo);
    logger.atFinest().log("Filter refs for repository %s by visibility (options = %s, refs = %s)", projectState.getNameKey(), opts, refs);
    logger.atFinest().log("Calling user: %s", user.getLoggableName());
    logger.atFinest().log("Groups: %s", lazy(() -> user.getEffectiveGroups().getKnownGroups()));
    logger.atFinest().log("auth.skipFullRefEvaluationIfAllRefsAreVisible = %s", skipFullRefEvaluationIfAllRefsAreVisible);
    logger.atFinest().log("Project state %s permits read = %s", projectState.getProject().getState(), projectState.statePermitsRead());
    // See if we can get away with a single, cheap ref evaluation.
    if (refs.size() == 1) {
        String refName = Iterables.getOnlyElement(refs).getName();
        if (opts.filterMeta() && isMetadata(refName)) {
            logger.atFinest().log("Filter out metadata ref %s", refName);
            return ImmutableList.of();
        }
        if (RefNames.isRefsChanges(refName)) {
            boolean isChangeRefVisisble = canSeeSingleChangeRef(repo, refName);
            if (isChangeRefVisisble) {
                logger.atFinest().log("Change ref %s is visible", refName);
                return ImmutableList.copyOf(refs);
            }
            logger.atFinest().log("Filter out non-visible change ref %s", refName);
            return ImmutableList.of();
        }
    }
    // Perform an initial ref filtering with all the refs the caller asked for. If we find tags that
    // we have to investigate separately (deferred tags) then perform a reachability check starting
    // from all visible branches (refs/heads/*).
    Result initialRefFilter = filterRefs(new ArrayList<>(refs), opts);
    ImmutableList.Builder<Ref> visibleRefs = ImmutableList.builder();
    visibleRefs.addAll(initialRefFilter.visibleRefs());
    if (!initialRefFilter.deferredTags().isEmpty()) {
        try (TraceTimer traceTimer = TraceContext.newTimer("Check visibility of deferred tags")) {
            Result allVisibleBranches = filterRefs(getTaggableRefs(repo), opts);
            checkState(allVisibleBranches.deferredTags().isEmpty(), "unexpected tags found when filtering refs/heads/* " + allVisibleBranches.deferredTags());
            TagMatcher tags = tagCache.get(projectState.getNameKey()).matcher(tagCache, repo, allVisibleBranches.visibleRefs());
            for (Ref tag : initialRefFilter.deferredTags()) {
                try {
                    if (tags.isReachable(tag)) {
                        logger.atFinest().log("Include reachable tag %s", tag.getName());
                        visibleRefs.add(tag);
                    } else {
                        logger.atFinest().log("Filter out non-reachable tag %s", tag.getName());
                    }
                } catch (IOException e) {
                    throw new PermissionBackendException(e);
                }
            }
        }
    }
    ImmutableList<Ref> visibleRefList = visibleRefs.build();
    logger.atFinest().log("visible refs = %s", visibleRefList);
    return visibleRefList;
}
Also used : Ref(org.eclipse.jgit.lib.Ref) ImmutableList(com.google.common.collect.ImmutableList) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) IOException(java.io.IOException) TagMatcher(com.google.gerrit.server.git.TagMatcher)

Aggregations

ImmutableList (com.google.common.collect.ImmutableList)1 TagMatcher (com.google.gerrit.server.git.TagMatcher)1 TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)1 IOException (java.io.IOException)1 Ref (org.eclipse.jgit.lib.Ref)1