Search in sources :

Example 11 with RepositoryNotFoundException

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

the class AdminSetParent method run.

@Override
protected void run() throws Failure {
    if (oldParent == null && children.isEmpty()) {
        throw die("child projects have to be specified as " + "arguments or the --children-of option has to be set");
    }
    if (oldParent == null && !excludedChildren.isEmpty()) {
        throw die("--exclude can only be used together with --children-of");
    }
    final StringBuilder err = new StringBuilder();
    final Set<Project.NameKey> grandParents = new HashSet<>();
    grandParents.add(allProjectsName);
    if (newParent != null) {
        newParentKey = newParent.getProject().getNameKey();
        // Catalog all grandparents of the "parent", we want to
        // catch a cycle in the parent pointers before it occurs.
        //
        Project.NameKey gp = newParent.getProject().getParent();
        while (gp != null && grandParents.add(gp)) {
            final ProjectState s = projectCache.get(gp);
            if (s != null) {
                gp = s.getProject().getParent();
            } else {
                break;
            }
        }
    }
    final List<Project.NameKey> childProjects = new ArrayList<>();
    for (final ProjectControl pc : children) {
        childProjects.add(pc.getProject().getNameKey());
    }
    if (oldParent != null) {
        try {
            childProjects.addAll(getChildrenForReparenting(oldParent));
        } catch (PermissionBackendException e) {
            throw new Failure(1, "permissions unavailable", e);
        }
    }
    for (final Project.NameKey nameKey : childProjects) {
        final String name = nameKey.get();
        if (allProjectsName.equals(nameKey)) {
            // Don't allow the wild card project to have a parent.
            //
            err.append("error: Cannot set parent of '").append(name).append("'\n");
            continue;
        }
        if (grandParents.contains(nameKey) || nameKey.equals(newParentKey)) {
            // Try to avoid creating a cycle in the parent pointers.
            //
            err.append("error: Cycle exists between '").append(name).append("' and '").append(newParentKey != null ? newParentKey.get() : allProjectsName.get()).append("'\n");
            continue;
        }
        try (MetaDataUpdate md = metaDataUpdateFactory.create(nameKey)) {
            ProjectConfig config = ProjectConfig.read(md);
            config.getProject().setParentName(newParentKey);
            md.setMessage("Inherit access from " + (newParentKey != null ? newParentKey.get() : allProjectsName.get()) + "\n");
            config.commit(md);
        } catch (RepositoryNotFoundException notFound) {
            err.append("error: Project ").append(name).append(" not found\n");
        } catch (IOException | ConfigInvalidException e) {
            final String msg = "Cannot update project " + name;
            log.error(msg, e);
            err.append("error: ").append(msg).append("\n");
        }
        projectCache.evict(nameKey);
    }
    if (err.length() > 0) {
        while (err.charAt(err.length() - 1) == '\n') {
            err.setLength(err.length() - 1);
        }
        throw die(err.toString());
    }
}
Also used : ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ArrayList(java.util.ArrayList) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) ProjectControl(com.google.gerrit.server.project.ProjectControl) ProjectConfig(com.google.gerrit.server.git.ProjectConfig) Project(com.google.gerrit.reviewdb.client.Project) ProjectState(com.google.gerrit.server.project.ProjectState) HashSet(java.util.HashSet) MetaDataUpdate(com.google.gerrit.server.git.MetaDataUpdate)

Example 12 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 13 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 14 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 15 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)

Aggregations

RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)37 Repository (org.eclipse.jgit.lib.Repository)20 IOException (java.io.IOException)18 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