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:edu.nju.cs.inform.jgit.porcelain.CloneRemoteRepositoryWithAuthentication.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // this is necessary when the remote host does not have a valid certificate, ideally we would install the certificate in the JVM
    // instead of this unsecure workaround!
    CredentialsProvider allowHosts = new CredentialsProvider() {

        @Override/*from   w ww  .  j  a va2 s.co  m*/
        public boolean supports(CredentialItem... items) {
            for (CredentialItem item : items) {
                if ((item instanceof CredentialItem.YesNoType)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean get(URIish uri, CredentialItem... items) throws UnsupportedCredentialItem {
            for (CredentialItem item : items) {
                if (item instanceof CredentialItem.YesNoType) {
                    ((CredentialItem.YesNoType) item).setValue(true);
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean isInteractive() {
            return false;
        }
    };

    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();

    // then clone
    System.out.println("Cloning from " + REMOTE_URL + " to " + localPath);
    try (Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath)
            .setCredentialsProvider(allowHosts).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:edu.nju.cs.inform.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", "");
    localPath.delete();//from w  w w  .ja  v  a 2 s  .co  m

    // 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:edu.nju.cs.inform.jgit.unfinished.PullFromRemoteRepository.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();/*ww  w  . jav a 2  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());
        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:edu.nju.cs.inform.jgit.unfinished.PullRemoteRepository.java

License:Apache License

private static Repository cloneRepository()
        throws IOException, GitAPIException, InvalidRemoteException, TransportException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();/*from   w  ww  .j a  v a 2  s  . co 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:edu.nju.cs.inform.jgit.unfinished.PushToRemoteRepository.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();/*from   w w w.  ja v  a2  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()) {
        // prepare a second folder for the 2nd clone
        File localPath2 = File.createTempFile("TestGitRepository", "");
        localPath2.delete();

        // then clone again
        System.out.println("Cloning from file://" + localPath + " to " + localPath2);
        try (Git result2 = Git.cloneRepository().setURI("file://" + localPath).setDirectory(localPath2)
                .call()) {
            // 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:edu.nju.cs.inform.jgit.unfinished.TrackMaster.java

License:Apache License

public static void main(String[] args)
        throws IOException, InvalidRemoteException, TransportException, GitAPIException {
    // prepare a new folder for the cloned repository
    File localPath = File.createTempFile("TestGitRepository", "");
    localPath.delete();//  w ww  . j  a va2  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()) {
        // 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:eu.atos.paas.git.Clone.java

License:Open Source License

public Repository call() throws GitAPIException, IOException {

    CloneCommand cmd = Git.cloneRepository();
    logger.debug("Start git clone {} into {}", sourceUrl, destProjectDir.getPath());
    Git gitRepo = cmd.setURI(sourceUrl.toString()).setDirectory(destProjectDir).call();
    gitRepo.close();// w ww.  j ava 2  s  .co  m
    logger.debug("End git clone {}", sourceUrl);

    return new Repository(destProjectDir);
}

From source file:eu.atos.paas.git.Repository.java

License:Open Source License

public static Repository clone(String from, File dest) throws GitAPIException, IOException {

    CloneCommand cmd = Git.cloneRepository();
    logger.debug("Start git clone {} into {}", from, dest.getPath());
    Git gitRepo = cmd.setURI(from).setDirectory(dest).call();
    gitRepo.close();/*from w ww  .ja  va2s . c o m*/
    logger.debug("End git clone {}", from);

    return new Repository(dest);
}

From source file:eu.seaclouds.platform.discoverer.crawler.PaasifyCrawler.java

License:Apache License

/**
 * Initializes a temporary directory and clones paasify repository in it
 *///from   w  w w  .j ava 2  s .  com
public PaasifyCrawler() {

    File tempDirectory = new File(paasifyRepositoryDirecory);

    /** Create paasify-offerings directory into os temp directory if it has not been created yet */
    if (!tempDirectory.exists()) {
        tempDirectory.mkdir();

        try {
            Git.cloneRepository().setURI(paasifyRepositoryURL).setDirectory(tempDirectory).call();
        } catch (Exception e) {
            log.error(e.getMessage());
        }
    }
}

From source file:eu.seaclouds.platform.discoverer.ws.PaasifySpider.java

License:Apache License

/**
 * Initializes a temporary directory and clones paasify repository in it
 *///from w  w  w .j a  v a2  s  .  c om
public PaasifySpider() {

    File tempDirectory = new File(paasifyRepositoryDirecory);

    /** Create paasify-offerings directory into os temp directory if it has not been created yet */
    if (!tempDirectory.exists()) {
        tempDirectory.mkdir();

        try {
            Git.cloneRepository().setURI(paasifyRepositoryURL).setDirectory(tempDirectory).call();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}