Example usage for org.eclipse.jgit.api Git push

List of usage examples for org.eclipse.jgit.api Git push

Introduction

In this page you can find the example usage for org.eclipse.jgit.api Git push.

Prototype

public PushCommand push() 

Source Link

Document

Return a command object to execute a Push command

Usage

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void changeAndRenameFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.append("\nchanged");
    }//from w  ww  . j  ava  2s . c  o m

    if (!file2.renameTo(file2.toPath().resolveSibling("file3.adoc").toFile()))
        throw new IOException("Could not rename file2 to file3");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.add().addFilepattern(".").call();
    git.commit().setMessage("changed file2 and renamed to file3").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void addTestCommits() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.write("hello world");
    }//from  w  w  w . j a v a  2 s . c om
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.append("\nhello world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.write("hi world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated").call();
    git.push().setRemote("origin").add("master").call();

    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void createNextCommit() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1, true)) {
        writer.write("hi world!\nhello hi");
    }//from w  w w . j a v  a  2  s  . c o  m
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file1").call();

    try (FileWriter writer = new FileWriter(file2, true)) {
        writer.write("world");
    }
    git.add().addFilepattern(".").call();
    git.commit().setMessage("updated file2").call();

    git.push().setRemote("origin").add("master").call();

    git.close();
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void renameFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.renameTo(file2.toPath().resolveSibling("file3.adoc").toFile()))
        throw new IOException("Could not rename file2 to file3");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.add().addFilepattern(".").call();
    git.commit().setMessage("renamed file2 to file3").call();
    git.push().setRemote("origin").add("master").call();
    git.close();/*from   w  w w.  ja va  2  s.  c o  m*/
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void deleteFile() throws IOException, GitAPIException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    if (!file2.delete())
        throw new IOException("Could not delete file2");

    git.add().setUpdate(true).addFilepattern(".").call();
    git.commit().setMessage("deleted file2").call();
    git.push().setRemote("origin").add("master").call();
    git.close();//from   w w w  . j  a v a2s . c  om
}

From source file:com.sebastian_daschner.asciiblog.business.source.control.GitExtractorTest.java

License:Apache License

private void addNotRelevantFile(final String fileName) throws GitAPIException, IOException {
    final Git git = Git.open(gitCloneDirectory);
    git.pull().setRemote("origin").setRemoteBranchName("master").call();

    try (FileWriter writer = new FileWriter(file1.toPath().resolveSibling(fileName + ".mustache").toFile(),
            true)) {//from   w  w  w. ja va 2s.  c o m
        writer.write("hello {{world}}");
    }

    git.add().addFilepattern(".").call();
    git.commit().setMessage("added template").call();
    git.push().setRemote("origin").add("master").call();
    git.close();
}

From source file:com.surevine.gateway.scm.git.jgit.JGitGitFacade.java

License:Open Source License

@Override
public void push(final LocalRepoBean repoBean, final String remoteName) throws GitException {
    try {//from  ww w  .  ja v  a2s. c  o  m
        FileRepositoryBuilder builder = new FileRepositoryBuilder();
        Repository repository = builder.setGitDir(repoBean.getRepoDirectory().toFile()).findGitDir().build();
        Git git = new org.eclipse.jgit.api.Git(repository);

        // dry run the push - if the push cannot be done automatically we have no way to recover so we just log
        // and throw an exception
        PushCommand dryRunPushCommand = git.push();
        dryRunPushCommand.setRemote(remoteName);
        dryRunPushCommand.setDryRun(true);
        Iterable<PushResult> dryRunResult = dryRunPushCommand.call();
        for (PushResult result : dryRunResult) {
            for (RemoteRefUpdate remoteRefUpdate : result.getRemoteUpdates()) {
                switch (remoteRefUpdate.getStatus()) {
                case OK:
                case UP_TO_DATE:
                    continue;
                default:
                    throw new GitException("During a dry run of a push one of the updates would have failed "
                            + "so the push was aborted for repo " + repoBean + " to remote "
                            + dryRunPushCommand.getRemote());
                }
            }
        }

        // if we get to this point the dry run was OK so try the real thing
        PushCommand realPushCommand = git.push();
        realPushCommand.setRemote(remoteName);
        Iterable<PushResult> pushResults = realPushCommand.call();
        for (PushResult result : pushResults) {
            for (RemoteRefUpdate remoteRefUpdate : result.getRemoteUpdates()) {
                switch (remoteRefUpdate.getStatus()) {
                case OK:
                case UP_TO_DATE:
                    continue;
                default:
                    throw new GitException(
                            "Push failed for " + repoBean + " to remote " + dryRunPushCommand.getRemote());
                }
            }
        }
    } catch (GitAPIException | IOException e) {
        LOGGER.error(e);
        throw new GitException(e);
    }
}

