CAMEL-7982: camel-git - A generic git component, add remove operation
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/db6f6e12 Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/db6f6e12 Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/db6f6e12 Branch: refs/heads/master Commit: db6f6e123136e05460aaa721228d326237b413be Parents: 9df0710 Author: Andrea Cosentino <[email protected]> Authored: Sat Jul 18 11:06:20 2015 +0200 Committer: Andrea Cosentino <[email protected]> Committed: Sat Jul 18 11:08:14 2015 +0200 ---------------------------------------------------------------------- .../camel/component/git/GitOperation.java | 1 + .../apache/camel/component/git/GitProducer.java | 24 ++++ .../github/producer/GitProducerTest.java | 137 +++++++++++++++++++ 3 files changed, 162 insertions(+) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/db6f6e12/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 f70728b..431540a 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 @@ -21,6 +21,7 @@ public interface GitOperation { public final static String CLONE_OPERATION = "clone"; public final static String INIT_OPERATION = "init"; public final static String ADD_OPERATION = "add"; + public final static String REMOVE_OPERATION = "remove"; 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/db6f6e12/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 1e0d3f5..c134475 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 @@ -49,6 +49,10 @@ public class GitProducer extends DefaultProducer{ doAdd(exchange, operation, repo); break; + case GitOperation.REMOVE_OPERATION: + doRemove(exchange, operation, repo); + break; + case GitOperation.COMMIT_OPERATION: doCommit(exchange, operation, repo); break; @@ -123,6 +127,26 @@ public class GitProducer extends DefaultProducer{ } } + protected void doRemove(Exchange exchange, String operation, Repository repo) { + Git git = null; + String fileName = null; + 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 = new Git(repo); + if (ObjectHelper.isNotEmpty(endpoint.getBranchName())) { + git.checkout().setCreateBranch(false).setName(endpoint.getBranchName()).call(); + } + git.rm().addFilepattern(fileName).call(); + } catch (Exception e) { + LOG.error("There was an error in Git " + operation + " operation"); + e.printStackTrace(); + } + } + protected void doCommit(Exchange exchange, String operation, Repository repo) { Git git = null; String commitMessage = null; http://git-wip-us.apache.org/repos/asf/camel/blob/db6f6e12/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 7122846..791e2aa 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 @@ -98,6 +98,57 @@ public class GitProducerTest extends CamelTestSupport { } @Test + public void removeTest() 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:remove", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_TO_ADD); + } + }); + gitDir = new File(GIT_LOCAL_REPO, ".git"); + assertEquals(gitDir.exists(), true); + + 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); + + status = new Git(repository).status().call(); + + assertFalse(status.getAdded().contains(FILENAME_TO_ADD)); + + repository.close(); + } + + @Test public void commitTest() throws Exception { Repository repository = getTestRepository(); @@ -190,6 +241,8 @@ public class GitProducerTest extends CamelTestSupport { repository.close(); } + + @Test public void commitAllTest() throws Exception { @@ -289,6 +342,86 @@ public class GitProducerTest extends CamelTestSupport { } @Test + public void removeFileBranchTest() 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); + + template.send("direct:remove-on-branch", new Processor() { + @Override + public void process(Exchange exchange) throws Exception { + exchange.getIn().setHeader(GitConstants.GIT_FILE_NAME, FILENAME_TO_ADD); + } + }); + + git = new Git(repository); + git.checkout().setCreateBranch(false).setName(BRANCH_TEST).call(); + + status = git.status().call(); + assertFalse(status.getAdded().contains(FILENAME_TO_ADD)); + + repository.close(); + } + + @Test public void createBranchTest() throws Exception { Repository repository = getTestRepository(); @@ -394,8 +527,12 @@ public class GitProducerTest extends CamelTestSupport { .to("git://" + GIT_LOCAL_REPO + "?operation=init"); from("direct:add") .to("git://" + GIT_LOCAL_REPO + "?operation=add"); + from("direct:remove") + .to("git://" + GIT_LOCAL_REPO + "?operation=rm"); from("direct:add-on-branch") .to("git://" + GIT_LOCAL_REPO + "?operation=add&branchName=" + BRANCH_TEST); + from("direct:remove-on-branch") + .to("git://" + GIT_LOCAL_REPO + "?operation=add&branchName=" + BRANCH_TEST); from("direct:commit") .to("git://" + GIT_LOCAL_REPO + "?operation=commit"); from("direct:commit-branch")
