Skip to content
This repository has been archived by the owner on Dec 18, 2022. It is now read-only.

Commit

Permalink
Resolve #4, support committing specific files.
Browse files Browse the repository at this point in the history
- Rename unittests.xml to integration-tests.xml.
- Add tests for committing multiple specific paths.
  • Loading branch information
yveszoundi committed Feb 23, 2014
1 parent 7053fb1 commit d907057
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 7 deletions.
2 changes: 1 addition & 1 deletion README.org
Expand Up @@ -12,7 +12,7 @@ Maven/Apache Ivy repository hosting is provided via [[https://oss.sonatype.org/i

* Tasks description
For an overview of available Ant tasks, please consult the [[https://github.com/rimerosolutions/ant-git-tasks/wiki][wiki pages]].
The [[https://github.com/rimerosolutions/ant-git-tasks/blob/master/src/test/resources/unittests.xml][available Apache AntUnit tests]] give a good overview of the intended usage.
The [[https://github.com/rimerosolutions/ant-git-tasks/blob/master/src/test/resources/integration-tests.xml][available Apache AntUnit tests]] give a good overview of the intended usage.
The *ant-git-tasks* project is tested with Apache Ant itself, to have a feel of what's working and how usable the tasks are.

* General information
Expand Down
2 changes: 1 addition & 1 deletion example-pom.xml
Expand Up @@ -96,7 +96,7 @@
<taskdef uri="antlib:com.rimerosolutions.ant.git"
resource="com/rimerosolutions/ant/git/jgit-ant-lib.xml"
classpath="${plugin_classpath}"/>
<!-- <ant antfile="${basedir}/src/test/resources/unittests.xml" target="testAdd"/>
<!-- <ant antfile="${basedir}/src/test/resources/integration-tests.xml" target="testAdd"/>
-->

<git:settings refId="git.test"
Expand Down
56 changes: 52 additions & 4 deletions src/main/java/com/rimerosolutions/ant/git/tasks/CommitTask.java
Expand Up @@ -20,7 +20,10 @@
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.revwalk.RevCommit;

import org.apache.tools.ant.types.FileSet;
import org.apache.tools.ant.types.resources.Union;
import java.io.File;
import java.io.IOException;
import com.rimerosolutions.ant.git.AbstractGitRepoAwareTask;
import com.rimerosolutions.ant.git.GitBuildException;
import com.rimerosolutions.ant.git.GitTaskUtils;
Expand All @@ -47,7 +50,8 @@ public class CommitTask extends AbstractGitRepoAwareTask {
private String reflogComment;
private String revCommitIdProperty;
private String only;

private Union path;

@Override
public String getName() {
return TASK_NAME;
Expand All @@ -62,6 +66,34 @@ public String getName() {
public void setOnly(String only) {
this.only = only;
}

/**
* Configure the fileset(s) of files to add to revision control
*
* @param fileset The fileset to add
*/
public void addFileset(FileSet fileset) {
getPath().add(fileset);
}

private synchronized Union getPath() {
if (path == null) {
path = new Union();
path.setProject(getProject());
}
return path;
}

private String translateFilePathUsingPrefix(String file, String prefix) throws IOException {
if (file.equals(prefix)) {
return ".";
}
String result = new File(file).getCanonicalPath().substring(prefix.length() + 1);
if (File.separatorChar != '/') {
result = result.replace(File.separatorChar, '/');
}
return result;
}

/**
* Assign the commit revision id to a property
Expand All @@ -79,13 +111,26 @@ protected void doExecute() throws BuildException {
setFailOnError(true);
CommitCommand cmd = git.commit();

if (message != null) {
if (!GitTaskUtils.isNullOrBlankString(message)) {
cmd.setMessage(GitTaskUtils.BRANDING_MESSAGE + " " + message);
}
else {
cmd.setMessage(GitTaskUtils.BRANDING_MESSAGE);
}

String prefix = getDirectory().getCanonicalPath();
String[] allFiles = getPath().list();

if (!GitTaskUtils.isNullOrBlankString(only)) {
cmd.setOnly(only);
}
else if (allFiles.length > 0) {
for (String file : allFiles) {
String modifiedFile = translateFilePathUsingPrefix(file, prefix);
log("Will commit " + modifiedFile);
cmd.setOnly(modifiedFile);
}
}
else {
cmd.setAll(true);
}
Expand All @@ -104,7 +149,10 @@ protected void doExecute() throws BuildException {
}

log(revCommit.getFullMessage());
} catch (GitAPIException ex) {
} catch (IOException ioe) {
throw new GitBuildException(MESSAGE_COMMIT_FAILED, ioe);
}
catch (GitAPIException ex) {
throw new GitBuildException(MESSAGE_COMMIT_FAILED, ex);
}
}
Expand Down
Expand Up @@ -297,12 +297,25 @@ limitations under the License.
<git:commit message="${dummy.commit.message}" revCommitIdProperty="testAdd.revcommit"/>
<git:uptodate failOnError="true"/>
</git:git>


<echo file="${testLocalRepo}/test-fileset1.txt" message="test"/>
<echo file="${testLocalRepo}/test-fileset2.txt" message="test"/>
<git:git directory="${testLocalRepo}" verbose="true" settingsRef="git.testing">
<git:add>
<fileset dir="${testLocalRepo}" includes="test-fileset*.txt"/>
</git:add>
<git:commit message="${dummy.commit.message}" revCommitIdProperty="testAdd.revcommit">
<fileset dir="${testLocalRepo}" includes="test-fileset*.txt"/>
</git:commit>
<git:uptodate failOnError="true"/>
</git:git>

<au:expectfailure>
<git:git directory="${testLocalRepo}" verbose="true" settingsRef="git.testing">
<git:commit message="${dummy.commit.message}" only="test.txt"/>
</git:git>
</au:expectfailure>

</target>

<target name="testClean" description="Test git-clean">
Expand Down

0 comments on commit d907057

Please sign in to comment.