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

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

Introduction

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

Prototype

public CommitCommand setAuthor(String name, String email) 

Source Link

Document

Sets the author for this commit .

Usage

From source file:com.cisco.step.jenkins.plugins.jenkow.JenkowWorkflowRepository.java

License:Open Source License

private void addAndCommit(Repository r, String filePattern, String msg) throws IOException {
    try {//from w w  w  .  j  a va 2  s.c o  m
        Git git = new Git(r);
        AddCommand cmd = git.add();
        cmd.addFilepattern(filePattern);
        cmd.call();

        CommitCommand co = git.commit();
        co.setAuthor("Jenkow", "noreply@jenkins-ci.org");
        co.setMessage(msg);
        co.call();
    } catch (GitAPIException e) {
        LOGGER.log(Level.WARNING, "Adding seeded jenkow-repository content to Git failed", e);
    }
}

From source file:com.sap.dirigible.ide.jgit.connector.JGitConnector.java

License:Open Source License

/**
 * /*from w ww. j  a v a 2s. com*/
 * Adds changes to the staging index. Then makes commit.
 * 
 * @param message
 *            the commit message
 * @param name
 *            the name of the committer used for the commit
 * @param email
 *            the email of the committer used for the commit
 * @param all
 *            if set to true, command will automatically stages files that
 *            have been modified and deleted, but new files not known by the
 *            repository are not affected. This corresponds to the parameter
 *            -a on the command line.
 * 
 * @throws NoHeadException
 * @throws NoMessageException
 * @throws UnmergedPathsException
 * @throws ConcurrentRefUpdateException
 * @throws WrongRepositoryStateException
 * @throws GitAPIException
 * @throws IOException
 */
public void commit(String message, String name, String email, boolean all)
        throws NoHeadException, NoMessageException, UnmergedPathsException, ConcurrentRefUpdateException,
        WrongRepositoryStateException, GitAPIException, IOException {
    CommitCommand commitCommand = git.commit();
    commitCommand.setMessage(message);
    commitCommand.setCommitter(name, email);
    commitCommand.setAuthor(name, email);
    commitCommand.setAll(all);
    commitCommand.call();
}

From source file:edu.wustl.lookingglass.community.CommunityRepository.java

License:Open Source License

private void verify() throws IOException, GitAPIException, URISyntaxException {

    if ((this.remoteName != null) && (this.remoteURL != null)) {
        this.addRemote(this.remoteName, this.remoteURL);
    }//from   w  w w .j a v  a  2s .c  o m

    String head = this.git.getRepository().getFullBranch();
    boolean headIsBranch = false;
    List<Ref> branches = this.git.branchList().call();
    for (Ref ref : branches) {
        if (head.equals(ref.getName())) {
            headIsBranch = true;
            break;
        }
    }

    RepositoryState state = this.git.getRepository().getRepositoryState();
    switch (state) {
    case SAFE:
        // Everything is good!
        break;
    case MERGING_RESOLVED:
    case CHERRY_PICKING_RESOLVED:
    case REVERTING_RESOLVED:
        // Commit this work!
        Logger.warning("commiting state: " + state + ".");
        CommitCommand commit = git.commit();
        if ((this.username != null) && (this.email != null)) {
            commit.setAuthor(this.username, this.email);
        }
        commit.call();
        break;
    case MERGING:
    case CHERRY_PICKING:
    case REVERTING:
    case REBASING:
    case REBASING_REBASING:
    case APPLY:
    case REBASING_MERGE:
    case REBASING_INTERACTIVE:
    case BISECTING:
        // Reset, because we can't sync with unresolved conflicts
        Logger.warning("unsafe repository state: " + state + ", reseting.");
        this.git.reset().setMode(ResetType.HARD).setRef(head).call();
        break;
    case BARE:
        throw new IllegalStateException("invalid repository state: " + state);
    default:
        throw new IllegalArgumentException("unknown merge repository state: " + state);
    }

    if (!headIsBranch) {
        if (branches.size() > 0) {
            try {
                this.git.branchCreate().setName(DEFAULT_BRANCH).call();
            } catch (RefAlreadyExistsException e) {
                // ignore
            }
            this.git.checkout().setName(DEFAULT_BRANCH).call();
        }
    }
}

From source file:edu.wustl.lookingglass.community.CommunityRepository.java

