Search in sources :

Example 1 with RepositoryNotFoundException

use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.

the class ChangeFinder method find.

/**
   * Find changes matching the given identifier.
   *
   * @param id change identifier, either a numeric ID, a Change-Id, or project~branch~id triplet.
   * @param user user to wrap in controls.
   * @return possibly-empty list of controls for all matching changes, corresponding to the given
   *     user; may or may not be visible.
   * @throws OrmException if an error occurred querying the database.
   */
public List<ChangeControl> find(String id, CurrentUser user) throws OrmException {
    if (id.isEmpty()) {
        return Collections.emptyList();
    }
    // Use the index to search for changes, but don't return any stored fields,
    // to force rereading in case the index is stale.
    InternalChangeQuery query = queryProvider.get().noFields();
    int numTwiddles = 0;
    for (char c : id.toCharArray()) {
        if (c == '~') {
            numTwiddles++;
        }
    }
    if (numTwiddles == 1) {
        // Try project~numericChangeId
        String project = id.substring(0, id.indexOf('~'));
        Integer n = Ints.tryParse(id.substring(project.length() + 1));
        if (n != null) {
            Change.Id changeId = new Change.Id(n);
            try {
                return ImmutableList.of(changeControlFactory.controlFor(reviewDb.get(), Project.NameKey.parse(project), changeId, user));
            } catch (NoSuchChangeException e) {
                return Collections.emptyList();
            } catch (IllegalArgumentException e) {
                String changeNotFound = String.format("change %s not found in ReviewDb", changeId);
                String projectNotFound = String.format("passed project %s when creating ChangeNotes for %s, but actual project is", project, changeId);
                if (e.getMessage().equals(changeNotFound) || e.getMessage().startsWith(projectNotFound)) {
                    return Collections.emptyList();
                }
                throw e;
            } catch (OrmException e) {
                // other OrmExceptions (failure in the persistence layer).
                if (Throwables.getRootCause(e) instanceof RepositoryNotFoundException) {
                    return Collections.emptyList();
                }
                throw e;
            }
        }
    } else if (numTwiddles == 2) {
        // Try change triplet
        Optional<ChangeTriplet> triplet = ChangeTriplet.parse(id);
        if (triplet.isPresent()) {
            return asChangeControls(query.byBranchKey(triplet.get().branch(), triplet.get().id()), user);
        }
    }
    // Try numeric changeId
    if (id.charAt(0) != '0') {
        Integer n = Ints.tryParse(id);
        if (n != null) {
            return asChangeControls(query.byLegacyChangeId(new Change.Id(n)), user);
        }
    }
    // Try isolated changeId
    return asChangeControls(query.byKeyPrefix(id), user);
}
Also used : Optional(java.util.Optional) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) InternalChangeQuery(com.google.gerrit.server.query.change.InternalChangeQuery) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) OrmException(com.google.gwtorm.server.OrmException)

Example 2 with RepositoryNotFoundException

use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.

the class GetContent method getMessage.

