diff --git a/CHANGELOG.md b/CHANGELOG.md new file mode 100644 index 0000000..5479b16 --- /dev/null +++ b/CHANGELOG.md @@ -0,0 +1,14 @@ +# Git Release Notes +# -------------------------------------------------------------------- +# Date: +06-Jun-2016 +# Version: +2.3.0 +# Supported Mule Runtime Versions: +${project.devkitVersion} +# New Features and Functionality +Updated to run on Mule Runtime ${project.devkitVersion} +# Closed Issues in this release +None +# Known Issues in this release +None diff --git a/README.adoc b/README.adoc new file mode 100644 index 0000000..34b9624 --- /dev/null +++ b/README.adoc @@ -0,0 +1,526 @@ + +:toc: left +:toc-title: Connector Summary +:toclevels: 2 +:last-update-label!: +:docinfo: +:source-highlighter: coderay +:icons: font + + += Git Connector API Reference + + +*Additional Info* +[width="50", cols=".<60%,^40%" ] +|====================== +| Requires Mule Enterprise License | No icon:remove[] {nbsp} +| Requires Entitlement | No icon:remove[] {nbsp} +| Mule Version | 3.5.0 or higher +|====================== + + +== Configs +--- +=== Git Configuration +`` + + +`Connection Management` + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +| Name | Java Type | Description | Default Value | Required +|name | +++String+++ | The name of this configuration. With this name can be later referenced. | | *x*{nbsp} +| username | +++String+++ | +++A username+++ | | *x*{nbsp} +| password | +++String+++ | +++A password+++ | | *x*{nbsp} +| directory | +++String+++ | +++Default repository base directory+++ | | {nbsp} +|====================== + + + +== Processors + +--- + +=== Clone repository +`` + + + + ++++ +Clone a repository into a new directory or resset it if it exists +../../../doc/mule-module-git.xml.sample git:clone ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +uri | +++String+++ | +++The (possibly remote) repository to clone from.+++ | | *x*{nbsp} + + + + +| +bare | +++boolean+++ | +++True if you want a bare Git repository, false otherwise.+++ | false | {nbsp} + + + + +| +remote | +++String+++ | +++Name of the remote to keep track of the upstream repository.+++ | origin | {nbsp} + + + + +| +branch | +++String+++ | +++Name of the local branch into which the remote will be cloned.+++ | HEAD | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Reset repository +`` + + + + + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +branch | +++String+++ | ++++++ | HEAD | {nbsp} + + + + +| +overrideDirectory | +++String+++ | ++++++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Add +`` + + + + ++++ +Add file contents to the index +../../../doc/mule-module-git.xml.sample git:add ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +filePatterns | +++String+++ | +++List of file patterns to add content from. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively. Use semicolon (";") to separete patterns+++ | | {nbsp} + + + + +| +forceAll | +++boolean+++ | +++Add all files+++ | false | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Create branch +`` + + + + ++++ +Create a local branch +../../../doc/mule-module-git.xml.sample git:create-branch ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +branchName | +++String+++ | +++Name of the new branch+++ | | *x*{nbsp} + + + + +| +force | +++boolean+++ | +++If true and the branch with the given name already exists, the start-point of an existing branch will be set to a new start-point; if false, the existing branch will not be changed.+++ | false | {nbsp} + + + + +| +startPoint | +++String+++ | +++The new branch head will point to this commit. It may be given as a branch name, a commit-id, or a tag. If this option is omitted, the current HEAD will be used instead.+++ | HEAD | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Delete branch +`` + + + + ++++ +Delete local branch +../../../doc/mule-module-git.xml.sample git:delete-branch ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +branchName | +++String+++ | +++Name of the branch to delete+++ | | *x*{nbsp} + + + + +| +force | +++boolean+++ | +++If false a check will be performed whether the branch to be deleted is already merged into the current branch and deletion will be refused in this case+++ | | *x*{nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Commit +`` + + + + ++++ +Record changes to the repository +../../../doc/mule-module-git.xml.sample git:commit ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +msg | +++String+++ | +++Commit message+++ | | *x*{nbsp} + + + + +| +committerName | +++String+++ | +++Name of the person performing this commit+++ | | *x*{nbsp} + + + + +| +committerEmail | +++String+++ | +++Email of the person performing this commit+++ | | *x*{nbsp} + + + + +| +authorName | +++String+++ | +++Name of the author of the changes to commit+++ | | {nbsp} + + + + +| +authorEmail | +++String+++ | +++Email of the author of the changes to commit+++ | | {nbsp} + + + + +| +all | +++boolean+++ | +++If set to true the Commit command automatically stages files that have been modified and deleted, but new files not known by the repository are not affected.+++ | false | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Push +`` + + + + ++++ +Update remote refs along with associated objects +../../../doc/mule-module-git.xml.sample git:push ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +remote | +++String+++ | +++The remote (uri or name) used for the push operation.+++ | origin | {nbsp} + + + + +| +force | +++boolean+++ | +++Sets the force preference for push operation+++ | false | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Pull +`` + + + + ++++ +Fetch from and merge with another repository or a local branch +../../../doc/mule-module-git.xml.sample git:pull ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Fetch +`` + + + + ++++ +Fetch changes from another repository +../../../doc/mule-module-git.xml.sample git:fetch ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + +--- + +=== Checkout +`` + + + + ++++ +Checkout a local branch or create a local branch from a remote branch +../../../doc/mule-module-git.xml.sample git:checkout +or +../../../doc/mule-module-git.xml.sample git:checkout ++++ + + + + +==== Attributes +[cols=".^20%,.^20%,.^35%,.^20%,^.^5%", options="header"] +|====================== +|Name |Java Type | Description | Default Value | Required +| config-ref | +++String+++ | Specify which config to use | |*x*{nbsp} + + + +| +branch | +++String+++ | +++Name of the branch to checkout+++ | | *x*{nbsp} + + + + +| +startPoint | +++String+++ | +++If specified creates a new branch pointing to this startPoint+++ | | {nbsp} + + + + +| +overrideDirectory | +++String+++ | +++Name of the directory to use for git repository+++ | | {nbsp} + + +|====================== + + + + + + + + + + + + + + + diff --git a/README.md b/README.md deleted file mode 100644 index 8879c7a..0000000 --- a/README.md +++ /dev/null @@ -1,241 +0,0 @@ -Mule Git Connector -================== - -Git is a free and open source, distributed version control system designed to handle everything from small to very large projects with speed and efficiency. This connector will allow to interact with Git repositories. - -Installation ------------- - -The connector can either be installed for all applications running within the Mule instance or can be setup to be used -for a single application. - -*All Applications* - -Download the connector from the link above and place the resulting jar file in -/lib/user directory of the Mule installation folder. - -*Single Application* - -To make the connector available only to single application then place it in the -lib directory of the application otherwise if using Maven to compile and deploy -your application the following can be done: - -Add the connector's maven repo to your pom.xml: - - - - muleforge-releases - MuleForge Releases Repository - https://repository.muleforge.org/releases/ - default - - - -Add the connector as a dependency to your project. This can be done by adding -the following under the dependencies element in the pom.xml file of the -application: - - - org.mule.modules - mule-module-git - 1.4-SNAPSHOT - - -Configuration -------------- - -You can configure the connector as follows: - - - -Here is detailed list of all the configuration attributes: - -| attribute | description | optional | default value | -|:-----------|:-----------|:---------|:--------------| -|name|Give a name to this configuration so it can be later referenced by config-ref.|yes|| -|directory|Directory of your git repository|no| - - -Clone ------ - -Clone a repository into a new directory - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|uri| The (possibly remote) repository to clone from.|no|| -|bare| True if you want a bare Git repository, false otherwise.|yes|false| -|remote| Name of the remote to keep track of the upstream repository.|yes|origin| -|branch| Name of the local branch into which the remote will be cloned.|yes|HEAD| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Add ---- - -Add file contents to the index - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|filePattern| File to add content from. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all files in the directory, recursively.|no|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Create Branch -------------- - -Create a local branch - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|name| Name of the new branch|no|| -|force| If true and the branch with the given name already exists, the start-point of an existing branch will be set to a new start-point; if false, the existing branch will not be changed.|yes|false| -|startPoint| The new branch head will point to this commit. It may be given as a branch name, a commit-id, or a tag. If this option is omitted, the current HEAD will be used instead.|yes|HEAD| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Delete Branch -------------- - -Delete local branch - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|name| Name of the branch to delete|no|| -|force| If false a check will be performed whether the branch to be deleted is already merged into the current branch and deletion will be refused in this case|no|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Commit ------- - -Record changes to the repository - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|msg| Commit message|no|| -|committerName| Name of the person performing this commit|no|| -|committerEmail| Email of the person performing this commit|no|| -|authorName| Name of the author of the changes to commit|yes|| -|authorEmail| Email of the author of the changes to commit|yes|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Push ----- - -Update remote refs along with associated objects - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|remote| The remote (uri or name) used for the push operation.|yes|origin| -|force| Sets the force preference for push operation|yes|false| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Pull ----- - -Fetch from and merge with another repository or a local branch - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Fetch ------ - -Fetch changes from another repository - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - -Checkout --------- - -Checkout a local branch or create a local branch from a remote branch - - - -or - - - - - - - -| attribute | description | optional | default value | possible values | -|:-----------|:-----------|:---------|:--------------|:----------------| -|config-ref|Specify which configuration to use for this invocation|yes|| -|branch| Name of the branch to checkout|no|| -|startPoint| If specified creates a new branch pointing to this startPoint|yes|| -|overrideDirectory| Name of the directory to use for git repository|yes|| - - - - - - - - - - - - - - - diff --git a/doc/mule-module-git.xml.sample b/doc/mule-module-git.xml.sample deleted file mode 100644 index c157c48..0000000 --- a/doc/mule-module-git.xml.sample +++ /dev/null @@ -1,41 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/icons/git-connector-24x16.png b/icons/git-connector-24x16.png index e8650be..2155eb2 100644 Binary files a/icons/git-connector-24x16.png and b/icons/git-connector-24x16.png differ diff --git a/icons/git-connector-48x32.png b/icons/git-connector-48x32.png index 3ec0590..567c40f 100644 Binary files a/icons/git-connector-48x32.png and b/icons/git-connector-48x32.png differ diff --git a/pom.xml b/pom.xml index 317904e..e857ef1 100644 --- a/pom.xml +++ b/pom.xml @@ -1,65 +1,33 @@ - - org.mule.tools.devkit - mule-devkit-parent - 3.3.1 - + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 org.mule.modules mule-module-git + 2.3.0-SNAPSHOT mule-module - Mule Git Connector - 2.2-SNAPSHOT - Git is a free and open source, distributed version control system designed to handle everything from - small to very large projects with speed and efficiency. This connector will allow to interact with Git - repositories. - - http://www.muleforge.org/projects/mule-module-git + Mule Git Anypoint Connector - - false - + + org.mule.tools.devkit + mule-devkit-parent + 3.8.0 + org.eclipse.jgit org.eclipse.jgit - 1.3.0 - - - - com.sun.jersey - jersey-client - 1.3 + 4.3.0.201604071810-r - - - org.mule.transports - mule-transport-axis - ${mule.version} - - - - org.mule.transports - mule-transport-vm - ${mule.version} - test - - - - scm:git:git://github.com:mulesoft/git-connector.git - - - scm:git:git@github.com:mulesoft/git-connector.git - - http://github.com/mulesoft/git-connector - - + + Community + LICENSE.md + false + mulesoft-releases @@ -67,6 +35,9 @@ http://repository.mulesoft.org/releases/ default + + jgit-repository + https://repo.eclipse.org/content/groups/releases/ + - diff --git a/src/main/java/org/mule/modules/git/GitConnector.java b/src/main/java/org/mule/modules/git/GitConnector.java new file mode 100644 index 0000000..1b28634 --- /dev/null +++ b/src/main/java/org/mule/modules/git/GitConnector.java @@ -0,0 +1,419 @@ +package org.mule.modules.git; + +import java.io.File; +import java.io.IOException; + +import org.eclipse.jgit.api.AddCommand; +import org.eclipse.jgit.api.CheckoutCommand; +import org.eclipse.jgit.api.CloneCommand; +import org.eclipse.jgit.api.CommitCommand; +import org.eclipse.jgit.api.CreateBranchCommand; +import org.eclipse.jgit.api.DeleteBranchCommand; +import org.eclipse.jgit.api.FetchCommand; +import org.eclipse.jgit.api.Git; +import org.eclipse.jgit.api.PullCommand; +import org.eclipse.jgit.api.PushCommand; +import org.eclipse.jgit.api.ResetCommand.ResetType; +import org.eclipse.jgit.lib.Repository; +import org.eclipse.jgit.storage.file.FileRepositoryBuilder; +import org.mule.api.annotations.Config; +import org.mule.api.annotations.Connector; +import org.mule.api.annotations.Processor; +import org.mule.api.annotations.param.Default; +import org.mule.api.annotations.param.Optional; +import org.mule.modules.git.config.ConnectorConfig; + +@Connector(name = "git", friendlyName = "Git") +public class GitConnector { + + @Config + ConnectorConfig config; + + /** + * Clone a repository into a new directory or resset it if it exists + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:clone} + * + * @param uri + * The (possibly remote) repository to clone from. + * @param bare + * True if you want a bare Git repository, false otherwise. + * @param remote + * Name of the remote to keep track of the upstream repository. + * @param branch + * Name of the local branch into which the remote will be cloned. + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void cloneRepository(String uri, @Default("false") boolean bare, @Default("origin") String remote, @Default("HEAD") String branch, + @Optional String overrideDirectory) { + File dir = resolveDirectory(overrideDirectory); + boolean dirExists = dir.exists(); + + if (!dirExists) { + if (!dir.mkdirs()) { + throw new RuntimeException("Directory " + dir.getAbsolutePath() + " cannot be created"); + } + } + Git git = null; + try { + if (dirExists) { + git = Git.open(dir); + git.log().setMaxCount(1).call(); + } else { + CloneCommand cloneCommand = Git.cloneRepository(); + cloneCommand.setCredentialsProvider(config.getCredentialsProvider()); + cloneCommand.setBare(bare); + cloneCommand.setDirectory(dir); + cloneCommand.setRemote(remote); + cloneCommand.setBranch(branch); + cloneCommand.setURI(uri); + git = cloneCommand.call(); + } + } catch (Exception e) { + throw new RuntimeException("Cannot clone or open repository", e); + } finally { + if (git != null) { + git.close(); + } + } + + } + + @Processor + public void resetRepository(@Default("HEAD") String branch, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + try { + git.reset().setMode(ResetType.HARD).setRef(branch).call(); + } catch (Exception e) { + throw new RuntimeException("Cannot reset repository", e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Add file contents to the index + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:add} + * + * @param filePatterns + * List of file patterns to add content from. Also a leading directory name (e.g. dir to add dir/file1 and dir/file2) can be given to add all + * files in the directory, recursively. Use semicolon (";") to separete patterns + * @param forceAll + * Add all files + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void add(@Optional String filePatterns, @Default("false") boolean forceAll, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + try { + AddCommand add = git.add(); + if (filePatterns != null) { + for (String file : filePatterns.split(";")) { + if (!file.trim().isEmpty()) { + add.addFilepattern(file.trim()); + } + } + } + if (forceAll) { + add.addFilepattern("."); + } + add.call(); + } catch (Exception e) { + throw new RuntimeException("Cannot add " + filePatterns, e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Create a local branch + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:create-branch} + * + * @param branchName + * Name of the new branch + * @param force + * If true and the branch with the given name already exists, the start-point of an existing branch will be set to a new start-point; if false, + * the existing branch will not be changed. + * @param startPoint + * The new branch head will point to this commit. It may be given as a branch name, a commit-id, or a tag. If this option is omitted, the current + * HEAD will be used instead. + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void createBranch(String branchName, @Default("false") boolean force, @Default("HEAD") String startPoint, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + try { + CreateBranchCommand createBranch = git.branchCreate(); + createBranch.setName(branchName); + createBranch.setForce(force); + createBranch.setStartPoint(startPoint); + createBranch.call(); + git.close(); + } catch (Exception e) { + throw new RuntimeException("Unable to create branch " + branchName, e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Delete local branch + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:delete-branch} + * + * @param branchName + * Name of the branch to delete + * @param force + * If false a check will be performed whether the branch to be deleted is already merged into the current branch and deletion will be refused in + * this case + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void deleteBranch(String branchName, boolean force, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + try { + DeleteBranchCommand deleteBranch = git.branchDelete(); + deleteBranch.setBranchNames(branchName); + deleteBranch.setForce(force); + + deleteBranch.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to create branch " + branchName, e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Record changes to the repository + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:commit} + * + * @param msg + * Commit message + * @param committerName + * Name of the person performing this commit + * @param committerEmail + * Email of the person performing this commit + * @param authorName + * Name of the author of the changes to commit + * @param authorEmail + * Email of the author of the changes to commit + * @param all + * If set to true the Commit command automatically stages files that have been modified and deleted, but new files not known by the repository + * are not affected. + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void commit(String msg, String committerName, String committerEmail, @Optional String authorName, @Optional String authorEmail, + @Default("false") boolean all, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + + try { + CommitCommand commit = git.commit(); + if (authorName != null && authorEmail != null) { + commit.setAuthor(authorName, authorEmail); + } + + commit.setCommitter(committerName, committerEmail); + commit.setMessage(msg); + commit.setAll(all); + + commit.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to commit", e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Update remote refs along with associated objects + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:push} + * + * @param remote + * The remote (uri or name) used for the push operation. + * @param force + * Sets the force preference for push operation + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void push(@Default("origin") String remote, @Default("false") boolean force, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + + try { + PushCommand push = git.push(); + push.setCredentialsProvider(config.getCredentialsProvider()); + push.setRemote(remote); + push.setForce(force); + + push.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to push to " + remote, e); + } finally { + if (git != null) { + git.close(); + } + } + + } + + /** + * Fetch from and merge with another repository or a local branch + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:pull} + * + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void pull(@Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + + try { + PullCommand pull = git.pull(); + pull.setCredentialsProvider(config.getCredentialsProvider()); + + pull.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to pull", e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Fetch changes from another repository + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:fetch} + * + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void fetch(@Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + + try { + FetchCommand fetch = git.fetch(); + fetch.setCredentialsProvider(config.getCredentialsProvider()); + + fetch.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to fetch", e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Checkout a local branch or create a local branch from a remote branch + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:checkout} + * + * or + * + * {@sample.xml ../../../doc/mule-module-git.xml.sample git:checkout} + * + * @param startPoint + * If specified creates a new branch pointing to this startPoint + * @param branch + * Name of the branch to checkout + * @param overrideDirectory + * Name of the directory to use for git repository + */ + @Processor + public void checkout(String branch, @Optional String startPoint, @Optional String overrideDirectory) { + Git git = getGitIntance(overrideDirectory); + + try { + CheckoutCommand checkout = git.checkout(); + + checkout.setName(branch); + if (startPoint != null) { + checkout.setCreateBranch(true); + checkout.setStartPoint(startPoint); + } + + checkout.call(); + } catch (Exception e) { + throw new RuntimeException("Unable to fetch", e); + } finally { + if (git != null) { + git.close(); + } + } + } + + /** + * Depending on the value of overrideDirectory, it returns the directory to use as the git repo. + * + * @param overrideDirectory + * path or null if default is required + * @return file pointing to repository directory to use + */ + private File resolveDirectory(String overrideDirectory) { + File dir; + if (overrideDirectory == null) { + dir = new File(config.getDirectory()); + } else { + dir = new File(overrideDirectory); + } + return dir; + } + + // private Repository getGitRepo(String overrideDirectory) throws IOException { + // File dir = resolveDirectory(overrideDirectory); + // if (!dir.exists()) { + // throw new RuntimeException("Directory " + dir.getAbsolutePath() + " does not exists"); + // } + // + // FileRepositoryBuilder builder = new FileRepositoryBuilder(); + // Repository repository = builder.setGitDir(dir).readEnvironment().findGitDir().build(); + // return repository; + // } + + private Git getGitIntance(String overrideDirectory) { + File dir = resolveDirectory(overrideDirectory); + Git git = null; + try { + git = Git.open(dir); + } catch (IOException e) { + throw new RuntimeException("Could not open Git repository", e); + } + return git; + } + + public ConnectorConfig getConfig() { + return config; + } + + public void setConfig(ConnectorConfig config) { + this.config = config; + } + +} \ No newline at end of file diff --git a/src/main/java/org/mule/modules/git/config/ConnectorConfig.java b/src/main/java/org/mule/modules/git/config/ConnectorConfig.java new file mode 100644 index 0000000..b321c0b --- /dev/null +++ b/src/main/java/org/mule/modules/git/config/ConnectorConfig.java @@ -0,0 +1,86 @@ +package org.mule.modules.git.config; + +import org.eclipse.jgit.transport.CredentialsProvider; +import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider; +import org.mule.api.ConnectionException; +import org.mule.api.annotations.Configurable; +import org.mule.api.annotations.Connect; +import org.mule.api.annotations.ConnectionIdentifier; +import org.mule.api.annotations.Disconnect; +import org.mule.api.annotations.TestConnectivity; +import org.mule.api.annotations.ValidateConnection; +import org.mule.api.annotations.components.ConnectionManagement; +import org.mule.api.annotations.display.Password; +import org.mule.api.annotations.param.ConnectionKey; +import org.mule.api.annotations.param.Optional; + +@ConnectionManagement(friendlyName = "Git Configuration") +public class ConnectorConfig { + + private String username; + private String password; + + /** + * Default repository base directory + */ + @Configurable + @Optional + private String directory; + + /** + * Connect + * + * @param username + * A username + * @param password + * A password + * @throws ConnectionException + */ + @Connect + @TestConnectivity + public void connect(@ConnectionKey String username, @Password String password) throws ConnectionException { + this.username = username; + this.password = password; + + } + + /** + * Disconnect + */ + @Disconnect + public void disconnect() { + this.username = null; + this.password = null; + } + + /** + * Are we connected + */ + @ValidateConnection + public boolean isConnected() { + // TODO: Change it to reflect that we are connected. + return username != null && password != null; + } + + /** + * Are we connected + */ + @ConnectionIdentifier + public String connectionId() { + return "001"; + } + + public CredentialsProvider getCredentialsProvider() { + CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password); + return credentialsProvider; + } + + public String getDirectory() { + return directory; + } + + public void setDirectory(String directory) { + this.directory = directory; + } + +} \ No newline at end of file diff --git a/src/main/resources/log4j2.xml b/src/main/resources/log4j2.xml new file mode 100644 index 0000000..dcdf122 --- /dev/null +++ b/src/main/resources/log4j2.xml @@ -0,0 +1,16 @@ + + + + %-5level %d [%t] %c:%M(%L): %m%n + + + + + + + + + + + + \ No newline at end of file