Search in sources :

Example 1 with VisibleRefFilter

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

the class RefAdvertisementIT method uploadPackNoSearchingChangeCacheImpl.

@Test
public void uploadPackNoSearchingChangeCacheImpl() throws Exception {
    allow(Permission.READ, REGISTERED_USERS, "refs/heads/*");
    setApiUser(user);
    try (Repository repo = repoManager.openRepository(project)) {
        assertRefs(repo, new VisibleRefFilter(tagCache, notesFactory, null, repo, projectControl(), db, true), // Can't use stored values from the index so DB must be enabled.
        false, "HEAD", r1 + "1", r1 + "meta", r2 + "1", r2 + "meta", r3 + "1", r3 + "meta", r4 + "1", r4 + "meta", "refs/heads/branch", "refs/heads/master", "refs/tags/branch-tag", "refs/tags/master-tag");
    }
}
Also used : TestRepository(org.eclipse.jgit.junit.TestRepository) Repository(org.eclipse.jgit.lib.Repository) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter) AbstractDaemonTest(com.google.gerrit.acceptance.AbstractDaemonTest) Test(org.junit.Test)

Example 2 with VisibleRefFilter

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

the class ProjectControl method isMergedIntoVisibleRef.

boolean isMergedIntoVisibleRef(Repository repo, ReviewDb db, RevWalk rw, RevCommit commit, Collection<Ref> unfilteredRefs) throws IOException {
    VisibleRefFilter filter = new VisibleRefFilter(tagCache, changeNotesFactory, changeCache, repo, this, db, true);
    Map<String, Ref> m = Maps.newHashMapWithExpectedSize(unfilteredRefs.size());
    for (Ref r : unfilteredRefs) {
        m.put(r.getName(), r);
    }
    Map<String, Ref> refs = filter.filter(m, true);
    return !refs.isEmpty() && IncludedInResolver.includedInOne(repo, rw, commit, refs.values());
}
Also used : ForRef(com.google.gerrit.server.permissions.PermissionBackend.ForRef) Ref(org.eclipse.jgit.lib.Ref) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter)

Example 3 with VisibleRefFilter

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

the class Receive method runImpl.

@Override
protected void runImpl() throws IOException, Failure {
    if (!projectControl.canRunReceivePack()) {
        throw new Failure(1, "fatal: receive-pack not permitted on this server");
    }
    final ReceiveCommits receive = factory.create(projectControl, repo).getReceiveCommits();
    Capable r = receive.canUpload();
    if (r != Capable.OK) {
        throw die(r.getMessage());
    }
    receive.init();
    receive.addReviewers(reviewerId);
    receive.addExtraCC(ccId);
    ReceivePack rp = receive.getReceivePack();
    try {
        rp.receive(in, out, err);
        session.setPeerAgent(rp.getPeerUserAgent());
    } catch (UnpackException badStream) {
        // we want to present this error to the user
        if (badStream.getCause() instanceof TooLargeObjectInPackException) {
            StringBuilder msg = new StringBuilder();
            msg.append("Receive error on project \"").append(projectControl.getProject().getName()).append("\"");
            msg.append(" (user ");
            msg.append(currentUser.getAccount().getUserName());
            msg.append(" account ");
            msg.append(currentUser.getAccountId());
            msg.append("): ");
            msg.append(badStream.getCause().getMessage());
            log.info(msg.toString());
            throw new UnloggedFailure(128, "error: " + badStream.getCause().getMessage());
        }
        // This may have been triggered by branch level access controls.
        // Log what the heck is going on, as detailed as we can.
        //
        StringBuilder msg = new StringBuilder();
        msg.append("Unpack error on project \"").append(projectControl.getProject().getName()).append("\":\n");
        msg.append("  AdvertiseRefsHook: ").append(rp.getAdvertiseRefsHook());
        if (rp.getAdvertiseRefsHook() == AdvertiseRefsHook.DEFAULT) {
            msg.append("DEFAULT");
        } else if (rp.getAdvertiseRefsHook() instanceof VisibleRefFilter) {
            msg.append("VisibleRefFilter");
        } else {
            msg.append(rp.getAdvertiseRefsHook().getClass());
        }
        msg.append("\n");
        if (rp.getAdvertiseRefsHook() instanceof VisibleRefFilter) {
            Map<String, Ref> adv = rp.getAdvertisedRefs();
            msg.append("  Visible references (").append(adv.size()).append("):\n");
            for (Ref ref : adv.values()) {
                msg.append("  - ").append(ref.getObjectId().abbreviate(8).name()).append(" ").append(ref.getName()).append("\n");
            }
            Map<String, Ref> allRefs = rp.getRepository().getRefDatabase().getRefs(RefDatabase.ALL);
            List<Ref> hidden = new ArrayList<>();
            for (Ref ref : allRefs.values()) {
                if (!adv.containsKey(ref.getName())) {
                    hidden.add(ref);
                }
            }
            msg.append("  Hidden references (").append(hidden.size()).append("):\n");
            for (Ref ref : hidden) {
                msg.append("  - ").append(ref.getObjectId().abbreviate(8).name()).append(" ").append(ref.getName()).append("\n");
            }
        }
        IOException detail = new IOException(msg.toString(), badStream);
        throw new Failure(128, "fatal: Unpack error, check server log", detail);
    }
}
Also used : TooLargeObjectInPackException(org.eclipse.jgit.errors.TooLargeObjectInPackException) IOException(java.io.IOException) AsyncReceiveCommits(com.google.gerrit.server.git.AsyncReceiveCommits) ReceiveCommits(com.google.gerrit.server.git.ReceiveCommits) Ref(org.eclipse.jgit.lib.Ref) Capable(com.google.gerrit.common.data.Capable) UnpackException(org.eclipse.jgit.errors.UnpackException) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter) ArrayList(java.util.ArrayList) List(java.util.List) ReceivePack(org.eclipse.jgit.transport.ReceivePack) Map(java.util.Map)

