camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [06/23] camel git commit: CAMEL-7982: camel-git - A generic git component, add createBranch operation
Date Sat, 18 Jul 2015 09:09:23 GMT
CAMEL-7982: camel-git - A generic git component, add createBranch operation


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/a6057b59
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/a6057b59
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/a6057b59

Branch: refs/heads/master
Commit: a6057b59dd149ea3bc73c16ec9f34b1f14144a78
Parents: d246776
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sat Jul 18 11:06:09 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sat Jul 18 11:08:14 2015 +0200

----------------------------------------------------------------------
 .../camel/component/git/GitOperation.java       |  1 +
 .../apache/camel/component/git/GitProducer.java | 18 +++++++
 .../github/producer/GitProducerTest.java        | 50 +++++++++++++++++++-
 3 files changed, 67 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/a6057b59/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
index 4f588dd..c12c33d 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitOperation.java
@@ -23,4 +23,5 @@ public interface GitOperation {
     public final static String ADD_OPERATION = "add";
     public final static String COMMIT_OPERATION = "commit";
     public final static String COMMIT_ALL_OPERATION = "commitAll";
+    public final static String CREATE_BRANCH_OPERATION = "createBranch";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/a6057b59/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
index 81e0d37..1bd407a 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitProducer.java
@@ -56,6 +56,10 @@ public class GitProducer extends DefaultProducer{
             case GitOperation.COMMIT_ALL_OPERATION:
                 doCommitAll(exchange, operation, repo);
                 break;
+                
+            case GitOperation.CREATE_BRANCH_OPERATION:
+                doCreateBranch(exchange, operation, repo);
+                break;
 	    }
 	    repo.close();
 	}
@@ -155,6 +159,20 @@ public class GitProducer extends DefaultProducer{
                 }
     }
     
+    protected void doCreateBranch(Exchange exchange, String operation, Repository repo) {
+        Git git = null;
+        if (ObjectHelper.isEmpty(endpoint.getBranchName())) {
+            throw new IllegalArgumentException("Branch Name must be specified to execute
" + operation);
+        } 
+        try {
+            git = new Git(repo);
+            git.branchCreate().setName(endpoint.getBranchName()).call();
+        } catch (Exception e) {
+            LOG.error("There was an error in Git " + operation + " operation");
+            e.printStackTrace();
+        }
+    }
+    
     private Repository getLocalRepository(){
         FileRepositoryBuilder builder = new FileRepositoryBuilder();
         Repository repo = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/a6057b59/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
index 1965f61..c27139c 100755
--- a/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
+++ b/components/camel-git/src/test/java/org/apache/camel/component/github/producer/GitProducerTest.java
@@ -18,6 +18,7 @@ package org.apache.camel.component.github.producer;
 
 import java.io.File;
 import java.io.IOException;
+import java.util.List;
 
 import org.apache.camel.Exchange;
 import org.apache.camel.Processor;
@@ -28,6 +29,7 @@ import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.Status;
 import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
@@ -55,8 +57,8 @@ public class GitProducerTest extends CamelTestSupport {
     @Override
     public void tearDown() throws Exception {
     	super.tearDown();
-        File path = new File(GIT_LOCAL_REPO);
-        deleteDirectory(path);
+//        File path = new File(GIT_LOCAL_REPO);
+//        deleteDirectory(path);
     }
     
     @Test
@@ -286,6 +288,48 @@ public class GitProducerTest extends CamelTestSupport {
         repository.close();
     }
     
+    @Test
+    public void createBranchTest() throws Exception {
+
+        Repository repository = getTestRepository();
+        
+        File fileToAdd = new File(GIT_LOCAL_REPO, FILENAME_TO_ADD);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_TO_ADD);
+            }
+        });
+        File gitDir = new File(GIT_LOCAL_REPO, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(FILENAME_TO_ADD));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, COMMIT_MESSAGE);
+            }
+        });
+        
+        Git git = new Git(repository);
+        
+        template.sendBody("direct:create-branch", "");
+        
+        List<Ref> ref = git.branchList().call();
+        boolean branchCreated = false;
+        for (Ref refInternal : ref) {
+            if (refInternal.getName().equals("refs/heads/" + BRANCH_TEST)) {
+                branchCreated = true;
+            }
+        }
+        assertEquals(branchCreated, true);
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
@@ -307,6 +351,8 @@ public class GitProducerTest extends CamelTestSupport {
                         .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
                 from("direct:commit-all-branch")
                         .to("git://" + GIT_LOCAL_REPO + "?operation=commit&branchName="
+ BRANCH_TEST);
+                from("direct:create-branch")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=createBranch&branchName="
+ BRANCH_TEST);
             } 
         };
     }


Mime
View raw message