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

The following examples show how to use org.eclipse.jgit.api.CloneCommand#setCredentialsProvider() . 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: CloneRepositoryAdapter.java    From coderadar with MIT License 6 votes vote down vote up
@Override
public void cloneRepository(CloneRepositoryCommand cloneRepositoryCommand)
    throws UnableToCloneRepositoryException {
  try {
    // TODO: support progress monitoring
    CloneCommand cloneCommand =
        Git.cloneRepository()
            .setURI(cloneRepositoryCommand.getRemoteUrl())
            .setDirectory(new File(cloneRepositoryCommand.getLocalDir()))
            .setBare(true);
    if (cloneRepositoryCommand.getUsername() != null
        && cloneRepositoryCommand.getPassword() != null) {
      cloneCommand.setCredentialsProvider(
          new UsernamePasswordCredentialsProvider(
              cloneRepositoryCommand.getUsername(), cloneRepositoryCommand.getPassword()));
    }
    cloneCommand.call().close();
  } catch (GitAPIException e) {
    throw new UnableToCloneRepositoryException(e.getMessage());
  }
}
 
Example 3
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 4
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);
    }
}