camel-commits mailing list archives

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


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

Branch: refs/heads/master
Commit: e224e8eb5e118c041f6e7f35835e60f71b27198f
Parents: 903e88f
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sat Jul 18 11:06:32 2015 +0200
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sat Jul 18 11:08:15 2015 +0200

----------------------------------------------------------------------
 .../camel/component/git/GitOperation.java       |   1 +
 .../apache/camel/component/git/GitProducer.java |  21 ++++
 .../github/producer/GitProducerTest.java        | 111 +++++++++++++++++++
 3 files changed, 133 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/e224e8eb/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 88e03de..df7bb0f 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
@@ -27,4 +27,5 @@ public interface GitOperation {
     public final static String CREATE_BRANCH_OPERATION = "createBranch";
     public final static String DELETE_BRANCH_OPERATION = "deleteBranch";
     public final static String STATUS_OPERATION = "status";
+    public final static String LOG_OPERATION = "log";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/e224e8eb/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 e7c2fbe..e931d54 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
@@ -9,6 +9,7 @@ import org.apache.camel.util.ObjectHelper;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.Status;
 import org.eclipse.jgit.lib.Repository;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -73,6 +74,10 @@ public class GitProducer extends DefaultProducer{
             case GitOperation.STATUS_OPERATION:
                 doStatus(exchange, operation, repo);
                 break;
+                
+            case GitOperation.LOG_OPERATION:
+                doLog(exchange, operation, repo);
+                break;
 	    }
 	    repo.close();
 	}
@@ -236,6 +241,22 @@ public class GitProducer extends DefaultProducer{
         exchange.getOut().setBody(status);
     }
     
+    protected void doLog(Exchange exchange, String operation, Repository repo) {
+        Git git = null;
+        Iterable<RevCommit> revCommit = null;
+        try {
+            git = new Git(repo);
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+                revCommit = git.log().call();
+                } catch (Exception e) {
+                        LOG.error("There was an error in Git " + operation + " operation");
+                        e.printStackTrace();
+                }
+        exchange.getOut().setBody(revCommit);
+    }
+    
     private Repository getLocalRepository(){
         FileRepositoryBuilder builder = new FileRepositoryBuilder();
         Repository repo = null;

http://git-wip-us.apache.org/repos/asf/camel/blob/e224e8eb/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 73693cc..8ad0cb6 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
@@ -595,6 +595,113 @@ public class GitProducerTest extends CamelTestSupport {
         repository.close();
     }
     
+    @Test
+    public void logTest() 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 = template.requestBody("direct:status", "", Status.class);
+        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> revCommits = template.requestBody("direct:log", "", Iterable.class);
+        for (RevCommit rev : revCommits) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+        }        
+        repository.close();
+    }
+    
+    @Test
+    public void logBranchTest() 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 = template.requestBody("direct:status", "", Status.class);
+        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> revCommits = template.requestBody("direct:log", "", Iterable.class);
+        for (RevCommit rev : revCommits) {
+            assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+        }
+        
+        template.sendBody("direct:create-branch", "");
+        
+        Git git = new Git(repository);
+        
+        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);
+        
+        File fileToAddDifferent = new File(GIT_LOCAL_REPO, FILENAME_BRANCH_TO_ADD);
+        fileToAddDifferent.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);
+            }
+        });
+        
+        revCommits = template.requestBody("direct:log-branch", "", Iterable.class);
+        int count = 0;
+        for (RevCommit rev : revCommits) {
+            if (count == 0) assertEquals(rev.getShortMessage(), COMMIT_MESSAGE_ALL);
+            if (count == 1) assertEquals(rev.getShortMessage(), COMMIT_MESSAGE);
+            count++;
+        }
+        
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
@@ -628,6 +735,10 @@ public class GitProducerTest extends CamelTestSupport {
                         .to("git://" + GIT_LOCAL_REPO + "?operation=status");
                 from("direct:status-branch")
                         .to("git://" + GIT_LOCAL_REPO + "?operation=status&branchName="
+ BRANCH_TEST);
+                from("direct:log")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=log");
+                from("direct:log-branch")
+                        .to("git://" + GIT_LOCAL_REPO + "?operation=log&branchName="
+ BRANCH_TEST);
             } 
         };
     }


Mime
View raw message