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:playRepository.GitRepository.java

License:Apache License

public static void cloneRepository(String gitUrl, Project forkingProject, String authId, String authPw)
        throws GitAPIException {
    Git.cloneRepository().setURI(gitUrl).setDirectory(getGitDirectory(forkingProject)).setCloneAllBranches(true)
            .setBare(true).setCredentialsProvider(new UsernamePasswordCredentialsProvider(authId, authPw))
            .call();//from   w w w.  j  av a  2s  . com
}

From source file:playRepository.GitRepository.java

License:Apache License

private static Git cloneRepository(Project project, File workingDirectory) throws GitAPIException, IOException {
    return Git.cloneRepository().setURI(GitRepository.getGitDirectoryURL(project))
            .setDirectory(workingDirectory).call();
}

From source file:plumber.core.git.GitWorker.java

License:Apache License

public void init() throws IOException, GitAPIException {
    File gitFolder = new File(gitPath);
    File gitDB = new File(gitFolder, ".git");
    if (gitDB.exists() && gitDB.isDirectory()) {
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(gitDB).readEnvironment().findGitDir().build();
        git = new Git(repository);
    } else {/*ww  w  . j a va 2 s. co m*/
        CloneCommand cloneCommand = Git.cloneRepository();
        cloneCommand.setURI(gitUrl);
        cloneCommand.setDirectory(gitFolder);
        cloneCommand.setTransportConfigCallback(sshTransportConfigCallback);
        git = cloneCommand.call();
    }
}

From source file:pt.up.fe.specs.git.SpecsGit.java

License:Apache License

public static File parseRepositoryUrl(String repositoryPath) {
    String repoName = getRepoName(repositoryPath);

    // Get repo folder
    File eclipseBuildFolder = getRepositoriesFolder();
    File repoFolder = new File(eclipseBuildFolder, repoName);

    // If folder does not exist, or if it exists and is empty, clone repository
    if (!repoFolder.exists() || SpecsIo.isEmptyFolder(repoFolder)) {
        try {//from   ww  w.j ava  2s  .co  m
            SpecsLogs.msgInfo("Cloning repo '" + repositoryPath + "' to folder '" + repoFolder + "'");
            Git.cloneRepository().setURI(repositoryPath).setDirectory(repoFolder).call();

            return repoFolder;
        } catch (GitAPIException e) {
            throw new RuntimeException("Could not clone repository '" + repositoryPath + "'", e);
        }
    }

    // Repository already exists, pull

    try {
        SpecsLogs.msgInfo("Pulling repo '" + repositoryPath + "' in folder '" + repoFolder + "'");
        Git gitRepo = Git.open(repoFolder);
        PullCommand pullCmd = gitRepo.pull();
        pullCmd.call();
    } catch (GitAPIException | IOException e) {
        throw new RuntimeException("Could not pull repository '" + repositoryPath + "'", e);
    }

    return repoFolder;
}

From source file:ru.nikitenkogleb.androidtools.newappwizard.GitSupport.java

/**   Creates new Git Support module */
public GitSupport(String login, String password, String repository, String projectPath, String tempFolder,
        String initBranch) {//from  w  w  w  .j ava  2 s .co  m

    final String home = System.getProperty("user.home");
    if (login == null || login.isEmpty()) {
        mSshConfigCallback = new SSHConfigCallback(home + Path.SEPARATOR + ".ssh" + Path.SEPARATOR + "id_rsa",
                new CredentialsProvider(password));
        mHttpsCredentialsProvider = null;
    } else {
        mSshConfigCallback = null;
        mHttpsCredentialsProvider = new UsernamePasswordCredentialsProvider(login, password);
    }

    try {
        final CloneCommand cloneCommand = Git.cloneRepository().setURI(repository)
                .setDirectory(new File(tempFolder));

        if (mSshConfigCallback != null)
            cloneCommand.setTransportConfigCallback(mSshConfigCallback);
        else
            cloneCommand.setCredentialsProvider(mHttpsCredentialsProvider);

        final Git mGit = cloneCommand.call();

        try {
            mGit.checkout().setCreateBranch(true).setName(initBranch).call();
        } catch (RefNotFoundException e) {
            e.printStackTrace();
            final StoredConfig config = mGit.getRepository().getConfig();
            config.setString("remote", "origin", "url", repository);
            config.save();
        }

        mGit.close();

        move(new File(tempFolder + "/.git"), new File(projectPath + "/.git"));
        move(new File(tempFolder + "/README.md"), new File(projectPath + "/README.md"));
        move(new File(tempFolder + "/LICENSE"), new File(projectPath + "/LICENSE"));
        move(new File(tempFolder + "/.gitignore"), new File(projectPath + "/.gitignore"));

        new File(tempFolder).delete();
        mProjectPath = projectPath;

    } catch (GitAPIException | IOException e) {
        e.printStackTrace();
    }

}

From source file:se.kth.karamel.backend.github.GithubApi.java

/**
 * Clone an existing github repo./*from  w w  w .  ja  va 2  s.c o  m*/
 *
 * @param owner
 * @param repoName
 * @throws se.kth.karamel.common.exception.KaramelException
 */
