Example usage for org.eclipse.jgit.lib Repository getDirectory

List of usage examples for org.eclipse.jgit.lib Repository getDirectory

Introduction

In this page you can find the example usage for org.eclipse.jgit.lib Repository getDirectory.

Prototype


public File getDirectory() 

Source Link

Document

Get local metadata directory

Usage

From source file:com.microsoft.gittf.core.util.DirectoryUtil.java

License:Open Source License

/**
 * Get the directory to use for staging changes to TFS.
 * //from   w ww  . j a v a2 s .  c  o m
 * @param repository
 *        the git repository
 * @return
 */
public static File getTempDirRoot(final Repository repository) {
    Check.notNull(repository, "repository"); //$NON-NLS-1$

    GitTFConfiguration config = GitTFConfiguration.loadFrom(repository);
    if (config.getTempDirectory() != null) {
        return new File(config.getTempDirectory());
    }

    File rootDirectory = repository.getDirectory().getAbsoluteFile();

    try {
        rootDirectory = rootDirectory.getCanonicalFile();
    } catch (IOException e) {
        /* suppress */
    }

    return new File(rootDirectory, GitTFConstants.GIT_TF_DIRNAME);
}

From source file:com.microsoft.gittf.core.util.RepositoryUtil.java

License:Open Source License

/**
 * Creates a repository object in the specified directory
 * //w ww  . ja v  a  2 s .  c  om
 * @param gitDir
 * @return
 * @throws IOException
 */
public static Repository findRepository(final String gitDir) throws IOException {
    RepositoryBuilder repoBuilder = new RepositoryBuilder().setGitDir(gitDir != null ? new File(gitDir) : null)
            .readEnvironment().findGitDir();

    boolean isBare = false;

    if (repoBuilder.getGitDir() == null) {
        isBare = true;
        repoBuilder.setGitDir(new File(".")); //$NON-NLS-1$
    }

    Repository repository = repoBuilder.build();

    if (isBare) {
        /*
         * if this is a bare repo we need to check if it has the
         * configuration
         */
        GitTFConfiguration config = GitTFConfiguration.loadFrom(repository);
        if (config == null) {
            return null;
        }
    }

    if (!repository.getDirectory().exists() || !repository.getDirectory().isDirectory()) {
        String directoryName = repository.getDirectory().toString();
        throw new IOException(Messages.formatString("RepositoryUtil.NotGitRepoExceptionFormat", directoryName)); //$NON-NLS-1$
    }

    return repoBuilder.build();
}

From source file:com.sonatype.sshjgit.core.gitcommand.AbstractGitCommand.java

License:Apache License

/**
 * Extracts the repo's directory path relative to the repo root, and
 * converts slashes to colon, so that subdirectories become permission parts
 * @param repo the git repo/* ww  w  . j a  va2  s  . c  o  m*/
 * @return a colon separated string of directories, representing the repo's
 * location in the repo root directory.
 */
protected String getRepoNameAsPermissionParts(Repository repo) {
    final String repoPath = repo.getDirectory().getAbsolutePath();
    String relativePath = repoPath.replace(reposRootDirPath, "");
    if (relativePath.startsWith("/")) {
        relativePath = relativePath.substring(1);
    }
    return relativePath.replace('/', ':');
}

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

License:Open Source License

private void sendCommitsInternal(Repository repo, String refName, ObjectId oldId, ObjectId newId)
        throws IOException {

    Ref ref = repo.getRef(refName);

    if (!newId.equals(ref.getObjectId())) {
        throw new IllegalStateException();
    }//from  w w w . j a v a  2  s.  c o m

    RevWalk revWal = new RevWalk(repo);
    revWal.markStart(revWal.parseCommit(ref.getObjectId()));
    if (!oldId.equals(ObjectId.zeroId())) {
        revWal.markUninteresting(revWal.parseCommit(oldId));
    }

    List<Commit> eventCommits = new ArrayList<Commit>();

    for (RevCommit revCommit : revWal) {
        Commit commit = GitDomain.createCommit(revCommit);
        commit.setRepository(repo.getDirectory().getName());
        commit.setUrl(configuration.getWebUrlForCommit(TenancyUtil.getCurrentTenantProjectIdentifer(), commit));

        eventCommits.add(commit);
    }

    PushEvent event = new PushEvent();
    event.setUserId(Security.getCurrentUser());
    event.setCommits(eventCommits);
    event.setProjectId(TenancyUtil.getCurrentTenantProjectIdentifer());
    event.setTimestamp(new Date());
    event.setRefName(refName);
    eventService.publishEvent(event);

}

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