private String getMessage(ChangeNotes notes) throws OrmException, IOException {
    Change.Id changeId = notes.getChangeId();
    PatchSet ps = psUtil.current(db.get(), notes);
    if (ps == null) {
        throw new NoSuchChangeException(changeId);
    }
    try (Repository git = gitManager.openRepository(notes.getProjectName());
        RevWalk revWalk = new RevWalk(git)) {
        RevCommit commit = revWalk.parseCommit(ObjectId.fromString(ps.getRevision().get()));
        return commit.getFullMessage();
    } catch (RepositoryNotFoundException e) {
        throw new NoSuchChangeException(changeId, e);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) NoSuchChangeException(com.google.gerrit.server.project.NoSuchChangeException) PatchSet(com.google.gerrit.reviewdb.client.PatchSet) Change(com.google.gerrit.reviewdb.client.Change) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Example 3 with RepositoryNotFoundException

use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gerrit by GerritCodeReview.

the class SetHead method apply.

@Override
public String apply(final ProjectResource rsrc, Input input) throws AuthException, ResourceNotFoundException, BadRequestException, UnprocessableEntityException, IOException {
    if (!rsrc.getControl().isOwner()) {
        throw new AuthException("restricted to project owner");
    }
    if (input == null || Strings.isNullOrEmpty(input.ref)) {
        throw new BadRequestException("ref required");
    }
    String ref = RefNames.fullName(input.ref);
    try (Repository repo = repoManager.openRepository(rsrc.getNameKey())) {
        Map<String, Ref> cur = repo.getRefDatabase().exactRef(Constants.HEAD, ref);
        if (!cur.containsKey(ref)) {
            throw new UnprocessableEntityException(String.format("Ref Not Found: %s", ref));
        }
        final String oldHead = cur.get(Constants.HEAD).getTarget().getName();
        final String newHead = ref;
        if (!oldHead.equals(newHead)) {
            final RefUpdate u = repo.updateRef(Constants.HEAD, true);
            u.setRefLogIdent(identifiedUser.get().newRefLogIdent());
            RefUpdate.Result res = u.link(newHead);
            switch(res) {
                case NO_CHANGE:
                case RENAMED:
                case FORCED:
                case NEW:
                    break;
                case FAST_FORWARD:
                case IO_FAILURE:
                case LOCK_FAILURE:
                case NOT_ATTEMPTED:
                case REJECTED:
                case REJECTED_CURRENT_BRANCH:
                default:
                    throw new IOException("Setting HEAD failed with " + res);
            }
            fire(rsrc.getNameKey(), oldHead, newHead);
        }
        return ref;
    } catch (RepositoryNotFoundException e) {
        throw new ResourceNotFoundException(rsrc.getName());
    }
}
Also used : UnprocessableEntityException(com.google.gerrit.extensions.restapi.UnprocessableEntityException) Repository(org.eclipse.jgit.lib.Repository) Ref(org.eclipse.jgit.lib.Ref) AuthException(com.google.gerrit.extensions.restapi.AuthException) BadRequestException(com.google.gerrit.extensions.restapi.BadRequestException) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ResourceNotFoundException(com.google.gerrit.extensions.restapi.ResourceNotFoundException) RefUpdate(org.eclipse.jgit.lib.RefUpdate)

Example 4 with RepositoryNotFoundException

use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gitiles by GerritCodeReview.

the class RepositoryFilter method doFilter.

@Override
public void doFilter(HttpServletRequest req, HttpServletResponse res, FilterChain chain) throws IOException, ServletException {
    try {
        String repo = ViewFilter.trimLeadingSlash(getRegexGroup(req, 1));
        try (Repository git = resolver.open(req, repo)) {
            req.setAttribute(ATTRIBUTE_REPOSITORY, git);
            chain.doFilter(req, res);
        } catch (RepositoryNotFoundException e) {
            // Drop through the rest of the chain. ViewFilter will pass this
            // to HostIndexServlet which will attempt to list repositories
            // or send SC_NOT_FOUND there.
            chain.doFilter(req, res);
        } catch (ServiceMayNotContinueException e) {
            sendError(req, res, e.getStatusCode(), e.getMessage());
        } finally {
            req.removeAttribute(ATTRIBUTE_REPOSITORY);
        }
    } catch (ServiceNotEnabledException e) {
        sendError(req, res, SC_FORBIDDEN);
    } catch (ServiceNotAuthorizedException e) {
        res.sendError(SC_UNAUTHORIZED);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ServiceNotEnabledException(org.eclipse.jgit.transport.resolver.ServiceNotEnabledException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) ServiceMayNotContinueException(org.eclipse.jgit.transport.ServiceMayNotContinueException) ServiceNotAuthorizedException(org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException)

Example 5 with RepositoryNotFoundException

use of org.eclipse.jgit.errors.RepositoryNotFoundException in project gitiles by GerritCodeReview.

the class RootedDocServlet method service.

@Override
public void service(HttpServletRequest req, HttpServletResponse res) throws IOException, ServletException {
    try (Repository repo = resolver.open(req, null);
        RevWalk rw = new RevWalk(repo)) {
        ObjectId id = repo.resolve(BRANCH);
        if (id == null) {
            res.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        RevObject obj = rw.peel(rw.parseAny(id));
        if (!(obj instanceof RevCommit)) {
            res.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        req.setAttribute(ATTRIBUTE_REPOSITORY, repo);
        ViewFilter.setView(req, GitilesView.rootedDoc().setHostName(req.getServerName()).setServletPath(req.getContextPath() + req.getServletPath()).setRevision(BRANCH, obj).setPathPart(req.getPathInfo()).build());
        docServlet.service(req, res);
    } catch (RepositoryNotFoundException | ServiceNotAuthorizedException | ServiceNotEnabledException e) {
        log.error(String.format("cannot open repository for %s", req.getServerName()), e);
        res.sendError(HttpServletResponse.SC_NOT_FOUND);
    } finally {
        ViewFilter.removeView(req);
        req.removeAttribute(ATTRIBUTE_REPOSITORY);
    }
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ServiceNotEnabledException(org.eclipse.jgit.transport.resolver.ServiceNotEnabledException) ObjectId(org.eclipse.jgit.lib.ObjectId) RevObject(org.eclipse.jgit.revwalk.RevObject) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) RevWalk(org.eclipse.jgit.revwalk.RevWalk) ServiceNotAuthorizedException(org.eclipse.jgit.transport.resolver.ServiceNotAuthorizedException) RevCommit(org.eclipse.jgit.revwalk.RevCommit)

Aggregations

RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)36 Repository (org.eclipse.jgit.lib.Repository)20 IOException (java.io.IOException)17 Project (com.google.gerrit.reviewdb.client.Project)14 ConfigInvalidException (org.eclipse.jgit.errors.ConfigInvalidException)11 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)9 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)8 MetaDataUpdate (com.google.gerrit.server.git.MetaDataUpdate)7 ProjectConfig (com.google.gerrit.server.git.ProjectConfig)7 AuthException (com.google.gerrit.extensions.restapi.AuthException)5 Change (com.google.gerrit.reviewdb.client.Change)5 Ref (org.eclipse.jgit.lib.Ref)5 RevWalk (org.eclipse.jgit.revwalk.RevWalk)5 BadRequestException (com.google.gerrit.extensions.restapi.BadRequestException)4 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)4 NoSuchChangeException (com.google.gerrit.server.project.NoSuchChangeException)4 File (java.io.File)4 ObjectId (org.eclipse.jgit.lib.ObjectId)4 ProjectControl (com.google.gerrit.server.project.ProjectControl)3 ArrayList (java.util.ArrayList)3