Permalink
Browse files

Merge pull request #1 from barend/master

Support the --dirty flag of git-describe
  • Loading branch information...
2 parents e8ee5de + ae2c0c2 commit dd8613aeb3b3ef946e3832519adb42b7cb0152fc @lukegb committed May 11, 2012
Showing with 106 additions and 37 deletions.
  1. +1 −1 pom.xml
  2. +51 −0 readme.md
  3. +54 −36 src/main/java/com/lukegb/mojo/build/GitDescribeMojo.java
View
@@ -7,7 +7,7 @@
<artifactId>gitdescribe-maven-plugin</artifactId>
<packaging>maven-plugin</packaging>
<name>Git Describe Maven Plugin</name>
- <version>1.2</version>
+ <version>1.3-SNAPSHOT</version>
<inceptionYear>2011</inceptionYear>
View
@@ -0,0 +1,51 @@
+# Git-describe Maven plugin
+
+This plugin invokes `git describe` and captures the output in a build variable.
+
+## Configuration
+
+The plugin might be configured in your pom like so:
+
+ <plugin>
+ <groupId>com.lukegb.mojo</groupId>
+ <artifactId>gitdescribe-maven-plugin</artifactId>
+ <version><!-- Version --></version>
+ <executions>
+ <execution>
+ <goals>
+ <goal>gitdescribe</goal>
+ </goals>
+ <id>git-describe</id>
+ <phase>initialize</phase>
+ <configuration>
+ <!-- configuration properties go here. -->
+ </configuration>
+ </execution>
+ </executions>
+ </plugin>
+
+## Configuration properties
+
+The following configuration properties are available:
+
+ descriptionProperty (Default: describe)
+ The name of the build property that will contain the output of git
+ describe.
+
+ dirty (Default: false)
+ If true, pass the `--dirty` flag to git-describe.
+
+ dirtyMark (Default: dirty)
+ The <mark> value for the `--dirty` parameter.
+
+ failOutput (Default: unknown)
+ String indicating full output if getting version fails
+
+ outputPrefix (Default: git-)
+ String to prepend to git describe/shorttag output
+
+ outputSuffix
+ String to append to git describe/shorttag output.
+
+
+
@@ -16,27 +16,20 @@
* limitations under the License.
*/
+import java.io.BufferedReader;
import java.io.File;
+import java.io.InputStream;
+import java.io.InputStreamReader;
import org.apache.maven.plugin.AbstractMojo;
import org.apache.maven.plugin.MojoExecutionException;
import org.apache.maven.project.MavenProject;
import org.apache.maven.scm.ScmException;
-import org.apache.maven.scm.ScmFileStatus;
-import org.apache.maven.scm.ScmResult;
-import org.apache.maven.scm.log.ScmLogDispatcher;
-import org.apache.maven.scm.log.ScmLogger;
-import org.codehaus.plexus.util.StringUtils;
-
-
-import java.io.BufferedReader;
-import java.io.InputStreamReader;
-import java.io.InputStream;
/**
* Goal which sets project properties for describer from the
* current Git repository.
- *
+ *
* @author Luke Granger-Brown
* @goal gitdescribe
* @requiresProject
@@ -45,20 +38,17 @@
public class GitDescribeMojo
extends AbstractMojo
{
-
- private ScmLogDispatcher logger = new ScmLogDispatcher();
-
/**
* The maven project.
- *
+ *
* @parameter expression="${project}"
* @readonly
*/
private MavenProject project;
/**
* Local directory to be used to issue SCM actions
- *
+ *
* @parameter expression="${maven.changeSet.scmDirectory}" default-value="${basedir}
* @since 1.0
*/
@@ -68,10 +58,18 @@
* String to append to git describe/shorttag output
*
* @parameter default-value=""
+ * @deprecated superseded by outputSuffix.
*/
private String outputPostfix;
/**
+ * String to append to git describe/shorttag output.
+ *
+ * @parameter default-value=""
+ */
+ private String outputSuffix;
+
+ /**
* String to prepend to git describe/shorttag output
*
* @parameter default-value="git-"
@@ -85,19 +83,26 @@
*/
private String failOutput;
- private void checkResult( ScmResult result )
- throws MojoExecutionException
- {
- if ( !result.isSuccess() )
- {
- getLog().debug( "Provider message:" );
- getLog().debug( result.getProviderMessage() == null ? "" : result.getProviderMessage() );
- getLog().debug( "Command output:" );
- getLog().debug( result.getCommandOutput() == null ? "" : result.getCommandOutput() );
- throw new MojoExecutionException( "Command failed."
- + StringUtils.defaultString( result.getProviderMessage() ) );
- }
- }
+ /**
+ * The name of the build property that will contain the output of git describe.
+ *
+ * @parameter default-value="describe"
+ */
+ private String descriptionProperty;
+
+ /**
+ * If true, pass the `--dirty` flag to git-describe.
+ *
+ * @parameter default-value=false
+ */
+ private boolean dirty;
+
+ /**
+ * The &lt;mark&gt; value for the `--dirty` parameter.
+ *
+ * @parameter default-value="dirty"
+ */
+ private String dirtyMark;
public void execute()
throws MojoExecutionException
@@ -121,21 +126,26 @@ public void execute()
protected String getDescriber()
throws ScmException, MojoExecutionException
{
- if (outputPrefix == null) outputPrefix = "";
- if (outputPostfix == null) outputPostfix = "";
+ outputPrefix = firstNonNull(outputPrefix, "");
+ outputSuffix = firstNonNull(outputSuffix, outputPostfix, "");
// scmDirectory
- String command[] = {"git","describe"};
- String line = commandExecutor(command);
+ String line = commandExecutor(buildDescribeCommand());
if (line == null) {
String commandtwo[] = {"git","log","--pretty=format:\"%h\""};
line = commandExecutor(commandtwo);
if (line == null) {
line = failOutput;
}
}
- return outputPrefix + line + outputPostfix;
+ return outputPrefix + line + outputSuffix;
}
+ private String[] buildDescribeCommand()
+ {
+ return dirty
+ ? new String[] {"git", "describe", "--dirty=" + dirtyMark}
+ : new String[] {"git","describe"};
+ }
private String commandExecutor(String[] command)
{
@@ -152,7 +162,7 @@ private String commandExecutor(String[] command)
protected String getDescribeProperty()
{
- return getProperty( "describer" );
+ return getProperty( descriptionProperty );
}
protected String getProperty( String property )
@@ -162,7 +172,7 @@ protected String getProperty( String property )
private void setDescribeProperty( String describer )
{
- setProperty( "describe", describer );
+ setProperty( descriptionProperty, describer );
}
private void setProperty( String property, String value )
@@ -173,4 +183,12 @@ private void setProperty( String property, String value )
}
}
+ private static String firstNonNull(String... strings) {
+ for (String string : strings) {
+ if (string != null) {
+ return string;
+ }
+ }
+ return null;
+ }
}

0 comments on commit dd8613a

Please sign in to comment.