You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ac...@apache.org on 2019/01/08 08:07:11 UTC

[camel] 01/02: CAMEL-13034 - Implement 'checkout' operation for Git component

This is an automated email from the ASF dual-hosted git repository.

acosentino pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit bc0a230ffc5c2031a3cb1d7a62855f29c6c884ba
Author: Andrea Cosentino <an...@gmail.com>
AuthorDate: Tue Jan 8 09:03:45 2019 +0100

    CAMEL-13034 - Implement 'checkout' operation for Git component
---
 .../camel/component/git/producer/GitOperation.java |  1 +
 .../camel/component/git/producer/GitProducer.java  | 20 ++++++
 .../component/git/producer/GitProducerTest.java    | 72 ++++++++++++++++++++++
 3 files changed, 93 insertions(+)

diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
index d5f63fa..fb03361 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
@@ -19,6 +19,7 @@ package org.apache.camel.component.git.producer;
 public interface GitOperation {
 
     String CLONE_OPERATION = "clone";
+    String CHECKOUT_OPERATION = "checkout";
     String INIT_OPERATION = "init";
     String ADD_OPERATION = "add";
     String REMOVE_OPERATION = "remove";
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
index c73b7a7..6046296 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
@@ -93,6 +93,10 @@ public class GitProducer extends DefaultProducer {
         case GitOperation.CLONE_OPERATION:
             doClone(exchange, operation);
             break;
+            
+        case GitOperation.CHECKOUT_OPERATION:
+            doCheckout(exchange, operation);
+            break;
 
         case GitOperation.INIT_OPERATION:
             doInit(exchange, operation);
@@ -213,6 +217,22 @@ public class GitProducer extends DefaultProducer {
             }
         }
     }
+    
+    protected void doCheckout(Exchange exchange, String operation) throws Exception {
+        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
+            throw new IllegalArgumentException("Branch Name must be specified to execute " + operation);
+        }
+        try {
+        	if (ObjectHelper.isEmpty(endpoint.getTagName())) {
+                git.checkout().setCreateBranch(true).setName(endpoint.getBranchName()).call();
+        	} else {
+        		git.checkout().setCreateBranch(true).setName(endpoint.getBranchName()).setStartPoint(endpoint.getTagName()).call();
+        	}
+        } catch (Exception e) {
+            log.error("There was an error in Git {} operation", operation);
+            throw e;
+        }
+    }
 
     protected void doInit(Exchange exchange, String operation) throws Exception {
         Git result = null;
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java
index 3a09f42..a3303ac 100644
--- a/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java
+++ b/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java
@@ -58,6 +58,76 @@ public class GitProducerTest extends GitTestSupport {
         File gitDir = new File(gitLocalRepo, ".git");
         assertEquals(gitDir.exists(), true);
     }
+    
+    @Test
+    public void checkoutTest() 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 checkout
+        template.sendBody("direct:checkout", "");
+
+        // Check
+        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);
+        git.close();
+    }
+    
+    
+    @Test
+    public void checkoutSpecificTagTest() throws Exception {
+        // Init
+        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();
+        
+        // Test camel-git create tag
+        template.sendBody("direct:create-tag", "");
+
+        // Check
+        List<Ref> ref = git.tagList().call();
+        boolean tagCreated = false;
+        for (Ref refInternal : ref) {
+            if (refInternal.getName().equals("refs/tags/" + tagTest)) {
+                tagCreated = true;
+            }
+        }
+        assertEquals(tagCreated, true);
+
+        // Test camel-git create-branch
+        template.sendBody("direct:checkout-specific-tag", "");
+
+        // Check
+        ref = git.branchList().call();
+        boolean branchCreated = false;
+        for (Ref refInternal : ref) {
+            if (refInternal.getName().equals("refs/heads/" + branchTest)) {
+                branchCreated = true;
+            }
+        }
+        assertEquals(branchCreated, true);
+        git.close();
+    }
 
     @Test(expected = CamelExecutionException.class)
     public void doubleCloneOperationTest() throws Exception {
@@ -814,6 +884,8 @@ public class GitProducerTest extends GitTestSupport {
                 from("direct:clone").to("git://" + gitLocalRepo + "?remotePath=https://github.com/oscerd/json-webserver-example.git&operation=clone");
                 from("direct:init").to("git://" + gitLocalRepo + "?operation=init");
                 from("direct:add").to("git://" + gitLocalRepo + "?operation=add");
+                from("direct:checkout").to("git://" + gitLocalRepo + "?operation=checkout&branchName=" + branchTest);
+                from("direct:checkout-specific-tag").to("git://" + gitLocalRepo + "?operation=checkout&branchName=" + branchTest + "&tagName=" + tagTest);
                 from("direct:remove").to("git://" + gitLocalRepo + "?operation=remove");
                 from("direct:add-on-branch").to("git://" + gitLocalRepo + "?operation=add&branchName=" + branchTest);
                 from("direct:remove-on-branch").to("git://" + gitLocalRepo + "?operation=add&branchName=" + branchTest);