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

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

Introduction

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

Prototype

@Override
public RevCommit call() throws GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException 

Source Link

Document

Executes the commit command with all the options and parameters collected by the setter methods of this class.

Usage

From source file:org.exist.git.xquery.Commit.java

License:Open Source License

@Override
public Sequence eval(Sequence[] args, Sequence contextSequence) throws XPathException {

    try {/*  w  ww  .  ja v  a  2 s.  c  o  m*/
        String localPath = args[0].getStringValue();
        if (!(localPath.endsWith("/")))
            localPath += File.separator;

        Git git = Git.open(new Resource(localPath), FS);

        CommitCommand command = git.commit().setMessage(args[1].getStringValue());
        //              .setAuthor(name, email)
        //              .setCommitter(name, email)

        if (args.length >= 3) {
            for (int i = 0; i < args[2].getItemCount(); i++) {
                command.setOnly(args[2].itemAt(i).getStringValue());
            }
        }
        //           command.setAll(true);
        command.call();

        return BooleanValue.TRUE;
    } catch (Throwable e) {
        Throwable cause = e.getCause();
        if (cause != null) {
            throw new XPathException(this, Module.EXGIT001, cause.getMessage());
        }

        throw new XPathException(this, Module.EXGIT001, e);
    }
}

From source file:org.flowerplatform.web.git.operation.CommitOperation.java

License:Open Source License

public boolean commit(String repositoryLocation, List<CommitResourceDto> files, String author, String committer,
        String message, boolean amending) {
    ProgressMonitor monitor = ProgressMonitor
            .create(GitPlugin.getInstance().getMessage("git.commit.monitor.title"), channel);

    try {/*from   w  w  w  . j ava  2 s  .co  m*/
        Repository repo = GitPlugin.getInstance().getUtils().getRepository(new File(repositoryLocation));

        Collection<String> notTracked = new HashSet<String>();
        Collection<String> resources = new HashSet<String>();

        for (CommitResourceDto file : files) {
            resources.add(file.getPath());
            if (file.getState() == CommitResourceDto.UNTRACKED) {
                notTracked.add(file.getPath());
            }
        }

        monitor.beginTask(GitPlugin.getInstance().getMessage("git.commit.monitor.message"), 10);
        addUntracked(notTracked, repo);
        monitor.worked(1);

        CommitCommand commitCommand = new Git(repo).commit();
        commitCommand.setAmend(amending).setMessage(message);

        for (String path : resources) {
            commitCommand.setOnly(path);
        }

        Date commitDate = new Date();
        TimeZone timeZone = TimeZone.getDefault();

        PersonIdent enteredAuthor = RawParseUtils.parsePersonIdent(author);
        PersonIdent enteredCommitter = RawParseUtils.parsePersonIdent(committer);
        if (enteredAuthor == null) {
            channel.appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                    CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance()
                            .getMessage("git.commit.errorParsingPersonIdent", new Object[] { author }),
                    DisplaySimpleMessageClientCommand.ICON_ERROR));
            return false;
        }
        if (enteredCommitter == null) {
            channel.appendOrSendCommand(new DisplaySimpleMessageClientCommand(
                    CommonPlugin.getInstance().getMessage("error"), GitPlugin.getInstance()
                            .getMessage("git.commit.errorParsingPersonIdent", new Object[] { committer }),
                    DisplaySimpleMessageClientCommand.ICON_ERROR));
            return false;
        }

        PersonIdent authorIdent = new PersonIdent(enteredAuthor, commitDate, timeZone);
        PersonIdent committerIdent = new PersonIdent(enteredCommitter, commitDate, timeZone);

        if (amending) {
            RevCommit headCommit = GitPlugin.getInstance().getUtils().getHeadCommit(repo);
            if (headCommit != null) {
                PersonIdent headAuthor = headCommit.getAuthorIdent();
                authorIdent = new PersonIdent(enteredAuthor, headAuthor.getWhen(), headAuthor.getTimeZone());
            }
        }
        commitCommand.setAuthor(authorIdent);
        commitCommand.setCommitter(committerIdent);

        monitor.worked(1);
        commitCommand.call();
        if (monitor.isCanceled()) {
            return false;
        }
        monitor.worked(8);

        //         GitLightweightDecorator.refresh();
        //         
        //         updateDispatcher.dispatchContentUpdate(null, repo, GitTreeUpdateDispatcher.COMMIT, null);

        return true;
    } catch (Exception e) {
        logger.debug(GitPlugin.getInstance().getMessage("git.commit.error"), e);
        channel.appendOrSendCommand(
                new DisplaySimpleMessageClientCommand(CommonPlugin.getInstance().getMessage("error"),
                        e.getMessage(), DisplaySimpleMessageClientCommand.ICON_ERROR));
        return false;
    } finally {
        monitor.done();
    }
}

From source file:org.jabylon.team.git.GitTeamProvider.java

License:Open Source License

