diff --git a/src/hunternif/jgitversion/JGitVersionTask.java b/src/hunternif/jgitversion/JGitVersionTask.java index 5053c10..da85e01 100644 --- a/src/hunternif/jgitversion/JGitVersionTask.java +++ b/src/hunternif/jgitversion/JGitVersionTask.java @@ -10,6 +10,7 @@ import org.apache.tools.ant.BuildException; import org.apache.tools.ant.Project; import org.apache.tools.ant.Task; +import org.apache.tools.ant.types.EnumeratedAttribute; import org.eclipse.jgit.api.Git; import org.eclipse.jgit.api.errors.GitAPIException; import org.eclipse.jgit.lib.ObjectId; @@ -23,25 +24,33 @@ import org.gitective.core.filter.commit.CommitCountFilter; public class JGitVersionTask extends Task { - private String dir; + + public static class BooleanOrAuto extends EnumeratedAttribute { + @Override + public String[] getValues() { + return new String[] { "true", "false", "auto" }; + } + } + + private File dir; private String property; - private boolean tagonly; + private BooleanOrAuto tagonly; - public void setDir(String dir) { + public void setDir(File dir) { this.dir = dir; } public void setProperty(String property) { this.property = property; } - public void setTagonly(boolean tagonly) { + public void setTagonly(BooleanOrAuto tagonly) { this.tagonly = tagonly; } @Override public void execute() throws BuildException { try { - String version = getProjectVersion(new File(dir)); + String version = getProjectVersion(dir); Project project = getProject(); if (project != null) { project.setProperty(property, version); @@ -111,7 +120,19 @@ public String getProjectVersion(File repoDir) throws IOException, GitAPIExceptio if (tagName.isEmpty()) { version = "0"; } - version += tagName + ((!tagonly) ? "." + commitsSinceLastMasterTag : ""); + + boolean tagonlyEvaluated = false; // Default if attribute not set + if (tagonly != null) { + boolean isAuto = tagonly.getValue().equalsIgnoreCase("auto"); + if (isAuto) { + tagonlyEvaluated = commitsSinceLastMasterTag == 0; + } + else { + tagonlyEvaluated = Boolean.parseBoolean(tagonly.getValue()); + } + } + + version += tagName + ((!tagonlyEvaluated) ? "." + commitsSinceLastMasterTag : ""); return version; } diff --git a/test/hunternif/jgitversion/TestGit.java b/test/hunternif/jgitversion/TestGit.java index 1c6fb2c..51c278e 100644 --- a/test/hunternif/jgitversion/TestGit.java +++ b/test/hunternif/jgitversion/TestGit.java @@ -5,7 +5,7 @@ public class TestGit { public static void main(String[] args) { try { - System.out.println(JGitVersionTask.getProjectVersion(new File("."))); + System.out.println(new JGitVersionTask().getProjectVersion(new File("."))); } catch (Exception e) { e.printStackTrace(); }