Java Code Examples for org.eclipse.jgit.api.Git#cloneRepository()

The following examples show how to use org.eclipse.jgit.api.Git#cloneRepository() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: UIGit.java    From hop with Apache License 2.0 6 votes vote down vote up
public boolean cloneRepo( String directory, String uri ) {
  CloneCommand cmd = Git.cloneRepository();
  cmd.setDirectory( new File( directory ) );
  cmd.setURI( uri );
  cmd.setCredentialsProvider( credentialsProvider );
  try {
    Git git = cmd.call();
    git.close();
    return true;
  } catch ( Exception e ) {
    if ( ( e instanceof TransportException )
        && ( ( e.getMessage().contains( "Authentication is required but no CredentialsProvider has been registered" )
          || e.getMessage().contains( "not authorized" ) ) ) ) {
      if ( promptUsernamePassword() ) {
        return cloneRepo( directory, uri );
      }
    } else {
      showMessageBox( BaseMessages.getString( PKG, "Dialog.Error" ), e.getMessage() );
    }
  }
  return false;
}
 
Example 2
Source File: ForgeClientAsserts.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
/**
 * Asserts that we can git clone the given repository
 */
public static Git assertGitCloneRepo(String cloneUrl, File outputFolder) throws GitAPIException, IOException {
    LOG.info("Cloning git repo: " + cloneUrl + " to folder: " + outputFolder);

    Files.recursiveDelete(outputFolder);
    outputFolder.mkdirs();

    CloneCommand command = Git.cloneRepository();
    command = command.setCloneAllBranches(false).setURI(cloneUrl).setDirectory(outputFolder).setRemote("origin");

    Git git;
    try {
        git = command.call();
    } catch (Exception e) {
        LOG.error("Failed to git clone remote repo " + cloneUrl + " due: " + e.getMessage(), e);
        throw e;
    }
    return git;
}
 
Example 3
Source File: ProjectFileSystem.java    From fabric8-forge with Apache License 2.0 6 votes vote down vote up
public static void cloneRepo(File projectFolder, String cloneUrl, CredentialsProvider credentialsProvider, final File sshPrivateKey, final File sshPublicKey, String remote, String tag) {
    StopWatch watch = new StopWatch();

    // clone the repo!
    boolean cloneAll = true;
    LOG.info("Cloning git repo " + cloneUrl + " into directory " + projectFolder.getAbsolutePath() + " cloneAllBranches: " + cloneAll);
    CloneCommand command = Git.cloneRepository();
    GitUtils.configureCommand(command, credentialsProvider, sshPrivateKey, sshPublicKey);
    command = command.setCredentialsProvider(credentialsProvider).
            setCloneAllBranches(cloneAll).setURI(cloneUrl).setDirectory(projectFolder).setRemote(remote);

    try {
        Git git = command.call();
        if (tag != null){
            git.checkout().setName(tag).call();
        }
    } catch (Throwable e) {
        LOG.error("Failed to command remote repo " + cloneUrl + " due: " + e.getMessage(), e);
        throw new RuntimeException("Failed to command remote repo " + cloneUrl + " due: " + e.getMessage());
    } finally {
        LOG.info("cloneRepo took " + watch.taken());
    }
}
 
Example 4
Source File: ExtensionPointAction.java    From maestro-java with Apache License 2.0 5 votes vote down vote up
private void doClone() throws GitAPIException, IOException {
    CloneCommand cloneCommand = Git.cloneRepository();

    cloneCommand.setURI(REPOSITORY_URL);

    File repositoryDir = new File(directory, name);
    cloneCommand.setDirectory(repositoryDir);
    cloneCommand.setProgressMonitor(new TextProgressMonitor());

    cloneCommand.call();
    System.out.println("Project directory for project created at " + repositoryDir);

    FileUtils.deleteDirectory(new File(repositoryDir, ".git"));
}
 
