Change how version numbers are determined

Using the most recent tag as the version number does not work very well
because it a version behind what we are actually developing for. This
changes how version are determined:

1) If the current commit is a tag, use that tag as the version
      a) Make it a SNAPSHOT if the git tree is dirty

2) If we are on a version branch, (e.g. 0.10.1), then use
   that as the version, making it a SNAPSHOT

3) If we are not on a version branch, but the current branch tracks a
   version branch (using --track), use the tracking version branch as the
   version, making a it a SNAPSHOT

4) Otherwise, use a default 0.0.0-SNAPSHOT version

DFDL-763
diff --git a/daffodil-pack/project/build.scala b/daffodil-pack/project/build.scala
index 85fe658..e609db5 100644
--- a/daffodil-pack/project/build.scala
+++ b/daffodil-pack/project/build.scala
@@ -141,18 +141,48 @@
   s ++= Seq(Keys.version := {
     val describe = exec("git describe --long HEAD")
     assert(describe.length == 1)
-
-    val VersionRegex = """^(.+)-(.+)-(.+)$""".r
+    
+    val DescribeRegex = """^(.+)-(.+)-(.+)$""".r
     val res = describe(0) match {
-      case VersionRegex(v, "0", hash) => {
+      case DescribeRegex(taggedVersion, "0", hash) => {
+        // we are on a tag, build a tag release
         val status = exec("git status --porcelain")
         if (status.length > 0) {
-          v + "-SNAPSHOT"
+          taggedVersion + "-SNAPSHOT"
         } else {
-          v
+          taggedVersion
         }
       }
-      case VersionRegex(v, _, hash) => v + "-SNAPSHOT"
+      case DescribeRegex(version, _, hash) => {
+        // not on a tag
+
+        // get the current branch
+        val branch = exec("git rev-parse --abbrev-ref HEAD")
+        assert(branch.length == 1)
+        val VersionBranchRegex = """^\d+\.\d+\.\d+$""".r
+        branch(0) match {
+          case VersionBranchRegex => {
+            // we are developing on a version branch, create a snapshot
+            branch + "-SNAPSHOT"
+          }
+          case _ => {
+            // not on a version branch (e.g. a review branch), try to figure
+            // out the tracking branch
+            val trackingBranch = exec("git for-each-ref --format=%(upstream:short) refs/heads/" + branch(0))
+            assert(trackingBranch.length == 1)
+            val TrackingBranchRegex = """^[^/]+/(.+)$""".r
+            trackingBranch(0) match {
+              case TrackingBranchRegex(trackingVersion) => {
+                trackingVersion + "-SNAPSHOT"
+              }
+              case _ => {
+                // no idea what the version is, set it to a fefault
+                "0.0.0-SNAPSHOT"
+              }
+            }
+          }
+        }
+      }
     }
     res
   })
diff --git a/project/build.scala b/project/build.scala
index 8d3d9cb..69f0dad 100644
--- a/project/build.scala
+++ b/project/build.scala
@@ -247,18 +247,48 @@
   s ++= Seq(version := {
     val describe = exec("git describe --long HEAD")
     assert(describe.length == 1)
-
-    val VersionRegex = """^(.+)-(.+)-(.+)$""".r
+    
+    val DescribeRegex = """^(.+)-(.+)-(.+)$""".r
     val res = describe(0) match {
-      case VersionRegex(v, "0", hash) => {
+      case DescribeRegex(taggedVersion, "0", hash) => {
+        // we are on a tag, build a tag release
         val status = exec("git status --porcelain")
         if (status.length > 0) {
-          v + "-SNAPSHOT"
+          taggedVersion + "-SNAPSHOT"
         } else {
-          v
+          taggedVersion
         }
       }
-      case VersionRegex(v, _, hash) => v + "-SNAPSHOT"
+      case DescribeRegex(version, _, hash) => {
+        // not on a tag
+
+        // get the current branch
+        val branch = exec("git rev-parse --abbrev-ref HEAD")
+        assert(branch.length == 1)
+        val VersionBranchRegex = """^\d+\.\d+\.\d+$""".r
+        branch(0) match {
+          case VersionBranchRegex => {
+            // we are developing on a version branch, create a snapshot
+            branch + "-SNAPSHOT"
+          }
+          case _ => {
+            // not on a version branch (e.g. a review branch), try to figure
+            // out the tracking branch
+            val trackingBranch = exec("git for-each-ref --format=%(upstream:short) refs/heads/" + branch(0))
+            assert(trackingBranch.length == 1)
+            val TrackingBranchRegex = """^[^/]+/(.+)$""".r
+            trackingBranch(0) match {
+              case TrackingBranchRegex(trackingVersion) => {
+                trackingVersion + "-SNAPSHOT"
+              }
+              case _ => {
+                // no idea what the version is, set it to a fefault
+                "0.0.0-SNAPSHOT"
+              }
+            }
+          }
+        }
+      }
     }
     res
   })