Java Code Examples for org.eclipse.jgit.api.CloneCommand#call()

The following examples show how to use org.eclipse.jgit.api.CloneCommand#call() . 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: GitProctorCore.java    From proctor with Apache License 2.0 6 votes vote down vote up
private Git cloneRepository(final File workingDir) throws GitAPIException {
    final CloneCommand cloneCommand = Git.cloneRepository()
            .setURI(gitUrl)
            .setDirectory(workingDir)
            .setProgressMonitor(PROGRESS_MONITOR)
            .setCredentialsProvider(user)
            .setTimeout(cloneTimeoutSeconds);

    if (StringUtils.isNotEmpty(branchName)) {
        final String refBranchName = "refs/heads/" + branchName;
        cloneCommand.setBranchesToClone(ImmutableSet.of(refBranchName))
                .setBranch(refBranchName); // prevent clone command from looking at the HEAD of the master branch
    }

    return cloneCommand.call();
}
 
Example 3
Source File: JGitWrapper.java    From mOrgAnd with GNU General Public License v2.0 6 votes vote down vote up
private void createNewRepo(ProgressMonitor monitor) throws GitAPIException, IllegalArgumentException {
    File localRepo = new File(localPath);
    if (localRepo.exists()) // Safety check so we don't accidentally delete directory
        throw new IllegalStateException("Directory already exists");

    try {
        CloneCommand cloneCommand = Git.cloneRepository()
                .setCredentialsProvider(credentialsProvider)
                .setURI(remotePath)
                .setBranch(branch)
                .setDirectory(localRepo)
                .setBare(false);
        if (monitor != null)
            cloneCommand.setProgressMonitor(monitor);
        cloneCommand.call();
    } catch (GitAPIException e) {
        FileUtils.deleteDirectory(localRepo);
        throw e;
    }
}
 
Example 4
Source File: GitHubConnector.java    From tool.accelerate.core with Apache License 2.0 6 votes vote down vote up
/**
 * Creates a repository on GitHub and in a local temporary directory. This is a one time operation as
 * once it is created on GitHub and locally it cannot be recreated.
 */
public File createGitRepository(String repositoryName) throws IOException {
    RepositoryService service = new RepositoryService();
    service.getClient().setOAuth2Token(oAuthToken);
    Repository repository = new Repository();
    repository.setName(repositoryName);
    repository = service.createRepository(repository);
    repositoryLocation = repository.getHtmlUrl();

    CloneCommand cloneCommand = Git.cloneRepository()
            .setURI(repository.getCloneUrl())
            .setDirectory(localGitDirectory);
    addAuth(cloneCommand);
    try {
        localRepository = cloneCommand.call();
    } catch (GitAPIException e) {
        throw new IOException("Error cloning to local file system", e);
    }
    return localGitDirectory;
}
 
Example 5
Source File: GitRepo.java    From spring-cloud-contract with Apache License 2.0 6 votes vote down vote up
private Git cloneToBasedir(URI projectUrl, File destinationFolder) {
	String url = projectUrl.toString();
	String projectGitUrl = url.endsWith(".git") ? url : url + ".git";
	if (log.isDebugEnabled()) {
		log.debug("Project git url [" + projectGitUrl + "]");
	}
	CloneCommand command = this.gitFactory.getCloneCommandByCloneRepository()
			.setURI(projectGitUrl).setDirectory(destinationFolder);
	try {
		Git git = command.call();
		if (git.getRepository().getRemoteNames().isEmpty()) {
			log.info("No remote added. Will add remote of the cloned project");
			git.remoteSetUrl().setUri(new URIish(projectGitUrl));
			git.remoteSetUrl().setName("origin");
			git.remoteSetUrl().setPush(true);
		}
		return git;
	}
	catch (GitAPIException | URISyntaxException e) {
		deleteBaseDirIfExists();
		throw new IllegalStateException(e);
	}
}
 
