Search in sources :

Example 1 with Git

use of org.eclipse.jgit.api.Git in project camel by apache.

the class AbstractGitConsumer method doStart.

@Override
protected void doStart() throws Exception {
    super.doStart();
    this.repo = getLocalRepository();
    this.git = new Git(repo);
}
Also used : Git(org.eclipse.jgit.api.Git)

Example 2 with Git

use of org.eclipse.jgit.api.Git in project camel by apache.

the class GitConsumerTest method commitConsumerTest.

@Test
public void commitConsumerTest() throws Exception {
    // Init
    MockEndpoint mockResultCommit = getMockEndpoint("mock:result-commit");
    mockResultCommit.expectedMessageCount(2);
    Git git = getGitTestRepository();
    File gitDir = new File(gitLocalRepo, ".git");
    assertEquals(gitDir.exists(), true);
    File fileToAdd = new File(gitLocalRepo, filenameToAdd);
    fileToAdd.createNewFile();
    git.add().addFilepattern(filenameToAdd).call();
    Status status = git.status().call();
    assertTrue(status.getAdded().contains(filenameToAdd));
    git.commit().setMessage(commitMessage).call();
    File fileToAdd1 = new File(gitLocalRepo, filenameBranchToAdd);
    fileToAdd1.createNewFile();
    git.add().addFilepattern(filenameBranchToAdd).call();
    status = git.status().call();
    assertTrue(status.getAdded().contains(filenameBranchToAdd));
    git.commit().setMessage("Test test Commit").call();
    Iterable<RevCommit> logs = git.log().call();
    validateGitLogs(git, "Test test Commit", commitMessage);
    // Test
    mockResultCommit.assertIsSatisfied();
    // Check
    Exchange ex1 = mockResultCommit.getExchanges().get(0);
    Exchange ex2 = mockResultCommit.getExchanges().get(1);
    assertEquals(commitMessage, ex2.getOut().getBody(RevCommit.class).getShortMessage());
    assertEquals("Test test Commit", ex1.getOut().getBody(RevCommit.class).getShortMessage());
    git.close();
}
Also used : Status(org.eclipse.jgit.api.Status) Exchange(org.apache.camel.Exchange) Git(org.eclipse.jgit.api.Git) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) RevCommit(org.eclipse.jgit.revwalk.RevCommit) Test(org.junit.Test)

Example 3 with Git

use of org.eclipse.jgit.api.Git in project camel by apache.

the class GitConsumerTest method branchConsumerTest.

@Test
public void branchConsumerTest() throws Exception {
    // Init
    MockEndpoint mockResultBranch = getMockEndpoint("mock:result-branch");
    mockResultBranch.expectedMessageCount(2);
    Git git = getGitTestRepository();
    File fileToAdd = new File(gitLocalRepo, filenameToAdd);
    fileToAdd.createNewFile();
    git.add().addFilepattern(filenameToAdd).call();
    File gitDir = new File(gitLocalRepo, ".git");
    assertEquals(gitDir.exists(), true);
    Status status = git.status().call();
    assertTrue(status.getAdded().contains(filenameToAdd));
    git.commit().setMessage(commitMessage).call();
    git.branchCreate().setName(branchTest).call();
    List<Ref> ref = git.branchList().call();
    boolean branchCreated = false;
    for (Ref refInternal : ref) {
        if (refInternal.getName().equals("refs/heads/" + branchTest)) {
            branchCreated = true;
        }
    }
    assertEquals(branchCreated, true);
    // Test
    mockResultBranch.assertIsSatisfied();
    // Check
    List<Exchange> exchanges = mockResultBranch.getExchanges();
    assertEquals("refs/heads/master", exchanges.get(0).getOut().getBody(ObjectIdRef.Unpeeled.class).getName());
    assertEquals("refs/heads/" + branchTest, exchanges.get(1).getOut().getBody(ObjectIdRef.Unpeeled.class).getName());
    git.close();
}
Also used : Status(org.eclipse.jgit.api.Status) Exchange(org.apache.camel.Exchange) ObjectIdRef(org.eclipse.jgit.lib.ObjectIdRef) Ref(org.eclipse.jgit.lib.Ref) ObjectIdRef(org.eclipse.jgit.lib.ObjectIdRef) Git(org.eclipse.jgit.api.Git) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) File(java.io.File) Test(org.junit.Test)