@Override
public void commit(ProjectVersion project, IProgressMonitor monitor) throws TeamProviderException {
    try {// w w w.j  ava2 s  .co  m
        Repository repository = createRepository(project);
        SubMonitor subMon = SubMonitor.convert(monitor, "Commit", 100);
        Git git = new Git(repository);
        // AddCommand addCommand = git.add();
        List<String> changedFiles = addNewFiles(git, subMon.newChild(30));
        if (!changedFiles.isEmpty()) {
            checkCanceled(subMon);
            CommitCommand commit = git.commit();
            Preferences node = PreferencesUtil.scopeFor(project.getParent());
            String username = node.get(GitConstants.KEY_USERNAME, "Jabylon");
            String email = node.get(GitConstants.KEY_EMAIL, "jabylon@example.org");
            String message = node.get(GitConstants.KEY_MESSAGE, "Auto Sync-up by Jabylon");
            boolean insertChangeId = node.getBoolean(GitConstants.KEY_INSERT_CHANGE_ID, false);
            commit.setAuthor(username, email);
            commit.setCommitter(username, email);
            commit.setInsertChangeId(insertChangeId);
            commit.setMessage(message);
            for (String path : changedFiles) {
                checkCanceled(subMon);
                commit.setOnly(path);

            }
            commit.call();
            subMon.worked(10);
        } else {
            LOGGER.info("No changed files, skipping commit phase");
        }
        checkCanceled(subMon);
        PushCommand push = git.push();
        URI uri = project.getParent().getRepositoryURI();
        if (uri != null)
            push.setRemote(stripUserInfo(uri).toString());
        push.setProgressMonitor(new ProgressMonitorWrapper(subMon.newChild(60)));
        if (!"https".equals(uri.scheme()) && !"http".equals(uri.scheme()))
            push.setTransportConfigCallback(createTransportConfigCallback(project.getParent()));
        push.setCredentialsProvider(createCredentialsProvider(project.getParent()));

        RefSpec spec = createRefSpec(project);
        push.setRefSpecs(spec);

        Iterable<PushResult> result = push.call();
        for (PushResult r : result) {
            for (RemoteRefUpdate rru : r.getRemoteUpdates()) {
                if (rru.getStatus() != RemoteRefUpdate.Status.OK
                        && rru.getStatus() != RemoteRefUpdate.Status.UP_TO_DATE) {
                    String error = "Push failed: " + rru.getStatus();
                    LOGGER.error(error);
                    throw new TeamProviderException(error);
                }
            }
        }

        Ref ref = repository.getRef(project.getName());
        if (ref != null) {
            LOGGER.info("Successfully pushed {} to {}", ref.getObjectId(),
                    project.getParent().getRepositoryURI());
        }
    } catch (NoHeadException e) {
        throw new TeamProviderException(e);
    } catch (NoMessageException e) {
        throw new TeamProviderException(e);
    } catch (ConcurrentRefUpdateException e) {
        throw new TeamProviderException(e);
    } catch (JGitInternalException e) {
        throw new TeamProviderException(e);
    } catch (WrongRepositoryStateException e) {
        throw new TeamProviderException(e);
    } catch (InvalidRemoteException e) {
        throw new TeamProviderException(e);
    } catch (IOException e) {
        throw new TeamProviderException(e);
    } catch (GitAPIException e) {
        throw new TeamProviderException(e);
    } finally {
        if (monitor != null)
            monitor.done();
    }
}

From source file:org.jboss.arquillian.ce.template.GitAdapter.java

License:Open Source License

public GitAdapter commit() throws Exception {
    CommitCommand commit = git.commit();
    commit.setMessage(String.format("Testing ... %s", new Date()));
    commit.call();
    return this;
}

From source file:org.jboss.arquillian.container.openshift.express.util.GitUtil.java

License:Apache License

/**
 * Commits changes to a local repository
 *
 * @param identification The person identification
 * @param message the commit message/*from  ww w  . j a  v a 2s . c  om*/
 */