Example 6
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 7
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 8
Source File: GitWrapper.java    From Stringlate with MIT License 5 votes vote down vote up
public static boolean cloneRepo(final String uri, final File cloneTo,
                                final String branch,
                                final GitCloneProgressCallback callback) {
    Git result = null;

    try {
        final CloneCommand clone = Git.cloneRepository()
                .setURI(uri).setDirectory(cloneTo)
                .setBare(false).setRemote(REMOTE_NAME).setNoCheckout(false)
                .setCloneAllBranches(false).setCloneSubmodules(false)
                .setProgressMonitor(callback);

        if (!branch.isEmpty()) {
            if (branch.contains("/")) {
                clone.setBranch(branch.substring(branch.lastIndexOf('/') + 1));
            } else {
                clone.setBranch(branch);
            }
        }

        result = clone.call();
        return true;
    } catch (GitAPIException e) {
        e.printStackTrace();
    } finally {
        if (result != null) {
            result.close();
        }
    }
    return false;
}
 
Example 9
Source File: GitRepo.java    From spring-cloud-release-tools with Apache License 2.0 5 votes vote down vote up
private Git cloneToBasedir(URIish projectUrl, File destinationFolder)
		throws GitAPIException {
	CloneCommand command = this.gitFactory.getCloneCommandByCloneRepository()
			.setURI(projectUrl.toString() + ".git")
			.setDirectory(humanishDestination(projectUrl, destinationFolder));
	try {
		return command.call();
	}
	catch (GitAPIException e) {
		deleteBaseDirIfExists();
		throw e;
	}
}
 
Example 10
Source File: RepositorioCartasServico.java    From portal-de-servicos with MIT License 5 votes vote down vote up
private File clonarRepositorio(File caminhoLocal) throws GitAPIException {
    log.debug("Clonando repositório de cartas de serviço de {} para {}", urlRepositorio, caminhoLocal);
    CloneCommand clone = Git.cloneRepository()
            .setURI(urlRepositorio)
            .setProgressMonitor(new LogstashProgressMonitor(log))
            .setDirectory(caminhoLocal);

    try (Git repositorio = clone.call()) {
        String head = repositorio.log().call().iterator().next().getName();
        log.info("Repositório de cartas de serviço clonado na versão {}", head);
        return repositorio.getRepository().getWorkTree();
    }
}
 
Example 11
Source File: JGitEnvironmentRepository.java    From spring-cloud-config with Apache License 2.0 5 votes vote down vote up
private Git cloneToBasedir() throws GitAPIException {
	CloneCommand clone = this.gitFactory.getCloneCommandByCloneRepository()
			.setURI(getUri()).setDirectory(getBasedir());
	configureCommand(clone);
	try {
		return clone.call();
	}
	catch (GitAPIException e) {
		this.logger.warn("Error occured cloning to base directory.", e);
		deleteBaseDirIfExists();
		throw e;
	}
}
 
Example 12
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 13
Source File: GitServiceImpl.java    From molicode with Apache License 2.0 5 votes vote down vote up
private void pullGitRepoInLock(GitRepoVo gitRepoVo, CommonResult<String> result) throws IOException, GitAPIException {
    String filePath = SystemFileUtils.buildGitRepoDir(gitRepoVo.getGitUrl(), gitRepoVo.getBranchName());
    File repoDir = new File(filePath);

    CostWatch costWatch = CostWatch.createStarted();
    Git git = null;
    CredentialsProvider credentialsProvider = buildCredentialsProvider(gitRepoVo);
    if (repoDir.exists() && isGitDirectory(repoDir)) {
        git = Git.open(repoDir);
        LogHelper.DEFAULT.info("git repo already exist, fetch from url={}, branch={}", gitRepoVo.getGitUrl(), gitRepoVo.getBranchName());
        PullCommand pull = git.pull();
        if (credentialsProvider != null) {
            pull.setCredentialsProvider(credentialsProvider);
        }
        pull.setRemoteBranchName(gitRepoVo.getBranchName()).call();
        result.addDefaultModel("info", "仓库已存在,拉取最新内容到分支:" + gitRepoVo.getBranchName());
    } else {
        FileIoUtil.makeDir(repoDir);
        CloneCommand cloneCommand = Git.cloneRepository().setURI(gitRepoVo.getGitUrl()).setDirectory(repoDir).setBranch(gitRepoVo.getBranchName());
        if (credentialsProvider != null) {
            cloneCommand.setCredentialsProvider(credentialsProvider);
        }
        git = cloneCommand
                .call();
        LogHelper.DEFAULT.info("Cloning from " + gitRepoVo.getGitUrl() + " to " + git.getRepository());
        result.addDefaultModel("info", "仓库在本地还未存在,拉取最新内容到分支:" + gitRepoVo.getBranchName());
    }
    costWatch.stop();
    LogHelper.DEFAULT.info("拉取仓库模板,gitUrl={}, 耗时={}ms ", gitRepoVo.getGitUrl(), costWatch.getCost(TimeUnit.MILLISECONDS));
    result.addModel(MoliCodeConstant.ResultInfo.COST_TIME_KEY, costWatch.getCost(TimeUnit.SECONDS));
    result.succeed();
}
 
