Example usage for org.eclipse.jgit.api Git cloneRepository

List of usage examples for org.eclipse.jgit.api Git cloneRepository

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git cloneRepository.

Prototype

public static CloneCommand cloneRepository() 

Source Link

Document

Return a command object to execute a clone command

Usage

From source file:org.craftercms.deployer.utils.GitUtils.java

License:Open Source License

public static Git cloneRemoteRepository(String remoteRepositoryUrl, String remoteRepositoryUsername,
        String remoteRepositoryPassword, File localRepositoryFolder) throws GitAPIException {
    return Git.cloneRepository().setURI(remoteRepositoryUrl).setDirectory(localRepositoryFolder)
            .setCredentialsProvider(// w  w  w.ja  va  2 s.c o m
                    new UsernamePasswordCredentialsProvider(remoteRepositoryUsername, remoteRepositoryPassword))
            .call();
}

From source file:org.craftercms.studio.impl.v1.deployment.EnvironmentStoreGitBranchDeployer.java

License:Open Source License

private Repository cloneSiteRepository(String site) {
    Path siteEnvironmentStoreRepoPath = Paths.get(environmentsStoreRootPath, site);
    File localPath = siteEnvironmentStoreRepoPath.toFile();
    try {//w  w w. ja v a2  s .  c o  m
        FileUtils.deleteDirectory(localPath);
    } catch (IOException e) {
        logger.error("Error deleting directory " + localPath.toString());
    }

    Path siteRepoPath = Paths.get(rootPath, "sites", site, ".git");
    try (Git result = Git.cloneRepository().setURI(siteRepoPath.toAbsolutePath().normalize().toString())
            .setDirectory(localPath).call()) {
        return result.getRepository();
    } catch (GitAPIException e) {
        logger.error("Error cloning repository for site " + site, e);
        return null;
    }
}

From source file:org.dstadler.jgit.porcelain.CloneRemoteRepository.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }/*from  w  w w.ja  v a2  s. c  om*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        System.out.println("Having repository: " + result.getRepository().getDirectory());
    }
}

From source file:org.dstadler.jgit.porcelain.FetchRemoteCommitsWithPrune.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }/*from www  .j  a v a 2s . com*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git git = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        System.out.println("Having repository: " + git.getRepository().getDirectory());

        System.out.println("Starting fetch");
        FetchResult result = git.fetch().setCheckFetchedObjects(true).call();
        System.out.println("Messages: " + result.getMessages());

        // ensure master/HEAD are still there
        System.out.println("Listing local branches:");
        List<Ref> call = git.branchList().call();
        for (Ref ref : call) {
            System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
        }

        System.out.println("Now including remote branches:");
        call = git.branchList().setListMode(ListMode.ALL).call();
        for (Ref ref : call) {
            System.out.println("Branch: " + ref + " " + ref.getName() + " " + ref.getObjectId().getName());
        }
    }
}

From source file:org.dstadler.jgit.unfinished.PullFromRemoteRepository.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }//from ww  w  .ja v a  2  s.c  o m

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        System.out.println("Having repository: " + result.getRepository().getDirectory());
        try (Git git = new Git(result.getRepository())) {
            git.pull().call();
        }

        System.out.println("Pulled from remote repository to local repository at "
                + result.getRepository().getDirectory());
    }
}

From source file:org.dstadler.jgit.unfinished.PullRemoteRepository.java

License:Apache License

private static Repository cloneRepository() throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }/*from w  w  w .  jav  a  2 s . c o  m*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        return result.getRepository();
    }
}

From source file:org.dstadler.jgit.unfinished.PushToRemoteRepository.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }/* www.ja  v  a2  s  . com*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        // prepare a second folder for the 2nd clone
        File localPath2 = File.createTempFile("TestGitRepository", "");
        if (!localPath2.delete()) {
            throw new IOException("Could not delete temporary file " + localPath2);
        }

        // then clone again
        System.out.println("Cloning from file://" + localPath + " to " + localPath2);
        try (Git result2 = Git.cloneRepository().setURI("file://" + localPath).setDirectory(localPath2)
                .call()) {
            System.out.println("Result: " + result2);

            // now open the created repository
            FileRepositoryBuilder builder = new FileRepositoryBuilder();
            try (Repository repository = builder.setGitDir(localPath2).readEnvironment() // scan environment GIT_* variables
                    .findGitDir() // scan up the file system tree
                    .build()) {
                try (Git git = new Git(repository)) {
                    git.push().call();
                }

                System.out.println("Pushed from repository: " + repository.getDirectory()
                        + " to remote repository at " + REMOTE_URL);
            }
        }
    }
}

From source file:org.dstadler.jgit.unfinished.TrackMaster.java

License:Apache License

public static void main(String[] args) throws IOException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    if (!localPath.delete()) {
        throw new IOException("Could not delete temporary file " + localPath);
    }/*from   w  ww  .  j  a  v a 2s  .  com*/

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()) {
        System.out.println("Result: " + result);

        // now open the created repository
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        try (Repository repository = builder.setGitDir(localPath).readEnvironment() // scan environment GIT_* variables
                .findGitDir() // scan up the file system tree
                .build()) {
            try (Git git = new Git(repository)) {
                git.branchCreate().setName("master")
                        // ?!? .setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
                        .setStartPoint("origin/master").setForce(true).call();
            }

            System.out.println("Now tracking master in repository at " + repository.getDirectory()
                    + " from origin/master at " + REMOTE_URL);
        }
    }
}

