Search in sources :

Example 21 with RepositoryNotFoundException

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

the class DevServer method createRootedDocServlet.

private Servlet createRootedDocServlet(DebugRenderer renderer, String docRoot) {
    File docRepo = new File(docRoot);
    FileKey repoKey = FileKey.exact(docRepo, FS.DETECTED);
    RepositoryResolver<HttpServletRequest> resolver = (req, name) -> {
        try {
            return RepositoryCache.open(repoKey, true);
        } catch (IOException e) {
            throw new RepositoryNotFoundException(repoKey.getFile(), e);
        }
    };
    return new RootedDocServlet(resolver, new RootedDocAccess(docRepo), renderer);
}
Also used : ServletContextHandler(org.eclipse.jetty.servlet.ServletContextHandler) Handler(org.eclipse.jetty.server.Handler) NoSuchFileException(java.nio.file.NoSuchFileException) Arrays(java.util.Arrays) ResourceHandler(org.eclipse.jetty.server.handler.ResourceHandler) PathResource(org.eclipse.jetty.util.resource.PathResource) ConfigInvalidException(org.eclipse.jgit.errors.ConfigInvalidException) ContextHandlerCollection(org.eclipse.jetty.server.handler.ContextHandlerCollection) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) URISyntaxException(java.net.URISyntaxException) GitilesAccess(com.google.gitiles.GitilesAccess) LoggerFactory(org.slf4j.LoggerFactory) InetAddress(java.net.InetAddress) Strings(com.google.common.base.Strings) Config(org.eclipse.jgit.lib.Config) HttpServletRequest(javax.servlet.http.HttpServletRequest) Map(java.util.Map) RepositoryDescription(com.google.gitiles.RepositoryDescription) Path(java.nio.file.Path) Server(org.eclipse.jetty.server.Server) GitilesServlet(com.google.gitiles.GitilesServlet) RepositoryCache(org.eclipse.jgit.lib.RepositoryCache) Logger(org.slf4j.Logger) Servlet(javax.servlet.Servlet) ServletHolder(org.eclipse.jetty.servlet.ServletHolder) PathServlet(com.google.gitiles.PathServlet) Set(java.util.Set) IOException(java.io.IOException) Constants(org.eclipse.jgit.lib.Constants) RepositoryResolver(org.eclipse.jgit.transport.resolver.RepositoryResolver) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) STATIC_PREFIX(com.google.gitiles.GitilesServlet.STATIC_PREFIX) UnknownHostException(java.net.UnknownHostException) FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) File(java.io.File) RootedDocServlet(com.google.gitiles.RootedDocServlet) FileBasedConfig(org.eclipse.jgit.storage.file.FileBasedConfig) Paths(java.nio.file.Paths) MoreObjects.firstNonNull(com.google.common.base.MoreObjects.firstNonNull) FS(org.eclipse.jgit.util.FS) DebugRenderer(com.google.gitiles.DebugRenderer) Collections(java.util.Collections) HttpServletRequest(javax.servlet.http.HttpServletRequest) FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) IOException(java.io.IOException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) File(java.io.File) RootedDocServlet(com.google.gitiles.RootedDocServlet)

Example 22 with RepositoryNotFoundException

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

the class DefaultAccess method scanRepositories.

private Collection<Repository> scanRepositories(File basePath, String prefix, HttpServletRequest req) throws IOException {
    List<Repository> repos = Lists.newArrayList();
    Queue<File> todo = initScan(basePath, prefix);
    while (!todo.isEmpty()) {
        File file = todo.remove();
        try {
            repos.add(resolver.open(req, getRelativePath(file.getPath())));
        } catch (RepositoryNotFoundException e) {
            File[] children = file.listFiles();
            if (children != null) {
                Collections.addAll(todo, children);
            }
        } catch (ServiceNotEnabledException e) {
            throw new IOException(e);
        }
    }
    return repos;
}
Also used : Repository(org.eclipse.jgit.lib.Repository) ServiceNotEnabledException(org.eclipse.jgit.transport.resolver.ServiceNotEnabledException) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 23 with RepositoryNotFoundException

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

the class LocalDiskRepositoryManager method openRepository.

private Repository openRepository(Path path, Project.NameKey name) throws RepositoryNotFoundException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }
    File gitDir = path.resolve(name.get()).toFile();
    if (!names.contains(name)) {
        //
        if (!name.get().endsWith(Constants.DOT_GIT_EXT)) {
            if (FileKey.resolve(gitDir, FS.DETECTED) != null) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        } else {
            final File directory = gitDir;
            if (FileKey.isGitRepository(new File(directory, Constants.DOT_GIT), FS.DETECTED)) {
                onCreateProject(name);
            } else if (FileKey.isGitRepository(new File(directory.getParentFile(), directory.getName() + Constants.DOT_GIT_EXT), FS.DETECTED)) {
                onCreateProject(name);
            } else {
                throw new RepositoryNotFoundException(gitDir);
            }
        }
    }
    final FileKey loc = FileKey.lenient(gitDir, FS.DETECTED);
    try {
        return RepositoryCache.open(loc);
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot open repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}
Also used : FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 24 with RepositoryNotFoundException

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

