camel-commits mailing list archives

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


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

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

----------------------------------------------------------------------
 .../camel/component/git/GitComponent.java       |   2 +-
 .../apache/camel/component/git/GitEndpoint.java |   7 +-
 .../camel/component/git/GitOperation.java       |   1 +
 .../apache/camel/component/git/GitProducer.java |  29 ++++-
 .../github/producer/GitProducerTest.java        | 116 ++++++++++++++++++-
 5 files changed, 142 insertions(+), 13 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/d2467763/components/camel-git/src/main/java/org/apache/camel/component/git/GitComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitComponent.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitComponent.java
index 349e020..81a759c 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitComponent.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitComponent.java
@@ -29,7 +29,7 @@ public class GitComponent extends DefaultComponent {
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         GitEndpoint endpoint = new GitEndpoint(uri, this);
         setProperties(endpoint, parameters);
-        endpoint.setRemotePath(remaining);
+        endpoint.setLocalPath(remaining);
         return endpoint;
     }
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d2467763/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
----------------------------------------------------------------------
diff --git a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
index 3dd6cbb..612ee00 100644
--- a/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
+++ b/components/camel-git/src/main/java/org/apache/camel/component/git/GitEndpoint.java
@@ -16,8 +16,6 @@
  */
 package org.apache.camel.component.git;
 
-import java.io.File;
-
 import org.apache.camel.Consumer;
 import org.apache.camel.Processor;
 import org.apache.camel.Producer;
@@ -26,11 +24,8 @@ import org.apache.camel.spi.Metadata;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
 import org.apache.camel.spi.UriPath;
-import org.eclipse.jgit.api.Git;
-import org.eclipse.jgit.internal.storage.file.FileRepository;
-import org.eclipse.jgit.lib.Repository;
 
-@UriEndpoint(scheme = "git", title = "Git", syntax = "git:remotepath/branchname", label =
"api,file")
+@UriEndpoint(scheme = "git", title = "Git", syntax = "git:localpath", label = "api,file")
 public class GitEndpoint extends DefaultEndpoint {
 
     @UriPath @Metadata(required = "true")

http://git-wip-us.apache.org/repos/asf/camel/blob/d2467763/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 49109c6..4f588dd 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
@@ -22,4 +22,5 @@ public interface GitOperation {
     public final static String INIT_OPERATION = "init";
     public final static String ADD_OPERATION = "add";
     public final static String COMMIT_OPERATION = "commit";
+    public final static String COMMIT_ALL_OPERATION = "commitAll";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/d2467763/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 5ee7ee3..81e0d37 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
@@ -51,7 +51,11 @@ public class GitProducer extends DefaultProducer{
 	    	
 	    case GitOperation.COMMIT_OPERATION:
 	    	doCommit(exchange, operation, repo);
-	    	break;	
+	    	break;
+	    
+            case GitOperation.COMMIT_ALL_OPERATION:
+                doCommitAll(exchange, operation, repo);
+                break;
 	    }
 	    repo.close();
 	}
@@ -101,6 +105,9 @@ public class GitProducer extends DefaultProducer{
     	}
     	try {
     		git = new Git(repo);
+                if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                    git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+                }
 			git.add().addFilepattern(fileName).call();
 		} catch (Exception e) {
 			LOG.error("There was an error in Git " + operation + " operation");
@@ -128,6 +135,26 @@ public class GitProducer extends DefaultProducer{
 		}
     }
     
+    protected void doCommitAll(Exchange exchange, String operation, Repository repo) {
+        Git git = null;
+        String commitMessage = null;
+        if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE)))
{
+                commitMessage = exchange.getIn().getHeader(GitConstants.GIT_COMMIT_MESSAGE,
String.class);
+        } else {
+                throw new IllegalArgumentException("Commit message must be specified to execute
" + operation);
+        }
+        try {
+            git = new Git(repo);
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+                git.commit().setAll(true).setMessage(commitMessage).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/d2467763/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 f911648..1965f61 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
@@ -37,7 +37,9 @@ public class GitProducerTest extends CamelTestSupport {
 
 	private final static String GIT_LOCAL_REPO = "testRepo";
 	private final static String FILENAME_TO_ADD = "filetest.txt";
+	private final static String FILENAME_BRANCH_TO_ADD = "filetest1.txt";
 	private final static String COMMIT_MESSAGE = "Test commit";
+        private final static String COMMIT_MESSAGE_ALL = "Test commit all";
 	private final static String COMMIT_MESSAGE_BRANCH = "Test commit on a branch";
 	private final static String BRANCH_TEST = "testBranch";
 	
@@ -186,21 +188,125 @@ public class GitProducerTest extends CamelTestSupport {
         repository.close();
     }
     
+    @Test
+    public void commitAllTest() 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);
+            }
+        });
+        
+        template.send("direct:commit-all", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, COMMIT_MESSAGE_ALL);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE_ALL);
+            count++;
+        }
+        assertEquals(count, 1);
+        repository.close();
+    }
+    
+    @Test
+    public void commitAllDifferentBranchTest() 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);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 1);
+        
+        Git git = new Git(repository);
+        git.checkout().setCreateBranch(true).setName(BRANCH_TEST).
+        setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM).call();
+        
+        File fileToAdd1 = new File(GIT_LOCAL_REPO, FILENAME_BRANCH_TO_ADD);
+        fileToAdd1.createNewFile();
+        
+        template.send("direct:add-on-branch", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_BRANCH_TO_ADD);
+            }
+        });
+        
+        template.send("direct:commit-all-branch", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, COMMIT_MESSAGE_ALL);
+            }
+        });
+        logs = git.log().call();
+        count = 0;
+        for (RevCommit rev : logs) {
+            if (count == 0) assertEquals(rev.getShortMessage(), COMMIT_MESSAGE_ALL);
+            if (count == 1) assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        assertEquals(count, 2);
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
             @Override
             public void configure() throws Exception {
                 from("direct:clone")
-                        .to("git://https://github.com/oscerd/json-webserver-example.git?localPath="
+ GIT_LOCAL_REPO + "&operation=clone");
+                        .to("git://" + GIT_LOCAL_REPO + "?remotePath=https://github.com/oscerd/json-webserver-example.git&operation=clone");
                 from("direct:init")
-                        .to("git://https://github.com/oscerd/json-webserver-example.git?localPath="
+ GIT_LOCAL_REPO + "&operation=init");
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=init");
                 from("direct:add")
-                        .to("git://https://github.com/oscerd/json-webserver-example.git?localPath="
+ GIT_LOCAL_REPO + "&operation=add");
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=add");
+                from("direct:add-on-branch")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=add&branchName="
+ BRANCH_TEST);
                 from("direct:commit")
-                        .to("git://https://github.com/oscerd/json-webserver-example.git?localPath="
+ GIT_LOCAL_REPO + "&operation=commit");
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
                 from("direct:commit-branch")
-                        .to("git://https://github.com/oscerd/json-webserver-example.git?localPath="
+ GIT_LOCAL_REPO + "&operation=commit&branchName=" + BRANCH_TEST);
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit&branchName="
+ BRANCH_TEST);
+                from("direct:commit-all")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit");
+                from("direct:commit-all-branch")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=commit&branchName="
+ BRANCH_TEST);
             } 
         };
     }


Mime
View raw message