From source file:com.tasktop.c2c.server.internal.profile.service.template.GitServiceCloner.java

License:Open Source License

private void copyRepo(ScmRepository scmRepo, CloneContext context)
        throws IOException, JGitInternalException, GitAPIException {

    File workDirectory = null;/*from   w w w .  ja v  a 2  s. c o m*/
    try {
        Project templateProject = context.getTemplateService().getProjectServiceProfile().getProject();
        String cloneUrl = jgitProvider.computeRepositoryUrl(templateProject.getIdentifier(), scmRepo.getName());

        AuthUtils.assumeSystemIdentity(templateProject.getIdentifier());
        tenancyManager.establishTenancyContext(context.getTemplateService());

        workDirectory = createTempDirectory();
        Git git = Git.cloneRepository().setDirectory(workDirectory)
                .setBranch(Constants.R_HEADS + Constants.MASTER).setURI(cloneUrl).call();

        AuthUtils.assumeSystemIdentity(
                context.getTargetService().getProjectServiceProfile().getProject().getIdentifier());
        tenancyManager.establishTenancyContext(context.getTargetService());

        FileUtils.deleteDirectory(git.getRepository().getDirectory());

        git = Git.init().setDirectory(git.getRepository().getDirectory().getParentFile()).call();

        maybeRewriteRepo(workDirectory, context);

        String pushUrl = jgitProvider.computeRepositoryUrl(
                context.getTargetService().getProjectServiceProfile().getProject().getIdentifier(),
                scmRepo.getName());

        // FIXME: User's locale is not defined here
        String commitMessage = messageSource.getMessage("project.template.git.commitMessage",
                new Object[] { templateProject.getName() }, null);

        git.add().addFilepattern(".").call();
        git.commit().setCommitter(committerName, committerEmail).setMessage(commitMessage).call();
        git.getRepository().getConfig().setString("remote", "target", "url", pushUrl);
        git.push().setRemote("target").setPushAll().call();
    } finally {
        if (workDirectory != null) {
            FileUtils.deleteDirectory(workDirectory);
        }
    }
}

From source file:com.tasktop.c2c.server.scm.service.GitServiceTestBase.java

License:Open Source License

protected void commitAndPushFile(Git git, String path, String content, String message)
        throws GitAPIException, JGitInternalException, IOException {
    File f = new File(git.getRepository().getDirectory().getParentFile(), path);
    ensureDirExists(f.getParentFile());//w  ww.j ava  2s . co m
    FileOutputStream writer = new FileOutputStream(f);
    writer.write(content.getBytes());
    writer.close();
    git.add().addFilepattern(path).call();
    git.commit().setMessage(message).call();
    git.push().call();
}

From source file:com.tenxdev.ovcs.command.AbstractOvcsCommand.java

License:Open Source License

/**
 * Push all committed changes to the remote repository
 *
 * @param git/*from w  w  w.  j a v  a  2s  .c  om*/
 *            the local git repository
 * @throws GitAPIException
 *             if the push operation failed
 */
protected void doPush(final Git git) throws GitAPIException {
    final Iterable<PushResult> pushResults = git.push().setRemote("origin").add("master")
            .setProgressMonitor(new TextProgressMonitor()).call();
    for (final PushResult pushResult : pushResults) {
        final String messages = pushResult.getMessages();
        if (!"".equals(messages)) {
            System.out.println();
        }
    }
}