the class LocalDiskRepositoryManager method createRepository.

@Override
public Repository createRepository(Project.NameKey name) throws RepositoryNotFoundException, RepositoryCaseMismatchException, IOException {
    Path path = getBasePath(name);
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }
    File dir = FileKey.resolve(path.resolve(name.get()).toFile(), FS.DETECTED);
    FileKey loc;
    if (dir != null) {
        // Already exists on disk, use the repository we found.
        //
        Project.NameKey onDiskName = getProjectName(path, dir.getCanonicalFile().toPath());
        onCreateProject(onDiskName);
        loc = FileKey.exact(dir, FS.DETECTED);
        if (!names.contains(name)) {
            throw new RepositoryCaseMismatchException(name);
        }
    } else {
        // It doesn't exist under any of the standard permutations
        // of the repository name, so prefer the standard bare name.
        //
        String n = name.get() + Constants.DOT_GIT_EXT;
        loc = FileKey.exact(path.resolve(n).toFile(), FS.DETECTED);
    }
    try {
        Repository db = RepositoryCache.open(loc, false);
        db.create(true);
        StoredConfig config = db.getConfig();
        config.setBoolean(ConfigConstants.CONFIG_CORE_SECTION, null, ConfigConstants.CONFIG_KEY_LOGALLREFUPDATES, true);
        config.save();
        // JGit only writes to the reflog for refs/meta/config if the log file
        // already exists.
        //
        File metaConfigLog = new File(db.getDirectory(), "logs/" + RefNames.REFS_CONFIG);
        if (!metaConfigLog.getParentFile().mkdirs() || !metaConfigLog.createNewFile()) {
            log.error(String.format("Failed to create ref log for %s in repository %s", RefNames.REFS_CONFIG, name));
        }
        onCreateProject(name);
        return db;
    } catch (IOException e1) {
        final RepositoryNotFoundException e2;
        e2 = new RepositoryNotFoundException("Cannot create repository " + name);
        e2.initCause(e1);
        throw e2;
    }
}
Also used : Path(java.nio.file.Path) StoredConfig(org.eclipse.jgit.lib.StoredConfig) FileKey(org.eclipse.jgit.lib.RepositoryCache.FileKey) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) IOException(java.io.IOException) File(java.io.File)

Example 25 with RepositoryNotFoundException

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

the class GarbageCollection method run.

public GarbageCollectionResult run(List<Project.NameKey> projectNames, boolean aggressive, PrintWriter writer) {
    GarbageCollectionResult result = new GarbageCollectionResult();
    Set<Project.NameKey> projectsToGc = gcQueue.addAll(projectNames);
    for (Project.NameKey projectName : Sets.difference(Sets.newHashSet(projectNames), projectsToGc)) {
        result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.GC_ALREADY_SCHEDULED, projectName));
    }
    for (Project.NameKey p : projectsToGc) {
        try (Repository repo = repoManager.openRepository(p)) {
            logGcConfiguration(p, repo, aggressive);
            print(writer, "collecting garbage for \"" + p + "\":\n");
            GarbageCollectCommand gc = Git.wrap(repo).gc();
            gc.setAggressive(aggressive);
            logGcInfo(p, "before:", gc.getStatistics());
            gc.setProgressMonitor(writer != null ? new TextProgressMonitor(writer) : NullProgressMonitor.INSTANCE);
            Properties statistics = gc.call();
            logGcInfo(p, "after: ", statistics);
            print(writer, "done.\n\n");
            fire(p, statistics);
        } catch (RepositoryNotFoundException e) {
            logGcError(writer, p, e);
            result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.REPOSITORY_NOT_FOUND, p));
        } catch (Exception e) {
            logGcError(writer, p, e);
            result.addError(new GarbageCollectionResult.Error(GarbageCollectionResult.Error.Type.GC_FAILED, p));
        } finally {
            gcQueue.gcFinished(p);
        }
    }
    return result;
}
Also used : GarbageCollectCommand(org.eclipse.jgit.api.GarbageCollectCommand) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) TextProgressMonitor(org.eclipse.jgit.lib.TextProgressMonitor) Properties(java.util.Properties) RepositoryNotFoundException(org.eclipse.jgit.errors.RepositoryNotFoundException) Project(com.google.gerrit.reviewdb.client.Project) Repository(org.eclipse.jgit.lib.Repository) GarbageCollectionResult(com.google.gerrit.common.data.GarbageCollectionResult)

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