License:Open Source License

public static List<Commit> getLog(Repository r, String revision, String path, Region region)
        throws IOException {
    if (path.startsWith("/")) {
        path = path.substring(1);//from  w w  w .j  av  a 2  s .  c  o m
    }

    List<Commit> commits = new ArrayList<Commit>();

    ObjectId revId = r.resolve(revision);
    RevWalk walk = new RevWalk(r);

    walk.markStart(walk.parseCommit(revId));

    if (path != null && path.trim().length() != 0) {
        walk.setTreeFilter(AndTreeFilter.create(PathFilterGroup.createFromStrings(path), TreeFilter.ANY_DIFF));
    }

    if (region != null) {
        // if (region.getOffset() > 0 && region.getSize() > 0)
        // walk.setRevFilter(AndRevFilter.create(SkipRevFilter.create(region.getOffset()),
        // MaxCountRevFilter.create(region.getSize())));
        /* else */if (region.getOffset() > 0) {
            walk.setRevFilter(SkipRevFilter.create(region.getOffset()));
        }
        // else if (region.getSize() > 0) {
        // walk.setRevFilter(MaxCountRevFilter.create(region.getSize()));
        // }
    }

    int max = region != null && region.getSize() > 0 ? region.getSize() : -1;

    for (RevCommit revCommit : walk) {
        Commit commit = GitDomain.createCommit(revCommit);
        commit.setRepository(r.getDirectory().getName());
        commits.add(commit);
        if (max != -1) {
            if (max == 0) {
                break;
            }
            max--;
        }
    }

    return commits;
}

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

License:Open Source License

private List<Commit> getLog(Repository repository, Region region, Set<ObjectId> visited) {
    List<Commit> result = new ArrayList<Commit>();

    for (RevCommit revCommit : getAllCommits(repository, region, visited)) {
        Commit commit = GitDomain.createCommit(revCommit);
        commit.setRepository(repository.getDirectory().getName());
        result.add(commit);/*from  w  ww.  j  a v  a  2 s . c om*/
    }

    return result;
}

From source file:com.telefonica.euro_iaas.sdc.puppetwrapper.services.impl.GitCloneServiceImpl.java

License:Apache License

public void download(String url, String moduleName) throws ModuleDownloaderException {
    // prepare a new folder for the cloned repository
    File localPath = new File(modulesCodeDownloadPath + moduleName);
    localPath.delete();//from w  ww . jav a 2 s  . c om

    try {
        FileUtils.deleteDirectory(localPath);
    } catch (IOException e) {
        throw new ModuleDownloaderException(e);
    }

    // then clone
    logger.debug("Cloning from " + url + " to " + localPath);
    try {
        Git.cloneRepository().setURI(url).setDirectory(localPath).call();
    } catch (InvalidRemoteException e) {
        throw new ModuleDownloaderException(e);
    } catch (TransportException e) {
        throw new ModuleDownloaderException(e);
    } catch (GitAPIException e) {
        throw new ModuleDownloaderException(e);
    }

    // now open the created repository
    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository repository;
    try {
        repository = builder.setGitDir(localPath).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build();
    } catch (IOException e) {
        throw new ModuleDownloaderException(e);
    }

    logger.debug("Having repository: " + repository.getDirectory());

    repository.close();
}

From source file:com.unboundid.buildtools.repositoryinfo.RepositoryInfo.java

License:Open Source License

/**
 * Obtains information about the git repository from which the LDAP SDK source
 * was obtained.//from w  w  w. ja va2 s .  com
 *
 * @throws  Exception  If a problem is encountered while attempting to obtain
 *                     information about the git repository.
 */
