Search in sources :

Example 6 with FileKey

use of org.eclipse.jgit.lib.RepositoryCache.FileKey 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)

Aggregations

FileKey (org.eclipse.jgit.lib.RepositoryCache.FileKey)6 File (java.io.File)4 IOException (java.io.IOException)4 Repository (org.eclipse.jgit.lib.Repository)4 RepositoryNotFoundException (org.eclipse.jgit.errors.RepositoryNotFoundException)3 Path (java.nio.file.Path)2 MoreObjects.firstNonNull (com.google.common.base.MoreObjects.firstNonNull)1 Strings (com.google.common.base.Strings)1 Project (com.google.gerrit.reviewdb.client.Project)1 DebugRenderer (com.google.gitiles.DebugRenderer)1 GitilesAccess (com.google.gitiles.GitilesAccess)1 GitilesServlet (com.google.gitiles.GitilesServlet)1 STATIC_PREFIX (com.google.gitiles.GitilesServlet.STATIC_PREFIX)1 PathServlet (com.google.gitiles.PathServlet)1 RepositoryDescription (com.google.gitiles.RepositoryDescription)1 RootedDocServlet (com.google.gitiles.RootedDocServlet)1 InetAddress (java.net.InetAddress)1 URISyntaxException (java.net.URISyntaxException)1 UnknownHostException (java.net.UnknownHostException)1 NoSuchFileException (java.nio.file.NoSuchFileException)1