Example usage for org.eclipse.jgit.lib RepositoryCache open

List of usage examples for org.eclipse.jgit.lib RepositoryCache open

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib RepositoryCache open.

Prototype

public static Repository open(Key location, boolean mustExist) throws IOException 

Source Link

Document

Open a repository, reusing a cached instance if possible.

Usage

From source file:com.cloudata.git.jgit.CloudGitRepositoryStore.java

License:Apache License

@Override
public Repository openRepository(GitUser user, GitRepository repo, boolean mustExist) throws IOException {
    if (user == null) {
        if (!repo.isPublicRead()) {
            return null;
        }//from  w w w.jav a 2s  .  c om
    } else {
        if (!user.canAccess(repo)) {
            return null;
        }
    }

    final CloudKey loc = new CloudKey(repo, this);
    return RepositoryCache.open(loc, mustExist);
}

From source file:com.gitblit.manager.RepositoryManager.java

License:Apache License

/**
 * Returns the JGit repository for the specified name.
 *
 * @param name//from  w w  w  . j a va 2s .  c o  m
 * @param logError
 * @return repository or null
 */
@Override
public Repository getRepository(String name, boolean logError) {
    String repositoryName = fixRepositoryName(name);

    if (isCollectingGarbage(repositoryName)) {
        logger.warn(
                MessageFormat.format("Rejecting request for {0}, busy collecting garbage!", repositoryName));
        return null;
    }

    File dir = FileKey.resolve(new File(repositoriesFolder, repositoryName), FS.DETECTED);
    if (dir == null)
        return null;

    Repository r = null;
    try {
        FileKey key = FileKey.exact(dir, FS.DETECTED);
        r = RepositoryCache.open(key, true);
    } catch (IOException e) {
        if (logError) {
            logger.error("GitBlit.getRepository(String) failed to find "
                    + new File(repositoriesFolder, repositoryName).getAbsolutePath());
        }
    }
    return r;
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManager.java

License:Apache License

private Repository createRepository(Path path, Project.NameKey name)
        throws RepositoryNotFoundException, RepositoryCaseMismatchException {
    if (isUnreasonableName(name)) {
        throw new RepositoryNotFoundException("Invalid name: " + name);
    }/*w  w w. j av a  2  s. c  om*/

    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.
        //
        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 /* bare */);

        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;
    }
}

From source file:com.google.gerrit.server.git.LocalDiskRepositoryManagerTest.java

License:Apache License

private void createRepository(Path directory, String projectName) throws IOException {
    String n = projectName + Constants.DOT_GIT_EXT;
    FileKey loc = FileKey.exact(directory.resolve(n).toFile(), FS.DETECTED);
    try (Repository db = RepositoryCache.open(loc, false)) {
        db.create(true /* bare */);
    }//from ww w.  j  av  a  2  s .  co  m
}

From source file:com.google.gerrit.server.git.MultiBaseLocalDiskRepositoryManagerTest.java

License:Apache License

private void createRepository(Path directory, Project.NameKey projectName) throws IOException {
    String n = projectName.get() + Constants.DOT_GIT_EXT;
    FileKey loc = FileKey.exact(directory.resolve(n).toFile(), FS.DETECTED);
    try (Repository db = RepositoryCache.open(loc, false)) {
        db.create(true /* bare */);
    }/*from www  .  j a  v  a  2s.  c om*/
}

From source file:com.madgag.agit.git.Repos.java

License:Open Source License

public static Repository openRepoFor(File gitdir) {
    try {/*from   w  w  w .  ja  v  a 2 s . co m*/
        Repository repo = RepositoryCache.open(FileKey.lenient(gitdir, FS.DETECTED), false);
        Log.d("REPO", "Opened " + describe(repo));
        return repo;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.tasktop.c2c.server.scm.service.JgitRepositoryProvider.java

License:Open Source License

public Repository getHostedRepository(String name) throws IOException {
    File repoDir = new File(getTenantHostedBaseDir(), name);
    FileKey key = FileKey.exact(repoDir, FS.DETECTED);
    Repository repo = RepositoryCache.open(key, true);
    return repo;//ww w. ja  v a2s.c o  m
}

From source file:net.sf.authorship.strategies.GitStrategy.java

License:Open Source License

public Set<Author> getAuthors() throws AuthorshipException {

    try {/*from ww w  . j a v a  2s.c  o  m*/
        this.cloneRepository(this.readOnlyUrl, this.folder);
    } catch (IOException ioe) {
        throw new AuthorshipException("Failed to clone git repository [" + this.readOnlyUrl + "]", ioe);
    }

    final Set<Author> authorEmails = new HashSet<Author>();
    final File directory = new File(this.folder);
    try {
        final Repository repository;
        try {
            repository = RepositoryCache.open(RepositoryCache.FileKey.lenient(directory, FS.DETECTED), true);
        } catch (IOException ioe) {
            throw new AuthorshipException("Failed to open git repository [" + this.readOnlyUrl
                    + "] cloned into local repository [" + this.folder + "]", ioe);
        }

        final RevWalk walk;
        try {
            walk = new RevWalk(repository);
            if (StringUtils.isNotBlank(this.fromRevision)) {
                this.fromRevision = Constants.HEAD;
            }
            ObjectId revId = repository.resolve(this.fromRevision);
            RevCommit root = walk.parseCommit(revId);
            walk.markStart(root);
            if (StringUtils.isNotBlank(this.toRevision)) {
                ObjectId to = repository.resolve(this.toRevision);
                RevCommit end = walk.parseCommit(to);
                walk.markUninteresting(end);
            }
        } catch (IOException ioe) {
            throw new AuthorshipException("Failed to analyse revisions from git repository [" + this.folder
                    + "]: " + ioe.getMessage(), ioe);
        }

        for (RevCommit commit : walk) {
            Author author = new Author(null, commit.getAuthorIdent().getName(),
                    commit.getAuthorIdent().getEmailAddress(), null);
            authorEmails.add(author);
        }
        walk.dispose();
    } finally {
        try {
            if (!directory.delete()) {
                directory.deleteOnExit();
            }
        } catch (RuntimeException re) {
            LOGGER.warning(re.getMessage());
        }
    }

    return authorEmails;
}

From source file:net.vexelon.jdevlog.vcs.git.GitSource.java

License:Open Source License

@Override
public void initialize() throws SCMException {

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    File repoPath = new File(configuration.get(ConfigOptions.SOURCE));

    try {/* ww w. j  a  v  a2 s .co  m*/
        //         repository = builder.setGitDir(repoPath).build();
        repository = RepositoryCache.open(RepositoryCache.FileKey.lenient(repoPath, FS.DETECTED), true);
    } catch (IOException e) {
        throw new SCMException("Could not locate Git repository in " + repoPath.getAbsolutePath(), e);
    }
}

From source file:org.gitorious.http.GitoriousResolver.java

License:Open Source License

public Repository open(HttpServletRequest req, String name)
        throws RepositoryNotFoundException, ServiceNotAuthorizedException, ServiceNotEnabledException {
    String realName = lookupName(name);
    if (realName == null) {
        throw new RepositoryNotFoundException(name);
    }//from  w  ww.j  a  v a2  s. c  o m
    try {
        File gitDir = new File(repositoryRoot, realName);
        Repository db = RepositoryCache.open(FileKey.lenient(gitDir, FS.DETECTED), true);
        return db;
    } catch (IOException io) {
        logger.error("ERROR: " + io.getMessage());
        throw new RepositoryNotFoundException(name);
    }
}