License:Open Source License

private void commit() throws GitAPIException, IOException {
    // Check for changed files
    this.git.add().setUpdate(true).addFilepattern(".").call();

    // Check for new files
    File gitignore = new File(this.repoDir, ".gitignore");
    if (gitignore.exists()) {
        this.git.add().addFilepattern(".").call();
    } else {//from  w  ww  .ja  v  a 2s .c  om
        // If there is no git ignore, then let's default to only adding lgp files
        Status status = this.git.status().call();
        AddCommand add = this.git.add();

        boolean newFiles = false;
        for (String untracked : status.getUntracked()) {
            if (untracked.toLowerCase().endsWith("." + IoUtilities.PROJECT_EXTENSION)) {
                add.addFilepattern(untracked);
                newFiles = true;
            }
        }

        if (newFiles) {
            add.call();
        }
    }

    Status status = this.git.status().call();
    if (!status.getChanged().isEmpty() || !status.getAdded().isEmpty() || !status.getRemoved().isEmpty()) {
        StringBuilder message = new StringBuilder();
        for (String changed : status.getChanged()) {
            message.append("/ ").append(changed).append(";\n");
        }
        for (String added : status.getAdded()) {
            message.append("+ ").append(added).append(";\n");
        }
        for (String removed : status.getRemoved()) {
            message.append("- ").append(removed).append(";\n");
        }

        CommitCommand commit = git.commit().setMessage(message.toString());
        if ((this.username != null) && (this.email != null)) {
            commit.setAuthor(this.username, this.email);
        }
        commit.call();
    }
}

From source file:gov.va.isaac.sync.git.SyncServiceGIT.java

License:Apache License

/**
 * @throws MergeFailure /*from  www .j a v a  2 s  .  c o  m*/
 * @throws AuthenticationException 
 * @see gov.va.isaac.interfaces.sync.ProfileSyncI#updateCommitAndPush(java.io.File, java.lang.String, java.lang.String, java.lang.String,
 * java.lang.String[])
 */
@Override
public Set<String> updateCommitAndPush(String commitMessage, String username, String password,
        MergeFailOption mergeFailOption, String... files)
        throws IllegalArgumentException, IOException, MergeFailure, AuthenticationException {
    try {
        log.info("Commit Files called {}", (files == null ? "-null-" : Arrays.toString(files)));
        Git git = getGit();

        if (git.status().call().getConflicting().size() > 0) {
            log.info("Previous merge failure not yet resolved");
            throw new MergeFailure(git.status().call().getConflicting(), new HashSet<>());
        }

        if (files == null) {
            files = git.status().call().getUncommittedChanges().toArray(new String[0]);
            log.info("Will commit the uncommitted files {}", Arrays.toString(files));
        }

        if (StringUtils.isEmptyOrNull(commitMessage) && files.length > 0) {
            throw new IllegalArgumentException("The commit message is required when files are specified");
        }

        if (files.length > 0) {
            CommitCommand commit = git.commit();
            for (String file : files) {
                commit.setOnly(file);
            }

            commit.setAuthor(username, "42");
            commit.setMessage(commitMessage);
            RevCommit rv = commit.call();
            log.debug("Local commit completed: " + rv.getFullMessage());
        }

        //need to merge origin/master into master now, prior to push
        Set<String> result = updateFromRemote(username, password, mergeFailOption);

        log.debug("Pushing");
        CredentialsProvider cp = new UsernamePasswordCredentialsProvider(username,
                (password == null ? new char[] {} : password.toCharArray()));

        Iterable<PushResult> pr = git.push().setCredentialsProvider(cp).call();
        pr.forEach(new Consumer<PushResult>() {
            @Override
            public void accept(PushResult t) {
                log.debug("Push Result Messages: " + t.getMessages());
            }
        });

        log.info("commit and push complete.  Current status: " + statusToString(git.status().call()));
        return result;
    } catch (TransportException te) {
        if (te.getMessage().contains("Auth fail")) {
            log.info("Auth fail", te);
            throw new AuthenticationException("Auth fail");
        } else {
            log.error("Unexpected", te);
            throw new IOException("Internal error", te);
        }
    } catch (GitAPIException e) {
        log.error("Unexpected", e);
        throw new IOException("Internal error", e);
    }
}