Example 4 with VisibleRefFilter

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

the class Upload method runImpl.

@Override
protected void runImpl() throws IOException, Failure {
    if (!projectControl.canRunUploadPack()) {
        throw new Failure(1, "fatal: upload-pack not permitted on this server");
    }
    final UploadPack up = new UploadPack(repo);
    up.setAdvertiseRefsHook(new VisibleRefFilter(tagCache, changeNotesFactory, changeCache, repo, projectControl, db, true));
    up.setPackConfig(config.getPackConfig());
    up.setTimeout(config.getTimeout());
    up.setPostUploadHook(PostUploadHookChain.newChain(Lists.newArrayList(postUploadHooks)));
    List<PreUploadHook> allPreUploadHooks = Lists.newArrayList(preUploadHooks);
    allPreUploadHooks.add(uploadValidatorsFactory.create(project, repo, session.getRemoteAddressAsString()));
    up.setPreUploadHook(PreUploadHookChain.newChain(allPreUploadHooks));
    try {
        up.upload(in, out, err);
        session.setPeerAgent(up.getPeerUserAgent());
    } catch (UploadValidationException e) {
        // internal server error to the client.
        if (!e.isOutput()) {
            up.sendMessage(e.getMessage());
        }
    }
}
Also used : PreUploadHook(org.eclipse.jgit.transport.PreUploadHook) UploadValidationException(com.google.gerrit.server.git.validators.UploadValidationException) UploadPack(org.eclipse.jgit.transport.UploadPack) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter)

Example 5 with VisibleRefFilter

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

the class LsUserRefs method run.

@Override
protected void run() throws Failure {
    Account userAccount;
    try {
        userAccount = accountResolver.find(db, userName);
    } catch (OrmException e) {
        throw die(e);
    }
    if (userAccount == null) {
        stdout.print("No single user could be found when searching for: " + userName + '\n');
        stdout.flush();
        return;
    }
    IdentifiedUser user = userFactory.create(userAccount.getId());
    ProjectControl userProjectControl = projectControl.forUser(user);
    try (Repository repo = repoManager.openRepository(userProjectControl.getProject().getNameKey())) {
        try {
            Map<String, Ref> refsMap = new VisibleRefFilter(tagCache, changeNotesFactory, changeCache, repo, userProjectControl, db, true).filter(repo.getRefDatabase().getRefs(ALL), false);
            for (final String ref : refsMap.keySet()) {
                if (!onlyRefsHeads || ref.startsWith(RefNames.REFS_HEADS)) {
                    stdout.println(ref);
                }
            }
        } catch (IOException e) {
            throw new Failure(1, "fatal: Error reading refs: '" + projectControl.getProject().getNameKey(), e);
        }
    } catch (RepositoryNotFoundException e) {
        throw die("'" + projectControl.getProject().getNameKey() + "': not a git archive");
    } catch (IOException e) {
        throw die("Error opening: '" + projectControl.getProject().getNameKey());
    }
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) OrmException(com.google.gwtorm.server.OrmException) VisibleRefFilter(com.google.gerrit.server.git.VisibleRefFilter) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IdentifiedUser(com.google.gerrit.server.IdentifiedUser) ProjectControl(com.google.gerrit.server.project.ProjectControl)

Aggregations

VisibleRefFilter (com.google.gerrit.server.git.VisibleRefFilter)5 Ref (org.eclipse.jgit.lib.Ref)3 IOException (java.io.IOException)2 Repository (org.eclipse.jgit.lib.Repository)2 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)1 Capable (com.google.gerrit.common.data.Capable)1 Account (com.google.gerrit.reviewdb.client.Account)1 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)1 AsyncReceiveCommits (com.google.gerrit.server.git.AsyncReceiveCommits)1 ReceiveCommits (com.google.gerrit.server.git.ReceiveCommits)1 UploadValidationException (com.google.gerrit.server.git.validators.UploadValidationException)1 ForRef (com.google.gerrit.server.permissions.PermissionBackend.ForRef)1 ProjectControl (com.google.gerrit.server.project.ProjectControl)1 OrmException (com.google.gwtorm.server.OrmException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Map (java.util.Map)1 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)1 TooLargeObjectInPackException (org.eclipse.jgit.errors.TooLargeObjectInPackException)1 UnpackException (org.eclipse.jgit.errors.UnpackException)1