Example 14
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 15
Source File: CloneTask.java    From ant-git-tasks with Apache License 2.0 5 votes vote down vote up
@Override
public void execute() {
        try {
                CloneCommand cloneCommand = new CloneCommand();

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

                if (!branchNames.isEmpty()) {
                        cloneCommand.setBranchesToClone(branchNames);
                }

                cloneCommand.setURI(getUri()).
                        setBare(bare).
                        setCloneAllBranches(cloneAllBranches).
                        setCloneSubmodules(cloneSubModules).
                        setNoCheckout(noCheckout).
                        setDirectory(getDirectory());

                setupCredentials(cloneCommand);

                if (getProgressMonitor() != null) {
                        cloneCommand.setProgressMonitor(getProgressMonitor());
                }

                cloneCommand.call();
        }
        catch (Exception e) {
                throw new GitBuildException(String.format(MESSAGE_CLONE_FAILED, getUri()), e);
        }
}
 
Example 16
Source File: CloneCheckoutBranchRepository.java    From repairnator with MIT License 4 votes vote down vote up
@Override
protected StepStatus businessExecute() {
    
    String repoUrl = ((GitRepositoryProjectInspector) getInspector()).getGitRepositoryUrl() + ".git";
    String branch = null;
    if (((GitRepositoryProjectInspector) getInspector()).getGitRepositoryBranch() != null) {
    	branch = "refs/heads/" + ((GitRepositoryProjectInspector) getInspector()).getGitRepositoryBranch();
    }
    
    String repoLocalPath = this.getInspector().getRepoLocalPath();
    try {
        this.getLogger().info("Cloning repository " + repoUrl + " in the following directory: " + repoLocalPath);

        List<String> branchList = new ArrayList<String>();
        branchList.add(branch);
        
        CloneCommand cloneRepositoryCommand = Git.cloneRepository()
        		.setCloneSubmodules(true)
        		.setURI( repoUrl )
        		.setDirectory(new File(repoLocalPath));
        if (branch != null) {
        	cloneRepositoryCommand.setBranchesToClone(branchList).setBranch(branch);
        }
        
        Git git = cloneRepositoryCommand.call();
        Repository repository = git.getRepository();
        
        List<RevCommit> commits = getBranchCommits(repository, repoLocalPath);
        
        if (getConfig().isGitRepositoryFirstCommit()) {
        	git.checkout().setName(commits.get(commits.size()-1).getName()).call();
        } else if (getConfig().getGitRepositoryIdCommit() != null) {
        	git.checkout().setName(getConfig().getGitRepositoryIdCommit()).call();
        } else {
        	git.checkout().setName(commits.get(0).getName()).call();
        }
        
        return StepStatus.buildSuccess(this);
    } catch (Exception e) {
        this.addStepError("Repository " + repoUrl + " cannot be cloned.", e);
        return StepStatus.buildError(this, PipelineState.NOTCLONABLE);
    }
}
 
Example 17
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 18
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);
    }
}