Repository: camel
Updated Branches:
  refs/heads/master ebf05e07c -> 46d61b8c5


CAMEL-7982: camel-git - A generic git component, add operation


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

Branch: refs/heads/master
Commit: 13b7373f42fc5c0feea70ee5422b72d44e9e55cf
Parents: 91b8b57
Author: Andrea Cosentino <[email protected]>
Authored: Sat Jul 18 11:05:54 2015 +0200
Committer: Andrea Cosentino <[email protected]>
Committed: Sat Jul 18 11:08:13 2015 +0200

----------------------------------------------------------------------
 .../camel/component/git/GitConstants.java       |  1 +
 .../camel/component/git/GitOperation.java       |  1 +
 .../apache/camel/component/git/GitProducer.java | 27 +++++++++++-
 .../github/producer/GitProducerTest.java        | 46 ++++++++++++++++++++
 4 files changed, 73 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/13b7373f/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
----------------------------------------------------------------------
diff --git 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
index 88331c7..c96959b 100644
--- 
a/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
+++ 
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitConstants.java
@@ -18,4 +18,5 @@ package org.apache.camel.component.git;
 
 public interface GitConstants {
        public static final String GIT_OPERATION = "CamelGitOperation";
+       public static final String GIT_FILE_NAME = "CamelGitFilename";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/13b7373f/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 2b951c0..62b19a4 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
@@ -20,4 +20,5 @@ public interface GitOperation {
 
     public final static String CLONE_OPERATION = "clone";
     public final static String INIT_OPERATION = "init";
+    public final static String ADD_OPERATION = "add";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/13b7373f/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 e0fa4fe..1ccbac7 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
@@ -6,6 +6,7 @@ import org.apache.camel.Exchange;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
 import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.lib.Repository;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -36,13 +37,17 @@ public class GitProducer extends DefaultProducer{
            case GitOperation.INIT_OPERATION:
                doInit(exchange, operation);
                break;
+
+           case GitOperation.ADD_OPERATION:
+               doAdd(exchange, operation);
+               break;          
            }
        }
        
     protected void doClone(Exchange exchange, String operation) {
        Git result = null;
        if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute" + operation);
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
        }
        try {
                File localRepo = new File(endpoint.getLocalPath(), "");
@@ -62,7 +67,7 @@ public class GitProducer extends DefaultProducer{
     protected void doInit(Exchange exchange, String operation) {
        Git result = null;
        if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
-               throw new IllegalArgumentException("Local path must specified 
to execute" + operation);
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
        }
        try {
                        result = Git.init().setDirectory(new 
File(endpoint.getLocalPath(),"")).call();
@@ -73,4 +78,22 @@ public class GitProducer extends DefaultProducer{
                        result.close();
                }
     }
+    
+    protected void doAdd(Exchange exchange, String operation) {
+       String fileName = null;
+       if (ObjectHelper.isEmpty(endpoint.getLocalPath())) {
+               throw new IllegalArgumentException("Local path must specified 
to execute " + operation);
+       }
+       if 
(ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME)))
 {
+               fileName = 
exchange.getIn().getHeader(GitConstants.GIT_FILE_NAME, String.class);
+       } else {
+               throw new IllegalArgumentException("File name must be specified 
to execute " + operation);
+       }
+       try {
+                       Git.open(new 
File(endpoint.getLocalPath())).add().addFilepattern(fileName).call();
+               } catch (Exception e) {
+                       LOG.error("There was an error in Git " + operation + " 
operation");
+                       e.printStackTrace();
+               }
+    }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/13b7373f/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 22e66b6..18965c8 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
@@ -17,14 +17,24 @@
 package org.apache.camel.component.github.producer;
 
 import java.io.File;
+import java.io.IOException;
 
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
 import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.git.GitConstants;
 import org.apache.camel.test.junit4.CamelTestSupport;
+import org.eclipse.jgit.api.Git;
+import org.eclipse.jgit.api.Status;
+import org.eclipse.jgit.api.errors.GitAPIException;
+import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.junit.Test;
 
 public class GitProducerTest extends CamelTestSupport{
 
        private final static String GIT_LOCAL_REPO = "pippo";
+       private final static String FILENAME_TO_ADD = "filetest.txt";
        
     @Override
     public void setUp() throws Exception {
@@ -56,6 +66,28 @@ public class GitProducerTest extends CamelTestSupport{
         assertEquals(gitDir.exists(), true);
     }
     
+    @Test
+    public void addTest() 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));
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
@@ -65,10 +97,24 @@ public class GitProducerTest extends CamelTestSupport{
                         
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=clone");
                 from("direct:init")
                         
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=init");
+                from("direct:add")
+                        
.to("git://https://github.com/oscerd/json-webserver-example.git?localPath="; + 
GIT_LOCAL_REPO + "&operation=add");
             } 
         };
     }
     
+    private Repository getTestRepository() throws IOException, 
IllegalStateException, GitAPIException {
+        File gitRepo = new File(GIT_LOCAL_REPO, ".git");
+        Git.init().setDirectory(new File(GIT_LOCAL_REPO,"")).call();
+        // now open the resulting repository with a FileRepositoryBuilder
+        FileRepositoryBuilder builder = new FileRepositoryBuilder();
+        Repository repo = builder.setGitDir(gitRepo)
+                .readEnvironment() // scan environment GIT_* variables
+                .findGitDir() // scan up the file system tree
+                .build();
+        return repo;
+    }
+    
     static public boolean deleteDirectory(File path) {
         if( path.exists() ) {
           File[] files = path.listFiles();

Reply via email to