From source file:org.archicontribs.modelrepository.grafico.GraficoUtilsTests.java

License:Open Source License

@Test
public void getFileContents_IsCorrect() throws Exception {
    File localGitFolder = new File(getTempTestsFolder(), "testRepo");
    String contents = "Hello World!\nTesting.";

    try (Repository repo = GitHelper.createNewRepository(localGitFolder)) {
        File file = new File(localGitFolder, "test.txt");

        try (FileWriter fw = new FileWriter(file)) {
            fw.write(contents);/*  ww  w .  ja  v  a 2s  .  c  om*/
            fw.flush();
        }

        assertTrue(file.exists());

        // Add file to index
        AddCommand addCommand = new AddCommand(repo);
        addCommand.addFilepattern("."); //$NON-NLS-1$
        addCommand.setUpdate(false);
        addCommand.call();

        // Commit file
        CommitCommand commitCommand = Git.wrap(repo).commit();
        commitCommand.setAuthor("Test", "Test");
        commitCommand.setMessage("Message");
        commitCommand.call();

        assertEquals(contents, GraficoUtils.getFileContents(localGitFolder, "test.txt", "HEAD"));
    }
}

From source file:org.archicontribs.modelrepository.grafico.MergeConflictHandler.java

License:Open Source License

public void mergeAndCommit() throws IOException, GitAPIException {
    try (Git git = Git.open(fLocalGitFolder)) {
        if (fOurs != null && !fOurs.isEmpty()) {
            checkout(git, Stage.OURS, fOurs);
        }//from   w  w w. j  a v  a 2  s  .  c om
        if (fTheirs != null && !fTheirs.isEmpty()) {
            checkout(git, Stage.THEIRS, fTheirs);
        }

        // Add to index all files
        AddCommand addCommand = git.add();
        addCommand.addFilepattern("."); //$NON-NLS-1$
        addCommand.setUpdate(false);
        addCommand.call();

        // Commit
        CommitCommand commitCommand = git.commit();
        String userName = ModelRepositoryPlugin.INSTANCE.getPreferenceStore()
                .getString(IPreferenceConstants.PREFS_COMMIT_USER_NAME);
        String userEmail = ModelRepositoryPlugin.INSTANCE.getPreferenceStore()
                .getString(IPreferenceConstants.PREFS_COMMIT_USER_EMAIL);
        commitCommand.setAuthor(userName, userEmail);
        commitCommand.call();
    }
}

From source file:org.eclipse.egit.core.synchronize.GitSyncInfoTest.java

License:Open Source License

private RevCommit commit() throws Exception {
    Git git = new Git(repo);
    CommitCommand commit = git.commit();
    commit.setMessage("Initial  commit");
    commit.setAuthor("EGit", "egi@eclipse.org");
    return commit.call();
}

From source file:org.eclipse.egit.core.test.TestRepository.java

License:Open Source License

/**
 * Commits the current index/*  w  w w . j a va  2s.  c  o  m*/
 *
 * @param message
 *            commit message
 * @return commit object
 *
 * @throws NoHeadException
 * @throws NoMessageException
 * @throws UnmergedPathException
 * @throws ConcurrentRefUpdateException
 * @throws JGitInternalException
 * @throws WrongRepositoryStateException
 */
public RevCommit commit(String message) throws NoHeadException, NoMessageException, UnmergedPathException,
        ConcurrentRefUpdateException, JGitInternalException, WrongRepositoryStateException {
    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.eclipse.egit.ui.gitflow.AbstractGitflowHandlerTest.java

License:Open Source License

protected RevCommit setContentAddAndCommit(String newContent)
        throws Exception, GitAPIException, NoHeadException, NoMessageException, UnmergedPathsException,
        ConcurrentRefUpdateException, WrongRepositoryStateException, AbortedByHookException, IOException {
    setTestFileContent(newContent);/*w  w w  . j a v a 2  s  .c  om*/

    Git git = Git.wrap(repository);
    git.add().addFilepattern(".").call();
    CommitCommand commit = git.commit().setMessage(newContent);
    commit.setAuthor(TestUtil.TESTCOMMITTER_NAME, TestUtil.TESTCOMMITTER_EMAIL);
    commit.setCommitter(TestUtil.TESTCOMMITTER_NAME, TestUtil.TESTCOMMITTER_EMAIL);
    return commit.call();
}