Example 5
Source File: ProjectWebsiteTest.java    From jbake with MIT License 5 votes vote down vote up
private void cloneJbakeWebsite() throws GitAPIException {
    CloneCommand cmd = Git.cloneRepository();
    cmd.setBare(false);
    cmd.setBranch("master");
    cmd.setRemote("origin");
    cmd.setURI(WEBSITE_REPO_URL);
    cmd.setDirectory(projectFolder);

    cmd.call();

    assertThat(new File(projectFolder,"README.md").exists()).isTrue();
}
 
Example 6
Source File: MaestroAgent.java    From maestro-java with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(final AgentSourceRequest note) {
    logger.info("Source request arrived");

    final String sourceUrl = note.getSourceUrl();
    final String branch = note.getBranch();

    if (branch == null) {
        logger.info("Preparing to download code from {}", sourceUrl);
    }
    else {
        logger.info("Preparing to download code from {} from branch {}", sourceUrl, branch);
    }
    final String projectDir = UUID.randomUUID().toString();

    final File repositoryDir = new File(sourceRoot + File.separator + projectDir + File.separator);

    if (!repositoryDir.exists()) {
        if (!repositoryDir.mkdirs()) {
            logger.warn("Unable to create directory: {}", repositoryDir);
        }
    }

    CloneCommand cloneCommand = Git.cloneRepository();

    cloneCommand.setURI(sourceUrl);
    cloneCommand.setDirectory(repositoryDir);
    cloneCommand.setProgressMonitor(NullProgressMonitor.INSTANCE);


    if (branch != null) {
        cloneCommand.setBranch(branch);
    }

    try {
        cloneCommand.call();
        logger.info("Source directory for project created at {}", repositoryDir);
        extensionPoints.add(new ExtensionPoint(new File(repositoryDir, "requests"), true));

        getClient().replyOk(note);
    } catch (GitAPIException e) {
        logger.error("Unable to clone repository: {}", e.getMessage(), e);
        getClient().replyInternalError(note,"Unable to clone repository: %s", e.getMessage());
    }
}
 
Example 7
Source File: GitRepoCloner.java    From multiapps-controller with Apache License 2.0 4 votes vote down vote up
public GitRepoCloner() {
    cloneCommand = Git.cloneRepository();
}
 
Example 8
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 4 votes vote down vote up
public CloneCommand getCloneCommandByCloneRepository() {
	CloneCommand command = Git.cloneRepository();
	return command;
}
 
Example 9
Source File: Clone.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void execute(Wandora wandora, Context context) {
    if(cloneUI == null) {
        cloneUI = new CloneUI();
    }
    
    cloneUI.setUsername(getUsername());
    cloneUI.setPassword(getPassword());
    cloneUI.openInDialog();
    
    if(cloneUI.wasAccepted()) {
        setDefaultLogger();
        setLogTitle("Git clone");
        
        String cloneUrl = cloneUI.getCloneUrl();
        String destinationDirectory = cloneUI.getDestinationDirectory();
        String username = cloneUI.getUsername();
        String password = cloneUI.getPassword();
        
        setUsername(username);
        setPassword(password);

        log("Cloning git repository from "+cloneUrl);
        
        try {
            CloneCommand clone = Git.cloneRepository();
            clone.setURI(cloneUrl);
            clone.setDirectory(new File(destinationDirectory));
            
            if(isNotEmpty(username)) {
                CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider( username, password );
                clone.setCredentialsProvider(credentialsProvider);
            }
            
            clone.call();
            
            if(cloneUI.getOpenProject()) {
                log("Opening project.");
                LoadWandoraProject loadProject = new LoadWandoraProject(destinationDirectory);
                loadProject.setToolLogger(this);
                loadProject.execute(wandora, context);
            }
            log("Ready.");
        } 
        catch (GitAPIException ex) {
            log(ex.toString());
        }
        catch (Exception e) {
            log(e);
        }
        setState(WAIT);
    }
}