From source file:org.eclipse.che.git.impl.jgit.JGitConnection.java

License:Open Source License

public void clone(CloneRequest request) throws GitException, UnauthorizedException {
    String remoteUri;//ww w .j a  v a2 s  . c  o  m
    boolean removeIfFailed = false;
    try {
        if (request.getRemoteName() == null) {
            request.setRemoteName(Constants.DEFAULT_REMOTE_NAME);
        }
        if (request.getWorkingDir() == null) {
            request.setWorkingDir(repository.getWorkTree().getCanonicalPath());
        }

        // If clone fails and the .git folder didn't exist we want to remove it.
        // We have to do this here because the clone command doesn't revert its own changes in case of failure.
        removeIfFailed = !repository.getDirectory().exists();

        remoteUri = request.getRemoteUri();
        CloneCommand cloneCommand = Git.cloneRepository().setDirectory(new File(request.getWorkingDir()))
                .setRemote(request.getRemoteName()).setURI(remoteUri);
        if (request.getBranchesToFetch().isEmpty()) {
            cloneCommand.setCloneAllBranches(true);
        } else {
            cloneCommand.setBranchesToClone(request.getBranchesToFetch());
        }

        executeRemoteCommand(remoteUri, cloneCommand);

        StoredConfig repositoryConfig = getRepository().getConfig();
        GitUser gitUser = getUser();
        if (gitUser != null) {
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null,
                    ConfigConstants.CONFIG_KEY_NAME, gitUser.getName());
            repositoryConfig.setString(ConfigConstants.CONFIG_USER_SECTION, null,
                    ConfigConstants.CONFIG_KEY_EMAIL, gitUser.getEmail());
        }
        repositoryConfig.save();
    } catch (IOException | GitAPIException exception) {
        // Delete .git directory in case it was created
        if (removeIfFailed) {
            deleteRepositoryFolder();
        }
        throw new GitException(exception.getMessage(), exception);
    }
}

From source file:org.eclipse.dirigible.repository.ext.git.JGitConnector.java

License:Open Source License

/**
 * Clones secured git remote repository to the file system.
 *
 * @param gitDirectory/*from   ww  w  . jav  a 2s  .  c  o m*/
 *            where the remote repository will be cloned
 * @param repositoryURI
 *            repository's URI example: https://qwerty.com/xyz/abc.git
 * @param username
 *            the username used for authentication
 * @param password
 *            the password used for authentication
 * @param branch
 *            the branch where sources will be cloned from
 * @throws InvalidRemoteException
 * @throws TransportException
 * @throws GitAPIException
 */
public static void cloneRepository(File gitDirectory, String repositoryURI, String username, String password,
        String branch) throws InvalidRemoteException, TransportException, GitAPIException {
    try {
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setURI(repositoryURI);
        if (!StringUtils.isEmptyOrNull(username) && !StringUtils.isEmptyOrNull(password)) {
            cloneCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(username, password));
        }
        cloneCommand.setBranch(branch);
        cloneCommand.setDirectory(gitDirectory);
        cloneCommand.call();
    } catch (Exception e) {
        throw new TransportException(e.getMessage());
    }
}