Example 4 with Git

use of org.eclipse.jgit.api.Git in project camel by apache.

the class GitProducerTest method commitAllDifferentBranchTest.

@Test
public void commitAllDifferentBranchTest() throws Exception {
    // Init
    Git git = getGitTestRepository();
    File gitDir = new File(gitLocalRepo, ".git");
    assertEquals(gitDir.exists(), true);
    File fileToAdd = new File(gitLocalRepo, filenameToAdd);
    fileToAdd.createNewFile();
    git.add().addFilepattern(filenameToAdd).call();
    Status status = git.status().call();
    assertTrue(status.getAdded().contains(filenameToAdd));
    git.commit().setMessage(commitMessage).call();
    validateGitLogs(git, commitMessage);
    git.checkout().setCreateBranch(true).setName(branchTest).setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM).call();
    File fileToAdd1 = new File(gitLocalRepo, filenameBranchToAdd);
    fileToAdd1.createNewFile();
    // Test camel-git add and commit (different branches)
    template.send("direct:add-on-branch", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, filenameBranchToAdd);
        }
    });
    template.send("direct:commit-all-branch", new Processor() {

        @Override
        public void process(Exchange exchange) throws Exception {
            exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, commitMessageAll);
        }
    });
    // Check
    validateGitLogs(git, commitMessageAll, commitMessage);
    git.close();
}
Also used : Status(org.eclipse.jgit.api.Status) Exchange(org.apache.camel.Exchange) Git(org.eclipse.jgit.api.Git) Processor(org.apache.camel.Processor) File(java.io.File) CamelExecutionException(org.apache.camel.CamelExecutionException) Test(org.junit.Test)

Example 5 with Git

use of org.eclipse.jgit.api.Git in project camel by apache.

the class GitProducerTest method commitTestAllowEmptyFalse.

@Test(expected = CamelExecutionException.class)
public void commitTestAllowEmptyFalse() throws Exception {
    // Init
    Git git = getGitTestRepository();
    File gitDir = new File(gitLocalRepo, ".git");
    assertEquals(gitDir.exists(), true);
    File fileToAdd = new File(gitLocalRepo, filenameToAdd);
    fileToAdd.createNewFile();
    git.add().addFilepattern(filenameToAdd).call();
    Status status = git.status().call();
    assertTrue(status.getAdded().contains(filenameToAdd));
    git.commit().setMessage(commitMessage).call();
    // Test camel-git commit (with allowEmpty set to false)
    Map<String, Object> headers = new HashMap<>();
    headers.put(GitConstants.GIT_COMMIT_MESSAGE, commitMessage);
    template.requestBodyAndHeaders("direct:commit-not-allow-empty", "", headers);
// Check : An exception should have been raised
}
Also used : Status(org.eclipse.jgit.api.Status) Git(org.eclipse.jgit.api.Git) HashMap(java.util.HashMap) File(java.io.File) Test(org.junit.Test)

Aggregations

Git (org.eclipse.jgit.api.Git)77 File (java.io.File)59 Test (org.junit.Test)41 Status (org.eclipse.jgit.api.Status)23 IOException (java.io.IOException)20 CloneCommand (org.eclipse.jgit.api.CloneCommand)19 UsernamePasswordCredentialsProvider (org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider)18 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)17 Ref (org.eclipse.jgit.lib.Ref)17 Date (java.util.Date)14 PushResult (org.eclipse.jgit.transport.PushResult)14 RepositoryModel (com.gitblit.models.RepositoryModel)13 Repository (org.eclipse.jgit.lib.Repository)13 RemoteRefUpdate (org.eclipse.jgit.transport.RemoteRefUpdate)13 FileOutputStream (java.io.FileOutputStream)12 RevCommit (org.eclipse.jgit.revwalk.RevCommit)12 BufferedWriter (java.io.BufferedWriter)11 OutputStreamWriter (java.io.OutputStreamWriter)11 RefSpec (org.eclipse.jgit.transport.RefSpec)9 Exchange (org.apache.camel.Exchange)8