Search in sources :

Example 1 with ReachabilityChecker

use of org.eclipse.jgit.revwalk.ReachabilityChecker in project gerrit by GerritCodeReview.

the class Reachable method fromRefs.

boolean fromRefs(Project.NameKey project, Repository repo, RevCommit commit, List<Ref> refs, Optional<CurrentUser> optionalUserProvider) {
    try (RevWalk rw = new RevWalk(repo)) {
        Collection<Ref> filtered = optionalUserProvider.map(permissionBackend::user).orElse(permissionBackend.currentUser()).project(project).filter(refs, repo, RefFilterOptions.defaults());
        Collection<RevCommit> visible = new ArrayList<>();
        for (Ref r : filtered) {
            try {
                visible.add(rw.parseCommit(r.getObjectId()));
            } catch (IncorrectObjectTypeException notCommit) {
                // is common in the Linux kernel or git.git repository.
                continue;
            } catch (MissingObjectException notHere) {
                // Log the problem with this branch, but keep processing.
                logger.atWarning().log("Reference %s in %s points to dangling object %s", r.getName(), repo.getDirectory(), r.getObjectId());
                continue;
            }
        }
        // from the reachability check, do the trace here:
        try (TraceTimer timer = TraceContext.newTimer("ReachabilityChecker.areAllReachable", Metadata.builder().projectName(project.get()).resourceCount(refs.size()).build())) {
            ReachabilityChecker checker = rw.getObjectReader().createReachabilityChecker(rw);
            Optional<RevCommit> unreachable = checker.areAllReachable(ImmutableList.of(rw.parseCommit(commit)), visible.stream());
            return !unreachable.isPresent();
        }
    } catch (IOException | PermissionBackendException e) {
        logger.atSevere().withCause(e).log("Cannot verify permissions to commit object %s in repository %s", commit.name(), project);
        return false;
    }
}
Also used : ReachabilityChecker(org.eclipse.jgit.revwalk.ReachabilityChecker) ArrayList(java.util.ArrayList) IncorrectObjectTypeException(org.eclipse.jgit.errors.IncorrectObjectTypeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) IOException(java.io.IOException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) MissingObjectException(org.eclipse.jgit.errors.MissingObjectException) Ref(org.eclipse.jgit.lib.Ref) TraceTimer(com.google.gerrit.server.logging.TraceContext.TraceTimer) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

TraceTimer (com.google.gerrit.server.logging.TraceContext.TraceTimer)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 IncorrectObjectTypeException (org.eclipse.jgit.errors.IncorrectObjectTypeException)1 MissingObjectException (org.eclipse.jgit.errors.MissingObjectException)1 Ref (org.eclipse.jgit.lib.Ref)1 ReachabilityChecker (org.eclipse.jgit.revwalk.ReachabilityChecker)1 RevCommit (org.eclipse.jgit.revwalk.RevCommit)1 RevWalk (org.eclipse.jgit.revwalk.RevWalk)1