camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From acosent...@apache.org
Subject [1/2] camel git commit: CAMEL-9453: Camel-Git: Add support for cherry-pick operation
Date Sun, 27 Dec 2015 08:52:43 GMT
Repository: camel
Updated Branches:
  refs/heads/master d72bfe1ea -> f76dc69c2


CAMEL-9453: Camel-Git: Add support for cherry-pick operation


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

Branch: refs/heads/master
Commit: b8dbbf400c1ebf3ad5fe7bec73e4061e769e67ad
Parents: d72bfe1
Author: Andrea Cosentino <ancosen@gmail.com>
Authored: Sun Dec 27 09:50:18 2015 +0100
Committer: Andrea Cosentino <ancosen@gmail.com>
Committed: Sun Dec 27 09:50:18 2015 +0100

----------------------------------------------------------------------
 .../camel/component/git/GitConstants.java       |   2 +
 .../component/git/producer/GitOperation.java    |   1 +
 .../component/git/producer/GitProducer.java     |  32 +++
 .../component/git/producer/GitProducerTest.java | 213 +++++++++++++++++++
 4 files changed, 248 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/b8dbbf40/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 0d4f518..5e575ce 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
@@ -26,4 +26,6 @@ public interface GitConstants {
     String GIT_COMMIT_USERNAME = "CamelGitCommitUsername";
     
     String GIT_COMMIT_EMAIL = "CamelGitCommitEmail";
+    
+    String GIT_COMMIT_ID = "CamelGitCommitId";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b8dbbf40/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitOperation.java
----------------------------------------------------------------------
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 884b154..ce6ae25 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
@@ -33,4 +33,5 @@ public interface GitOperation {
     String PUSH_OPERATION = "push";
     String PULL_OPERATION = "pull";
     String SHOW_BRANCHES = "showBranches";
+    String CHERRYPICK_OPERATION = "cherryPick";
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/b8dbbf40/components/camel-git/src/main/java/org/apache/camel/component/git/producer/GitProducer.java
----------------------------------------------------------------------
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 0f10f29..9f38d17 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
@@ -25,14 +25,18 @@ import org.apache.camel.component.git.GitConstants;
 import org.apache.camel.component.git.GitEndpoint;
 import org.apache.camel.impl.DefaultProducer;
 import org.apache.camel.util.ObjectHelper;
+import org.eclipse.jgit.api.CherryPickCommand;
+import org.eclipse.jgit.api.CherryPickResult;
 import org.eclipse.jgit.api.Git;
 import org.eclipse.jgit.api.ListBranchCommand.ListMode;
 import org.eclipse.jgit.api.PullResult;
 import org.eclipse.jgit.api.Status;
+import org.eclipse.jgit.lib.ObjectId;
 import org.eclipse.jgit.lib.PersonIdent;
 import org.eclipse.jgit.lib.Ref;
 import org.eclipse.jgit.lib.Repository;
 import org.eclipse.jgit.revwalk.RevCommit;
+import org.eclipse.jgit.revwalk.RevWalk;
 import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
 import org.eclipse.jgit.transport.PushResult;
 import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
@@ -94,6 +98,10 @@ public class GitProducer extends DefaultProducer {
             doAdd(exchange, operation);
             break;
 
+        case GitOperation.CHERRYPICK_OPERATION:
+            doCherryPick(exchange, operation);
+            break;
+            
         case GitOperation.REMOVE_OPERATION:
             doRemove(exchange, operation);
             break;
@@ -411,6 +419,30 @@ public class GitProducer extends DefaultProducer {
         }
         exchange.getOut().setBody(result);
     }
+    
+    protected void doCherryPick(Exchange exchange, String operation) throws Exception {
+        CherryPickResult result = null;
+        String commitId = null;
+        try {
+        	if (ObjectHelper.isNotEmpty(exchange.getIn().getHeader(GitConstants.GIT_COMMIT_ID)))
{
+                commitId = exchange.getIn().getHeader(GitConstants.GIT_COMMIT_ID, String.class);
+            } else {
+                throw new IllegalArgumentException("Commit id must be specified to execute
" + operation);
+            }
+        	RevWalk walk = new RevWalk(repo);
+        	ObjectId id = repo.resolve(commitId);
+        	RevCommit commit = walk.parseCommit(id);
+        	walk.dispose();
+            if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) {
+                git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call();
+            }
+            result = git.cherryPick().include(commit).call();
+        } catch (Exception e) {
+            LOG.error("There was an error in Git " + operation + " operation");
+            throw e;
+        }
+        exchange.getOut().setBody(result);
+    }
 
     private Repository getLocalRepository() throws IOException {
         FileRepositoryBuilder builder = new FileRepositoryBuilder();

http://git-wip-us.apache.org/repos/asf/camel/blob/b8dbbf40/components/camel-git/src/test/java/org/apache/camel/component/git/producer/GitProducerTest.java
----------------------------------------------------------------------
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 1a4fc2d..00a234e 100755
--- 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
@@ -831,6 +831,215 @@ public class GitProducerTest extends GitTestSupport {
         repository.close();
     }
     
+    @Test
+    public void cherryPickTest() throws Exception {
+
+        Repository repository = getTestRepository();
+        
+        File fileToAdd = new File(gitLocalRepo, filenameToAdd);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, filenameToAdd);
+            }
+        });
+        File gitDir = new File(gitLocalRepo, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(filenameToAdd));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, commitMessage);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), commitMessage);
+            count++;
+        }
+        assertEquals(count, 1);
+        
+        template.sendBody("direct:create-branch", "");
+        
+        List<Ref> branches = template.requestBody("direct:show-branches", "", List.class);
+        
+        Boolean branchExists = false;
+        
+        for (Ref reference : branches) {
+            if (("refs/heads/" + branchTest).equals(reference.getName())) {
+                branchExists = true;
+            }
+        }
+        assertTrue(branchExists);
+        
+        File fileToAdd1 = new File(gitLocalRepo, "filetest1test.txt");
+        fileToAdd1.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, "filetest1test.txt");
+            }
+        });
+        
+        status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains("filetest1test.txt"));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, "Test second
commit");
+            }
+        });
+        logs = new Git(repository).log()
+                .call();
+        count = 0;
+        String id = "";
+        for (RevCommit rev : logs) {
+            if (count == 0) { 
+            	id = rev.getName();
+            	assertEquals(rev.getShortMessage(), "Test second commit");
+            }
+        	count++;
+        }
+        assertEquals(count, 2);
+        
+        final String str = id;
+        template.send("direct:cherrypick", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_ID, str);
+            }
+        });
+        
+        Git git = new Git(repository);
+        git.checkout().setCreateBranch(false).setName(branchTest).call();
+        logs = git.log()
+                .call();
+        count = 0;
+        for (RevCommit rev : logs) {
+            if (count == 0) {
+            	assertEquals(rev.getShortMessage(), "Test second commit");
+            }
+            count++;
+        }
+        assertEquals(count, 2);
+        repository.close();
+    }
+    
+    @Test
+    public void cherryPickBranchToMasterTest() throws Exception {
+
+        Repository repository = getTestRepository();
+        
+        File fileToAdd = new File(gitLocalRepo, filenameToAdd);
+        fileToAdd.createNewFile();
+        
+        template.send("direct:add", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, filenameToAdd);
+            }
+        });
+        File gitDir = new File(gitLocalRepo, ".git");
+        assertEquals(gitDir.exists(), true);
+        
+        Status status = new Git(repository).status().call();
+        assertTrue(status.getAdded().contains(filenameToAdd));
+        
+        template.send("direct:commit", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, commitMessage);
+            }
+        });
+        Iterable<RevCommit> logs = new Git(repository).log()
+                .call();
+        int count = 0;
+        
+        for (RevCommit rev : logs) {
+            assertEquals(rev.getShortMessage(), commitMessage);
+            count++;
+        }
+        assertEquals(count, 1);
+        
+        template.sendBody("direct:create-branch", "");
+        
+        List<Ref> branches = template.requestBody("direct:show-branches", "", List.class);
+        
+        Boolean branchExists = false;
+        
+        for (Ref reference : branches) {
+            if (("refs/heads/" + branchTest).equals(reference.getName())) {
+                branchExists = true;
+            }
+        }
+        assertTrue(branchExists);
+        
+        File fileToAdd1 = new File(gitLocalRepo, "filetest1test.txt");
+        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, "filetest1test.txt");
+            }
+        });
+        Git git = new Git(repository);
+        git.checkout().setCreateBranch(false).setName(branchTest).call();
+        status = git.status().call();
+        assertTrue(status.getAdded().contains("filetest1test.txt"));
+        
+        template.send("direct:commit-branch", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_MESSAGE, "Test second
commit");
+            }
+        });
+        logs = git.log()
+                .call();
+        count = 0;
+        String id = "";
+        for (RevCommit rev : logs) {
+            if (count == 0) { 
+            	id = rev.getName();
+            	assertEquals(rev.getShortMessage(), "Test second commit");
+            }
+        	count++;
+        }
+        assertEquals(count, 2);
+        
+        final String str = id;
+        template.send("direct:cherrypick-master", new Processor() {
+            @Override
+            public void process(Exchange exchange) throws Exception {
+                exchange.getIn().setHeader(GitConstants.GIT_COMMIT_ID, str);
+            }
+        });
+        
+        git = new Git(repository);
+        git.checkout().setCreateBranch(false).setName("refs/heads/master").call();
+        logs = git.log()
+                .call();
+        count = 0;
+        for (RevCommit rev : logs) {
+            if (count == 0) {
+            	assertEquals(rev.getShortMessage(), "Test second commit");
+            }
+            count++;
+        }
+        assertEquals(count, 2);
+        repository.close();
+    }
+    
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {            
@@ -874,6 +1083,10 @@ public class GitProducerTest extends GitTestSupport {
                         .to("git://" + gitLocalRepo + "?operation=deleteTag&tagName="
+ tagTest);
                 from("direct:show-branches")
                         .to("git://" + gitLocalRepo + "?operation=showBranches");
+                from("direct:cherrypick")
+                        .to("git://" + gitLocalRepo + "?operation=cherryPick&branchName="
+ branchTest);
+                from("direct:cherrypick-master")
+                        .to("git://" + gitLocalRepo + "?operation=cherryPick&branchName=refs/heads/master");
             } 
         };
     }


Mime
View raw message