Search in sources :

Example 1 with Status

use of org.eclipse.jgit.api.Status in project bazel by bazelbuild.

the class GitCloner method isUpToDate.

private static boolean isUpToDate(GitRepositoryDescriptor descriptor) {
    // Initializing/checking status of/etc submodules cleanly is hard, so don't try for now.
    if (descriptor.initSubmodules) {
        return false;
    }
    Repository repository = null;
    try {
        repository = new FileRepositoryBuilder().setGitDir(descriptor.directory.getChild(Constants.DOT_GIT).getPathFile()).setMustExist(true).build();
        ObjectId head = repository.resolve(Constants.HEAD);
        ObjectId checkout = repository.resolve(descriptor.checkout);
        if (head != null && checkout != null && head.equals(checkout)) {
            Status status = Git.wrap(repository).status().call();
            if (!status.hasUncommittedChanges()) {
                // new_git_repository puts (only) BUILD and WORKSPACE, and
                // git_repository doesn't add any files.
                Set<String> untracked = status.getUntracked();
                if (untracked.isEmpty() || (untracked.size() == 2 && untracked.contains("BUILD") && untracked.contains("WORKSPACE"))) {
                    return true;
                }
            }
        }
    } catch (GitAPIException | IOException e) {
    // Any exceptions here, we'll just blow it away and try cloning fresh.
    // The fresh clone avoids any weirdness due to what's there and has nicer
    // error reporting.
    } finally {
        if (repository != null) {
            repository.close();
        }
    }
    return false;
}
Also used : Status(org.eclipse.jgit.api.Status) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) Repository(org.eclipse.jgit.lib.Repository) ObjectId(org.eclipse.jgit.lib.ObjectId) IOException(java.io.IOException) FileRepositoryBuilder(org.eclipse.jgit.storage.file.FileRepositoryBuilder)

Example 2 with Status

use of org.eclipse.jgit.api.Status 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 Status

use of org.eclipse.jgit.api.Status 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 Status

use of org.eclipse.jgit.api.Status 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 Status

use of org.eclipse.jgit.api.Status 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

Status (org.eclipse.jgit.api.Status)64 Git (org.eclipse.jgit.api.Git)46 Test (org.junit.Test)44 File (java.io.File)35 Ref (org.eclipse.jgit.lib.Ref)17 Repository (org.eclipse.jgit.lib.Repository)13 IOException (java.io.IOException)10 ArrayList (java.util.ArrayList)10 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)9 RevCommit (org.eclipse.jgit.revwalk.RevCommit)9 StoredConfig (org.eclipse.jgit.lib.StoredConfig)8 Exchange (org.apache.camel.Exchange)7 StatusCommand (org.eclipse.jgit.api.StatusCommand)7 ObjectId (org.eclipse.jgit.lib.ObjectId)7 IFile (org.eclipse.core.resources.IFile)6 IStatus (org.eclipse.core.runtime.IStatus)6 NullProgressMonitor (org.eclipse.core.runtime.NullProgressMonitor)6 Processor (org.apache.camel.Processor)4 ZipFile (org.apache.commons.compress.archivers.zip.ZipFile)4 CheckoutCommand (org.eclipse.jgit.api.CheckoutCommand)4