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

The following examples show how to use org.eclipse.jgit.api.Git#pull() . 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: UpdaterGenerator.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Execute git pull command on the given repository.
 * It populates an ArrayList with all the updated files.
 * @param localPath The path where the project is.
 * @return Returns true if you should update plugins, false otherwise.
 */
private boolean gitPull(File localPath) {
    try {
        Repository localRepo = new FileRepository(localPath.getAbsolutePath() + "/.git");
        git = new Git(localRepo);
        
        populateDiff();
        
        if(!pluginsToUpdate.isEmpty()){
            PullCommand pullCmd = git.pull();
            pullCmd.call();
            return true;
        }
        else{
            return false;
        }
        
    } catch (GitAPIException | IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return true;
}
 
Example 2
Source File: UpdaterGenerator.java    From neembuu-uploader with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Execute git pull command on the given repository.
 * It populates an ArrayList with all the updated files.
 * @param localPath The path where the project is.
 * @return Returns true if you should update plugins, false otherwise.
 */
private boolean gitPull(File localPath) {
    try {
        Repository localRepo = new FileRepository(localPath.getAbsolutePath() + "/.git");
        git = new Git(localRepo);
        
        
        
        if(populateDiff()){
            PullCommand pullCmd = git.pull();
            pullCmd.call();
            return true;
        }
        else{
            return false;
        }
        
    } catch (GitAPIException | IOException ex) {
        Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
    }

    return true;
}
 
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: RepositoryResource.java    From fabric8-forge with Apache License 2.0 5 votes vote down vote up
protected void doPull(Git git, GitContext context) throws GitAPIException {
    StopWatch watch = new StopWatch();

    LOG.info("Performing a pull in git repository " + this.gitFolder + " on remote URL: " + this.remoteRepository);
    CredentialsProvider cp = userDetails.createCredentialsProvider();
    PullCommand command = git.pull();
    configureCommand(command, userDetails);
    command.setCredentialsProvider(cp).setRebase(true).call();
    LOG.info("Took " + watch.taken() + " to complete pull in git repository " + this.gitFolder + " on remote URL: " + this.remoteRepository);
}
 
Example 5
Source File: PullJob.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
private IStatus doPull(IProgressMonitor monitor) throws IOException, GitAPIException, CoreException {
	ProgressMonitor gitMonitor = new EclipseGitProgressTransformer(monitor);
	Repository db = null;
	try {
		db = FileRepositoryBuilder.create(GitUtils.getGitDir(path));
		Git git = Git.wrap(db);
		PullCommand pc = git.pull();
		pc.setProgressMonitor(gitMonitor);
		pc.setCredentialsProvider(credentials);
		pc.setTransportConfigCallback(new TransportConfigCallback() {
			@Override
			public void configure(Transport t) {
				credentials.setUri(t.getURI());
				if (t instanceof TransportHttp && cookie != null) {
					HashMap<String, String> map = new HashMap<String, String>();
					map.put(GitConstants.KEY_COOKIE, cookie.getName() + "=" + cookie.getValue());
					((TransportHttp) t).setAdditionalHeaders(map);
				}
			}
		});
		PullResult pullResult = pc.call();

		if (monitor.isCanceled()) {
			return new Status(IStatus.CANCEL, GitActivator.PI_GIT, "Cancelled");
		}

		// handle result
		if (pullResult.isSuccessful()) {
			return Status.OK_STATUS;
		}
		FetchResult fetchResult = pullResult.getFetchResult();

		IStatus fetchStatus = FetchJob.handleFetchResult(fetchResult);
		if (!fetchStatus.isOK()) {
			return fetchStatus;
		}

		MergeStatus mergeStatus = pullResult.getMergeResult().getMergeStatus();
		if (!mergeStatus.isSuccessful())
			return new Status(IStatus.ERROR, GitActivator.PI_GIT, mergeStatus.name());
	} finally {
		if (db != null) {
			db.close();
		}
	}
	return Status.OK_STATUS;
}
 
Example 6
Source File: Pull.java    From wandora with GNU General Public License v3.0 4 votes vote down vote up
@Override
public void execute(Wandora wandora, Context context) {

    try {
        Git git = getGit();
        if(git != null) {
            if(isNotEmpty(getGitRemoteUrl())) {
                PullCommand pull = git.pull();
                String user = getUsername();
                if(user == null) {
                    if(pullUI == null) {
                        pullUI = new PullUI();
                    }
                    pullUI.setUsername(getUsername());
                    pullUI.setPassword(getPassword());
                    pullUI.setRemoteUrl(getGitRemoteUrl());

                    pullUI.openInDialog();

                    if(pullUI.wasAccepted()) {
                        setUsername(pullUI.getUsername());
                        setPassword(pullUI.getPassword());
                        // setGitRemoteUrl(pullUI.getRemoteUrl());    
                        
                        // pull.setRemote(pullUI.getRemoteUrl());
                        if(isNotEmpty(getUsername())) {
                            CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider( getUsername(), getPassword() );
                            pull.setCredentialsProvider(credentialsProvider);
                        }
                    }
                    else {
                        return;
                    }
                }

                setDefaultLogger();
                setLogTitle("Git pull");

                log("Pulling changes from remote repository...");
                PullResult result = pull.call();

                FetchResult fetchResult = result.getFetchResult();
                MergeResult mergeResult = result.getMergeResult();
                MergeStatus mergeStatus = mergeResult.getMergeStatus();

                String fetchResultMessages = fetchResult.getMessages();
                if(isNotEmpty(fetchResultMessages)) {
                    log(fetchResult.getMessages());
                }
                log(mergeStatus.toString());

                if(mergeStatus.equals(MergeStatus.MERGED)) {
                    int a = WandoraOptionPane.showConfirmDialog(wandora, "Reload Wandora project after pull?", "Reload Wandora project after pull?", WandoraOptionPane.YES_NO_OPTION);
                    if(a == WandoraOptionPane.YES_OPTION) {
                        reloadWandoraProject();
                    }
                }
                log("Ready.");
            }
            else {
                log("Repository has no remote origin and can't be pulled. " 
                    + "Initialize repository by cloning remote repository to set the remote origin.");
            }
        }
        else {
            logAboutMissingGitRepository();
        }
    }
    catch(GitAPIException gae) {
        log(gae.toString());
    }
    catch(NoWorkTreeException nwte) {
        log(nwte.toString());
    }
    catch(Exception e) {
        log(e);
    }
    setState(WAIT);
}