public synchronized static void cloneRepo(String owner, String repoName) throws KaramelException {
    Git result = null;
    try {
        RepositoryService rs = new RepositoryService(client);
        Repository r = rs.getRepository(owner, repoName);

        String cloneURL = r.getSshUrl();
        // prepare a new folder for the cloned repository
        File localPath = new File(Settings.COOKBOOKS_PATH + File.separator + repoName);
        if (localPath.isDirectory() == false) {
            localPath.mkdirs();
        } else {
            throw new KaramelException("Local directory already exists. Delete it first: " + localPath);
        }

        logger.debug("Cloning from " + cloneURL + " to " + localPath);
        result = Git.cloneRepository().setURI(cloneURL).setDirectory(localPath).call();
        // Note: the call() returns an opened repository already which needs to be closed to avoid file handle leaks!
        logger.debug("Cloned repository: " + result.getRepository().getDirectory());
    } catch (IOException | GitAPIException ex) {
        throw new KaramelException("Problem cloning repo: " + ex.getMessage());
    } finally {
        if (result != null) {
            result.close();
        }
    }

}

From source file:se.lnu.cs.doris.git.GitRepository.java

License:Open Source License

/**
 * Method to fetch a bare .git repository for local storage.
 * @throws Exception /*w w  w  . ja v  a 2 s . com*/
 */
public void pullBare() throws Exception {
    String barePath = this.m_target + "/" + this.m_repoName + "_" + this.m_branch + ".git";
    File file = new File(barePath);

    if (file.exists()) {
        Utilities.deleteDirectory(file);
    }
    try {
        Git git = Git.cloneRepository().setURI(this.m_uri)
                .setDirectory(new File(this.m_target, this.m_repoName + "_" + this.m_branch + ".git"))
                .setCloneAllBranches(true).setBare(true).setBranch(m_branch).call();

        this.m_headRepository = git.getRepository();

    } catch (Exception e) {
        this.errorHandlingMining(e, null);
    }

}

From source file:to.sauerkraut.krautadmin.core.Toolkit.java

License:Open Source License

public static void cloneFromGit(final File targetDirectory, final URI repositoryUri, final boolean shallow)
        throws Exception {
    try {/*  ww w.j  a v a2s. com*/
        FileUtils.deleteDirectory(targetDirectory);
        FileUtils.forceMkdir(targetDirectory);
    } catch (IOException ioex) {
        LOG.info("Could not delete and/or recreate plugin git repo directory, maybe did not exist");
    }

    LOG.info("starting full plugin-update from git (shallow: " + String.valueOf(shallow) + ") ...");

    if (shallow) {
        final GitClone gitClone = new GitClone();
        final GitCloneOptions gitCloneOptions = new GitCloneOptions();
        gitCloneOptions.setDepth(2);

        gitClone.clone(targetDirectory, gitCloneOptions, UrlUtilities.url2JavaGitUrl(repositoryUri.toURL()),
                targetDirectory);
    } else {
        Git.cloneRepository().setURI(repositoryUri.toString()).setDirectory(targetDirectory).call();
    }
    LOG.info("full plugin-update successful");
}

From source file:uk.ac.cam.UROP.twentyfourteen.database.GitDb.java

/**
 * Clone a repository and return a GitDb object.
 * <p>//from w w  w.  j  a v  a2s  . c o  m
 * Note, you may wish to delete the directory after you have finished with it.
 * This is entirely your responsibility!
 *
 * @param src Source repository path
 * @param dest Destination directory
 * @param bare Clone bare?
 * @param branch Branch to clone
 */
public GitDb(String src, File dest, boolean bare, String branch, String remote, final String privateKey)
        throws IOException {
    this.privateKey = privateKey;
    this.sshFetchUrl = src;

    try {
        SshSessionFactory factory = new JschConfigSessionFactory() {
            @Override
            public void configure(Host hc, com.jcraft.jsch.Session session) {
                // // TODO: Bad!
                // session.setConfig("StrictHostKeyChecking", "no");
            }

            @Override
            protected JSch getJSch(final OpenSshConfig.Host hc, org.eclipse.jgit.util.FS fs)
                    throws JSchException {
                JSch jsch = super.getJSch(hc, fs);
                jsch.removeAllIdentity();

                if (null != privateKey) {
                    jsch.addIdentity(privateKey);
                }

                return jsch;
            }
        };

        if (src != null)
            SshSessionFactory.setInstance(factory);

        this.gitHandle = Git.cloneRepository().setURI(src).setDirectory(dest).setBare(bare).setBranch(branch)
                .setRemote(remote).call();
        this.gitHandle = Git.open(dest);

    } catch (GitAPIException e) {
        log.error("Error while trying to clone the repository.", e);
        throw new RuntimeException("Error while trying to clone the repository." + e);
    }
}

From source file:us.rader.dinodocs.GitPuller.java

License:Apache License

/**
 * Clone the "master" branch from specified remote repository to the
 * specified local directory.//  ww w. j  a va  2 s .c  om
 * 
 * @param remoteRepository
 *            Remote repository {@link URL}
 * 
 * @param branch
 *            Branch name
 * 
 * @param localRepository
 *            File system path to local repository
 * 
 * @throws Exception
 *             Thrown if check out fails
 */
private void cloneRepository(URI remoteRepository, String branch, File localRepository) throws Exception {

    String externalForm = remoteRepository.toString();
    CloneCommand cloneCommand = Git.cloneRepository();
    cloneCommand.setDirectory(localRepository);
    cloneCommand.setURI(externalForm);
    cloneCommand.setNoCheckout(false);
    cloneCommand.setCloneAllBranches(false);
    List<String> branches = new ArrayList<String>();
    branches.add(branch);
    cloneCommand.setBranchesToClone(branches);
    cloneCommand.setBranch(branch);

    try (Git git = cloneCommand.call()) {

        if (logger.isLoggable(Level.FINE)) {

            logger.logp(Level.FINE, getClass().getName(), "cloneRepository()", //$NON-NLS-1$
                    MessageFormat.format("cloned {1} of {0} to \"{0}\"", externalForm, branch, //$NON-NLS-1$
                            localRepository.getCanonicalPath()));

        }
    }
}