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

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

Introduction

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

Prototype

public CommitCommand commit() 

Source Link

Document

Return a command object to execute a Commit 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");
    }//  w  ww.  j  a v  a 2  s .  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");
    }/*  w  ww  .  ja  va2s  . 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 ww . ja v  a  2s.  c  om*/
    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 .  co  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 ww w .  ja v a  2  s . c o m*/
}

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)) {/*  w w  w  .  j  av a2  s.  c  om*/
        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.stormcloud.ide.api.git.GitManager.java

License:Open Source License

@Override
public void commit(String repository, String message, String[] files, boolean all) throws GitManagerException {

    try {/*  w w w  .j  ava  2 s  .  c  o m*/

        Git git = Git.open(new File(repository));

        CommitCommand commit = git.commit();

        commit.setMessage(message);

        if (all) {

            commit.setAll(true);

        } else {

            for (String file : files) {

                commit.setOnly(file);
            }

        }

        RevCommit result = commit.call();

        // result....

    } catch (IOException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    } catch (GitAPIException e) {
        LOG.error(e);
        throw new GitManagerException(e);
    }
}

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

License:Open Source License

@Test
public void testFetch() throws Exception {
    LocalRepoBean sourceRepoBean = TestUtility.createTestRepo();
    String sourceDirectory = sourceRepoBean.getRepoDirectory().toString();

    LocalRepoBean targetRepoBean = new LocalRepoBean();
    targetRepoBean.setLocalBare(false);/*from  w w w.j  a  v a 2s .  co  m*/
    targetRepoBean.setProjectKey(sourceRepoBean.getProjectKey());
    targetRepoBean.setSlug(sourceRepoBean.getSlug() + "_clone");
    targetRepoBean.setCloneSourceURI(sourceDirectory);
    underTest.clone(targetRepoBean);
    underTest.addRemote(targetRepoBean, "source_repo", sourceDirectory);
    Map<String, String> remotes = underTest.getRemotes(targetRepoBean);

    assertTrue(remotes.containsKey("source_repo") && remotes.containsValue(sourceDirectory));
    assertFalse(underTest.fetch(targetRepoBean, "source_repo"));

    FileRepositoryBuilder builder = new FileRepositoryBuilder();
    Repository sourceRepository = builder.setGitDir(sourceRepoBean.getGitConfigDirectory().toFile())
            .findGitDir().build();

    String filename = "should_be_in_both.txt";
    Files.write(sourceRepoBean.getRepoDirectory().resolve(filename), Arrays.asList("Hello World"),
            StandardCharsets.UTF_8, StandardOpenOption.CREATE, StandardOpenOption.APPEND);
    Git git = new Git(sourceRepository);
    git.add().addFilepattern(filename).call();
    git.commit().setMessage("Added " + filename).call();

    assertTrue(underTest.fetch(targetRepoBean, "source_repo"));

    TestUtility.destroyTestRepo(sourceRepoBean);
    TestUtility.destroyTestRepo(targetRepoBean);
}

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

License:Open Source License

public static LocalRepoBean createTestRepoMultipleBranches() throws Exception {
    final String projectKey = "test_" + UUID.randomUUID().toString();
    final String repoSlug = "testRepo";
    final String remoteURL = "ssh://fake_url";
    final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug);
    Files.createDirectories(repoPath);
    final Repository repo = new FileRepository(repoPath.resolve(".git").toFile());
    repo.create();/* w ww  .ja v a2 s. c  o m*/
    final StoredConfig config = repo.getConfig();
    config.setString("remote", "origin", "url", remoteURL);
    config.save();

    final LocalRepoBean repoBean = new LocalRepoBean();
    repoBean.setProjectKey(projectKey);
    repoBean.setSlug(repoSlug);
    repoBean.setLocalBare(false);

    final Git git = new Git(repo);

    // add some files to some branches
    for (int i = 0; i < 3; i++) {
        final String filename = "newfile" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("branch1").setCreateBranch(true).call();
    for (int i = 0; i < 3; i++) {
        final String filename = "branch1" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("master").call();
    git.checkout().setName("branch2").setCreateBranch(true).call();
    for (int i = 0; i < 3; i++) {
        final String filename = "branch2" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    repo.close();
    return repoBean;
}

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

License:Open Source License

public static LocalRepoBean createTestRepo() throws Exception {
    final String projectKey = "test_" + UUID.randomUUID().toString();
    final String repoSlug = "testRepo";
    final String remoteURL = "ssh://fake_url";
    final Path repoPath = Paths.get(PropertyUtil.getGitDir(), "local_scm", projectKey, repoSlug);
    Files.createDirectories(repoPath);
    final Repository repo = new FileRepository(repoPath.resolve(".git").toFile());
    repo.create();// w  w w .j ava2  s  .c  o m
    final StoredConfig config = repo.getConfig();
    config.setString("remote", "origin", "url", remoteURL);
    config.save();

    final LocalRepoBean repoBean = new LocalRepoBean();
    repoBean.setProjectKey(projectKey);
    repoBean.setSlug(repoSlug);
    repoBean.setLocalBare(false);
    repoBean.setSourcePartner("partner");

    final Git git = new Git(repo);

    for (int i = 0; i < 3; i++) {
        final String filename = "newfile" + i + ".txt";
        Files.write(repoPath.resolve(filename), Arrays.asList("Hello World"), StandardCharsets.UTF_8,
                StandardOpenOption.CREATE, StandardOpenOption.APPEND);
        git.add().addFilepattern(filename).call();
        git.commit().setMessage("Added " + filename).call();
    }

    git.checkout().setName("master").call();

    repo.close();
    return repoBean;
}