Example usage for org.eclipse.jgit.transport PushResult getAdvertisedRefs

List of usage examples for org.eclipse.jgit.transport PushResult getAdvertisedRefs

Introduction

In this page you can find the example usage for org.eclipse.jgit.transport PushResult getAdvertisedRefs.

Prototype

public Collection<Ref> getAdvertisedRefs() 

Source Link

Document

Get the complete list of refs advertised by the remote.

Usage

From source file:org.zend.sdkcli.internal.commands.GitPushApplicationCommand.java

License:Open Source License

@Override
protected boolean doExecute() {
    Repository repo = null;/*from w ww. ja  v a 2  s  . com*/
    try {
        File gitDir = getRepo();
        if (!gitDir.exists()) {
            getLogger().error("Git repository is not available in provided location");
            return false;
        }
        repo = FileRepositoryBuilder.create(getRepo());
    } catch (IOException e) {
        getLogger().error(e);
        return false;
    }
    if (repo != null) {
        Git git = new Git(repo);

        String remote = doGetRemote(repo);
        if (remote == null) {
            getLogger().error("Invalid remote value: " + getRemote());
            return false;
        }

        // perform operation only if it is clone of phpCloud repository
        String repoUrl = git.getRepository().getConfig().getString("remote", remote, "url");

        AddCommand addCommand = git.add();
        addCommand.setUpdate(false);
        // add all new files
        addCommand.addFilepattern(".");
        try {
            addCommand.call();
        } catch (NoFilepatternException e) {
            // should not occur because '.' is used
            getLogger().error(e);
            return false;
        } catch (GitAPIException e) {
            getLogger().error(e);
            return false;
        }

        CommitCommand commitCommand = git.commit();
        // automatically stage files that have been modified and deleted
        commitCommand.setAll(true);
        PersonIdent ident = getPersonalIdent(repoUrl);
        if (ident == null) {
            getLogger().error("Invalid author information provided: " + getAuthor());
            return false;
        }
        commitCommand.setAuthor(ident);
        commitCommand.setInsertChangeId(true);
        commitCommand.setMessage(getMessage());
        try {
            commitCommand.call();
        } catch (Exception e) {
            getLogger().error(e);
            return false;
        }

        // at the end push all changes
        PushCommand pushCommand = git.push();
        pushCommand.setPushAll();
        pushCommand.setRemote(remote);
        pushCommand.setProgressMonitor(new TextProgressMonitor(new PrintWriter(System.out)));

        try {
            URIish uri = new URIish(repoUrl);
            if (GITHUB_HOST.equals(uri.getHost()) && "git".equals(uri.getUser())) {
                if (!prepareSSHFactory()) {
                    return false;
                }
            } else {
                CredentialsProvider credentials = getCredentials(repoUrl);
                if (credentials != null) {
                    pushCommand.setCredentialsProvider(credentials);
                }
            }
            Iterable<PushResult> result = pushCommand.call();
            for (PushResult pushResult : result) {
                pushResult.getAdvertisedRefs();
                Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates();
                for (RemoteRefUpdate remoteRefUpdate : updates) {
                    TrackingRefUpdate trackingRefUpdate = remoteRefUpdate.getTrackingRefUpdate();
                    getLogger().info(MessageFormat.format("Remote name: {0}, status: {1}",
                            remoteRefUpdate.getRemoteName(), remoteRefUpdate.getStatus().toString()));
                    getLogger().info(MessageFormat.format("Remote name: {0}, result: {1}",
                            trackingRefUpdate.getRemoteName(), trackingRefUpdate.getResult().toString()));
                }
            }
        } catch (JGitInternalException e) {
            getLogger().error(e);
            return false;
        } catch (InvalidRemoteException e) {
            // should not occur because selected remote is available
            getLogger().error(e);
            return false;
        } catch (URISyntaxException e) {
            getLogger().error(e);
            return false;
        } catch (TransportException e) {
            getLogger().error(e);
            return false;
        } catch (GitAPIException e) {
            getLogger().error(e);
            return false;
        }

    }
    return true;
}