public void commit(PersonIdent identification, String message) {
    CommitCommand commit = git.commit();
    commit.setAuthor(identification);
    commit.setCommitter(identification);
    commit.setMessage(message);
    try {
        commit.call();
    } catch (NoHeadException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (NoMessageException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (UnmergedPathException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (ConcurrentRefUpdateException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (JGitInternalException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (WrongRepositoryStateException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    }
}

From source file:org.jboss.arquillian.container.openshift.util.GitUtil.java

License:Apache License

/**
 * Commits changes to a local repository
 *
 * @param identification The person identification
 * @param message the commit message/* w  ww. jav  a2  s.co m*/
 */
public void commit(PersonIdent identification, String message) {
    CommitCommand commit = git.commit();
    commit.setAuthor(identification);
    commit.setCommitter(identification);
    commit.setMessage(message);
    try {
        commit.call();
    } catch (NoHeadException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (NoMessageException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (ConcurrentRefUpdateException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (JGitInternalException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (WrongRepositoryStateException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (UnmergedPathsException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    } catch (GitAPIException e) {
        throw new IllegalStateException("Unable to commit into Git repository", e);
    }
}

From source file:org.jboss.forge.rest.main.GitCommandCompletePostProcessor.java

License:Apache License

protected RevCommit doCommitAndPush(Git git, String message, CredentialsProvider credentials,
        PersonIdent author, String remote, String branch) throws IOException, GitAPIException {
    CommitCommand commit = git.commit().setAll(true).setMessage(message);
    if (author != null) {
        commit = commit.setAuthor(author);
    }//from w w w .j  a va  2s.co  m

    RevCommit answer = commit.call();
    if (LOG.isDebugEnabled()) {
        LOG.debug("Committed " + answer.getId() + " " + answer.getFullMessage());
    }

    if (isPushOnCommit()) {
        Iterable<PushResult> results = git.push().setCredentialsProvider(credentials).setRemote(remote).call();
        for (PushResult result : results) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Pushed " + result.getMessages() + " " + result.getURI() + " branch: " + branch
                        + " updates: " + GitHelpers.toString(result.getRemoteUpdates()));
            }
        }
    }
    return answer;

}

From source file:org.jboss.tools.openshift.egit.internal.test.util.TestRepository.java

License:Open Source License

/**
 * Commits the current index//  w ww  .j a va  2 s  .  c o m
 * 
 * @param message
 *            commit message
 * @return commit object
 * 
 * @throws UnmergedPathException
 * @throws JGitInternalException
 * @throws GitAPIException 
 * @throws UnmergedPathsException 
 */
public RevCommit commit(String message)
        throws UnmergedPathException, JGitInternalException, UnmergedPathsException, GitAPIException {
    Git git = new Git(repository);
    CommitCommand commitCommand = git.commit();
    commitCommand.setAuthor("J. Git", "j.git@egit.org");
    commitCommand.setCommitter(commitCommand.getAuthor());
    commitCommand.setMessage(message);
    return commitCommand.call();
}

From source file:org.kuali.student.git.model.TestSvnRevisionMapper.java

License:Educational Community License

private RevCommit createFileContentAndCommit(String fileName, String fileContent)
        throws NoWorkTreeException, IOException, NoHeadException, NoMessageException, UnmergedPathsException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, GitAPIException {

    FileUtils.write(new File(repo.getWorkTree(), fileName), fileContent);

    AddCommand addCommand = git.add();//from  w  ww  .  j  a  v a 2 s .co  m

    addCommand.addFilepattern(fileName);

    addCommand.call();

    CommitCommand commitCommand = git.commit();

    commitCommand.setAuthor(new PersonIdent("test", "test@kuali.org"));

    commitCommand.setMessage("test commit");

    return commitCommand.call();

}

From source file:org.moxie.ant.Main.java

License:Apache License

private void initGit() throws GitAPIException {
    // create the repository
    InitCommand init = Git.init();//from  w ww . ja v a  2s .com
    init.setBare(false);
    init.setDirectory(newProject.dir);
    Git git = init.call();

    if (!StringUtils.isEmpty(newProject.gitOrigin)) {
        // set the origin and configure the master branch for merging 
        StoredConfig config = git.getRepository().getConfig();
        config.setString("remote", "origin", "url", getGitUrl());
        config.setString("remote", "origin", "fetch", "+refs/heads/*:refs/remotes/origin/*");
        config.setString("branch", "master", "remote", "origin");
        config.setString("branch", "master", "merge", "refs/heads/master");
        try {
            config.save();
        } catch (IOException e) {
            throw new MoxieException(e);
        }
    }

    // prepare a common gitignore file
    StringBuilder sb = new StringBuilder();
    sb.append("/.directory\n");
    sb.append("/.DS_STORE\n");
    sb.append("/.DS_Store\n");
    sb.append("/.settings\n");
    sb.append("/bin\n");
    sb.append("/build\n");
    sb.append("/ext\n");
    sb.append("/target\n");
    sb.append("/tmp\n");
    sb.append("/temp\n");
    if (!newProject.eclipse.includeClasspath()) {
        // ignore hard-coded .classpath
        sb.append("/.classpath\n");
    }
    FileUtils.writeContent(new File(newProject.dir, ".gitignore"), sb.toString());

    AddCommand add = git.add();
    add.addFilepattern("build.xml");
    add.addFilepattern("build.moxie");
    add.addFilepattern(".gitignore");
    if (newProject.eclipse.includeProject()) {
        add.addFilepattern(".project");
    }
    if (newProject.eclipse.includeClasspath()) {
        // MOXIE_HOME relative dependencies in .classpath
        add.addFilepattern(".classpath");
    }
    if (newProject.idea.includeProject()) {
        add.addFilepattern(".project");
    }
    if (newProject.idea.includeClasspath()) {
        // MOXIE_HOME relative dependencies in .iml
        add.addFilepattern("*.iml");
    }
    try {
        add.call();
        CommitCommand commit = git.commit();
        PersonIdent moxie = new PersonIdent("Moxie", "moxie@localhost");
        commit.setAuthor(moxie);
        commit.setCommitter(moxie);
        commit.setMessage("Project structure created");
        commit.call();
    } catch (Exception e) {
        throw new MoxieException(e);
    }
}