private void getGitInfo() throws Exception {
    final Repository r = new FileRepositoryBuilder().readEnvironment().findGitDir(baseDir).build();

    final String repoURL = r.getConfig().getString("remote", "origin", "url");
    final String repoRevision = r.resolve("HEAD").getName();

    final String canonicalWorkspacePath = baseDir.getCanonicalPath();
    final String canonicalRepositoryBaseDir = r.getDirectory().getParentFile().getCanonicalPath();

    String repoPath = canonicalWorkspacePath.substring(canonicalRepositoryBaseDir.length());
    if (!repoPath.startsWith("/")) {
        repoPath = '/' + repoPath;
    }

    getProject().setProperty(repositoryTypePropertyName, "git");
    getProject().setProperty(repositoryURLPropertyName, repoURL);
    getProject().setProperty(repositoryPathPropertyName, repoPath);
    getProject().setProperty(repositoryRevisionPropertyName, repoRevision);
    getProject().setProperty(repositoryRevisionNumberPropertyName, "-1");
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

ILockedRepository createBranchRepository(String branchName, String startingBranch)
        throws IOException, GitAPIException {
    Assert.hasLength(branchName);//from   www . j  a  va 2 s . c o  m
    if (startingBranch != null) {
        Assert.hasLength(startingBranch);
    }

    File repoDir = new File(reposDir, branchName);
    if (repoDir.isDirectory()) {
        throw new IllegalStateException("repository already exists: " + repoDir.getAbsolutePath()); //$NON-NLS-1$
    }

    List<String> branches = listBranches();
    if (branches.contains(branchName)) {
        throw new IllegalArgumentException("branch already exists: " + branchName); //$NON-NLS-1$
    }

    if ((startingBranch == null) && !branches.isEmpty()) {
        throw new IllegalArgumentException("must specify a starting branch"); //$NON-NLS-1$
    }

    ILock lock = lockManager.lockAll();
    try {
        Repository centralRepo = null;
        File centralRepoGitDir;
        try {
            centralRepo = getCentralRepositoryInternal(true);
            centralRepoGitDir = centralRepo.getDirectory();
        } finally {
            RepositoryUtil.closeQuietly(centralRepo);
            centralRepo = null;
        }

        Repository repo = null;
        try {
            repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString()).setDirectory(repoDir)
                    .call().getRepository();

            try {
                centralRepo = getCentralRepositoryInternal(true);
                if (!RepositoryUtils.getBranches(centralRepo).contains(branchName)) {
                    CreateBranchCommand createBranchCommand = Git.wrap(centralRepo).branchCreate();
                    if (startingBranch != null) {
                        createBranchCommand.setStartPoint(startingBranch);
                    }
                    createBranchCommand.setName(branchName).call();
                }
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            Git git = Git.wrap(repo);
            RefSpec refSpec = new RefSpec("refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
            git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
            git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
            git.checkout().setName(branchName).call();
        } finally {
            RepositoryUtil.closeQuietly(repo);
        }
    } finally {
        lockManager.unlock(lock);
    }

    return getBranchRepository(branchName);
}

From source file:de.blizzy.documentr.repository.ProjectRepositoryManager.java

License:Open Source License

public void importSampleContents() throws IOException, GitAPIException {
    // TODO: synchronization is not quite correct here, but should be okay in this edge case
    if (listBranches().isEmpty()) {
        ILock lock = lockManager.lockAll();
        List<String> branches;
        try {//from   ww  w. j a  va2 s .  c o  m
            File gitDir = new File(centralRepoDir, ".git"); //$NON-NLS-1$
            FileUtils.forceDelete(gitDir);

            Git.cloneRepository().setURI(DocumentrConstants.SAMPLE_REPO_URL).setDirectory(gitDir).setBare(true)
                    .call();

            Repository centralRepo = null;
            File centralRepoGitDir;
            try {
                centralRepo = getCentralRepositoryInternal(true);
                centralRepoGitDir = centralRepo.getDirectory();
                StoredConfig config = centralRepo.getConfig();
                config.unsetSection("remote", "origin"); //$NON-NLS-1$ //$NON-NLS-2$
                config.unsetSection("branch", "master"); //$NON-NLS-1$ //$NON-NLS-2$
                config.save();
            } finally {
                RepositoryUtil.closeQuietly(centralRepo);
            }

            branches = listBranches();
            for (String branchName : branches) {
                File repoDir = new File(reposDir, branchName);
                Repository repo = null;
                try {
                    repo = Git.cloneRepository().setURI(centralRepoGitDir.toURI().toString())
                            .setDirectory(repoDir).call().getRepository();

                    Git git = Git.wrap(repo);
                    RefSpec refSpec = new RefSpec(
                            "refs/heads/" + branchName + ":refs/remotes/origin/" + branchName); //$NON-NLS-1$ //$NON-NLS-2$
                    git.fetch().setRemote("origin").setRefSpecs(refSpec).call(); //$NON-NLS-1$
                    git.branchCreate().setName(branchName).setStartPoint("origin/" + branchName).call(); //$NON-NLS-1$
                    git.checkout().setName(branchName).call();
                } finally {
                    RepositoryUtil.closeQuietly(repo);
                }
            }
        } finally {
            lockManager.unlock(lock);
        }

        for (String branch : branches) {
            eventBus.post(new BranchCreatedEvent(projectName, branch));
        }
    }
}