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

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

Introduction

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

Prototype

public PushCommand setPushAll() 

Source Link

Document

Push all branches under refs/heads/*.

Usage

From source file:com.photon.phresco.framework.impl.SCMManagerImpl.java

License:Apache License

private void importToGITRepo(RepoDetail repodetail, ApplicationInfo appInfo, File appDir) throws Exception {
    if (debugEnabled) {
        S_LOGGER.debug("Entering Method  SCMManagerImpl.importToGITRepo()");
    }//from   w ww . java 2  s. co m
    boolean gitExists = false;
    if (new File(appDir.getPath() + FORWARD_SLASH + DOT + GIT).exists()) {
        gitExists = true;
    }
    try {
        //For https and ssh
        additionalAuthentication(repodetail.getPassPhrase());

        CredentialsProvider cp = new UsernamePasswordCredentialsProvider(repodetail.getUserName(),
                repodetail.getPassword());

        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(appDir).readEnvironment().findGitDir().build();
        String dirPath = appDir.getPath();
        File gitignore = new File(dirPath + GITIGNORE_FILE);
        gitignore.createNewFile();
        if (gitignore.exists()) {
            String contents = FileUtils.readFileToString(gitignore);
            if (!contents.isEmpty() && !contents.contains(DO_NOT_CHECKIN_DIR)) {
                String source = NEWLINE + DO_NOT_CHECKIN_DIR + NEWLINE;
                OutputStream out = new FileOutputStream((dirPath + GITIGNORE_FILE), true);
                byte buf[] = source.getBytes();
                out.write(buf);
                out.close();
            } else if (contents.isEmpty()) {
                String source = NEWLINE + DO_NOT_CHECKIN_DIR + NEWLINE;
                OutputStream out = new FileOutputStream((dirPath + GITIGNORE_FILE), true);
                byte buf[] = source.getBytes();
                out.write(buf);
                out.close();
            }
        }

        Git git = new Git(repository);

        InitCommand initCommand = Git.init();
        initCommand.setDirectory(appDir);
        git = initCommand.call();

        AddCommand add = git.add();
        add.addFilepattern(".");
        add.call();

        CommitCommand commit = git.commit().setAll(true);
        commit.setMessage(repodetail.getCommitMessage()).call();
        StoredConfig config = git.getRepository().getConfig();

        config.setString(REMOTE, ORIGIN, URL, repodetail.getRepoUrl());
        config.setString(REMOTE, ORIGIN, FETCH, REFS_HEADS_REMOTE_ORIGIN);
        config.setString(BRANCH, MASTER, REMOTE, ORIGIN);
        config.setString(BRANCH, MASTER, MERGE, REF_HEAD_MASTER);
        config.save();

        try {
            PushCommand pc = git.push();
            pc.setCredentialsProvider(cp).setForce(true);
            pc.setPushAll().call();
        } catch (Exception e) {
            git.getRepository().close();
            PomProcessor appPomProcessor = new PomProcessor(new File(appDir, appInfo.getPhrescoPomFile()));
            appPomProcessor.removeProperty(Constants.POM_PROP_KEY_SRC_REPO_URL);
            appPomProcessor.save();
            throw e;
        }

        if (appInfo != null) {
            updateSCMConnection(appInfo, repodetail.getRepoUrl());
        }
        git.getRepository().close();
    } catch (Exception e) {
        Exception s = e;
        resetLocalCommit(appDir, gitExists, e);
        throw s;
    }
}

From source file:org.ajoberstar.gradle.git.tasks.GitPush.java

License:Apache License

/**
 * Pushes changes to a remote repository.
 *//*from w w  w .j  a  v  a2 s. co  m*/
@TaskAction
public void push() {
    PushCommand cmd = getGit().push();
    TransportAuthUtil.configure(cmd, this);
    cmd.setRemote(getRemote());
    if (isPushTags()) {
        cmd.setPushTags();
    }
    if (isPushAll()) {
        cmd.setPushAll();
    }
    cmd.setForce(isForce());
    try {
        cmd.call();
    } catch (Exception e) {
        throw new GradleException("Problem pushing to repository.", e);
    }
    //TODO add progress monitor to go to Gradle status bar
}

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

License:Open Source License

@Override
protected boolean doExecute() {
    Repository repo = null;//  w ww .jav  a 2s .  c  om
    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;
}