Example usage for org.eclipse.jgit.api PushCommand call

List of usage examples for org.eclipse.jgit.api PushCommand call

Introduction

In this page you can find the example usage for org.eclipse.jgit.api PushCommand call.

Prototype

@Override
public Iterable<PushResult> call()
        throws GitAPIException, InvalidRemoteException, org.eclipse.jgit.api.errors.TransportException 

Source Link

Document

Execute the push command with all the options and parameters collected by the setter methods of this class.

Usage

From source file:org.zanata.sync.plugin.git.service.impl.GitSyncService.java

License:Open Source License

@Override
public void syncTranslationToRepo(String repoUrl, String branch, File baseDir) throws RepoSyncException {
    try {/* w  w  w  .ja  va2s.c o m*/
        Git git = Git.open(baseDir);
        StatusCommand statusCommand = git.status();
        Status status = statusCommand.call();
        Set<String> uncommittedChanges = status.getUncommittedChanges();
        uncommittedChanges.addAll(status.getUntracked());
        if (!uncommittedChanges.isEmpty()) {
            log.info("uncommitted files in git repo: {}", uncommittedChanges);
            AddCommand addCommand = git.add();
            addCommand.addFilepattern(".");
            addCommand.call();

            log.info("commit changed files");
            CommitCommand commitCommand = git.commit();
            commitCommand.setCommitter("Zanata Auto Repo Sync", "zanata-users@redhat.com");
            commitCommand.setMessage("Zanata Auto Repo Sync (pushing translations)");
            commitCommand.call();

            log.info("push to remote repo");
            PushCommand pushCommand = git.push();
            UsernamePasswordCredentialsProvider user = new UsernamePasswordCredentialsProvider(
                    getCredentials().getUsername(), getCredentials().getSecret());
            pushCommand.setCredentialsProvider(user);
            pushCommand.call();
        } else {
            log.info("nothing changed so nothing to do");
        }
    } catch (IOException e) {
        throw new RepoSyncException("failed opening " + baseDir + " as git repo", e);
    } catch (GitAPIException e) {
        throw new RepoSyncException("Failed committing translations into the repo", e);
    }
}

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

License:Open Source License

@Override
protected boolean doExecute() {
    Repository repo = null;//  w  ww. j  ava2  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;
}

From source file:ru.nikitenkogleb.androidtools.newappwizard.GitSupport.java

/**   Send changes to remote repository */
final void close(String userName, String userEmail, String initMessage) {
    if (!isSuccessful())
        return;//from w w  w.  ja v  a  2s .  c o  m
    FileRepository mRepository = null;
    Git mGit = null;
    try {

        mRepository = new FileRepository(new File(mProjectPath, ".git"));
        mGit = new Git(mRepository);

        mGit.add().addFilepattern(".").call();
        mGit.commit().setAll(true).setAuthor(userName, userEmail).setCommitter(userName, userEmail)
                .setMessage(initMessage).call();
        final PushCommand pushCommand = mGit.push().setRemote("origin").setPushAll();

        if (mSshConfigCallback != null)
            pushCommand.setTransportConfigCallback(mSshConfigCallback);
        else
            pushCommand.setCredentialsProvider(mHttpsCredentialsProvider);

        pushCommand.call();
    } catch (GitAPIException | IOException e) {
        e.printStackTrace();
    }

    if (mGit != null)
        mGit.close();
    if (mRepository != null)
        mRepository.close();
}