validating -dev versions

Change-Id: Ib6c4e164ab0715b6b52127e67e073532b346da83
diff --git a/jjb/shell/tagcollisionreject.sh b/jjb/shell/tagcollisionreject.sh
index 4a791b6..b72ff65 100755
--- a/jjb/shell/tagcollisionreject.sh
+++ b/jjb/shell/tagcollisionreject.sh
@@ -60,7 +60,7 @@
     VERSIONFILE="pom.xml"
   else
     echo "ERROR: No versioning file found!"
-    exit 1
+    fail_validation=1
   fi
 }
 
@@ -83,14 +83,84 @@
 
 # check if the version is already a tag in git
 function is_git_tag_duplicated {
-  for existing_tag in $(git tag)
+  while IFS= read -r existing_tag
   do
     if [ "$TAG_VERSION" = "$existing_tag" ]
     then
       echo "ERROR: Duplicate tag: $existing_tag"
-      exit 2
+      fail_validation=2
     fi
-  done
+  done <<< $existing_tags
+}
+
+# from https://github.com/cloudflare/semver_bash/blob/master/semver.sh
+function semverParseInto() {
+    local RE='[^0-9]*\([0-9]*\)[.]\([0-9]*\)[.]\([0-9]*\)\([0-9A-Za-z-]*\)'
+    #MAJOR
+    eval $2=`echo $1 | sed -e "s#$RE#\1#"`
+    #MINOR
+    eval $3=`echo $1 | sed -e "s#$RE#\2#"`
+    #MINOR
+    eval $4=`echo $1 | sed -e "s#$RE#\3#"`
+    #SPECIAL
+    eval $5=`echo $1 | sed -e "s#$RE#\4#"`
+}
+
+# if it's a -dev version check if a previous tag has been created (to avoid going from 2.7.0-dev to 2.7.1-dev)
+function is_valid_version {
+  local MAJOR=0 MINOR=0 PATCH=0 SPECIAL=""
+  local C_MAJOR=0 C_MINOR=0 C_PATCH=0 C_SPECIAL="" # these are used in the inner loops to compare
+
+  semverParseInto $NEW_VERSION MAJOR MINOR PATCH SPECIAL
+
+  found_parent=false
+
+  # if minor == 0, check that there was a release with MAJOR-1.X.X
+  if [[ "$MINOR" == 0 ]]; then
+    new_major=$(( $MAJOR - 1 ))
+    parent_version="$new_major.x.x"
+    while IFS= read -r existing_tag
+    do
+      semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL
+      if [[ "$new_major" == "$C_MAJOR" ]]; then
+        found_parent=true
+      fi
+    done <<< $existing_tags
+
+  # if patch == 0, check that there was a release with MAJOR.MINOR-1.X
+  elif [[ "$PATCH" == 0 ]]; then
+    new_minor=$(( $MINOR - 1 ))
+    parent_version="$MAJOR.$new_minor.x"
+    while IFS= read -r existing_tag
+    do
+      semverParseInto $existing_tag C_MAJOR C_MINOR C_PATCH C_SPECIAL
+      if [[ "$new_minor" == "$C_MINOR" ]]; then
+        found_parent=true
+      fi
+    done <<< $existing_tags
+
+  # if patch != 0 check that there was a release with MAJOR.MINOR.PATCH-1
+  elif [[ "$PATCH" != 0 ]]; then
+    new_patch=$(( $PATCH - 1 ))
+    parent_version="$MAJOR.$MINOR.$new_patch"
+    while IFS= read -r existing_tag
+    do
+      if [[ "$parent_version" == "$existing_tag" ]]
+      then
+        found_parent=true
+      fi
+    done <<< $existing_tags
+  fi
+
+  # if we are the beginning the is no parent, but that's fine
+  if [[ "$MAJOR" == 0 ]]; then
+    found_parent=true
+  fi
+
+  if [[ $found_parent == false ]]; then
+    echo "Invalid $NEW_VERSION version. Expected parent version $parent_version does not exist."
+    fail_validation=1
+  fi
 }
 
 # check if Dockerfiles have a released version as their parent
@@ -151,12 +221,15 @@
 git remote -v
 
 echo "Branches:"
-git branch -v
+branches=$(git branch -v)
+echo $branches
 
 echo "Existing git tags:"
-git tag -n
+existing_tags=$(git tag)
+echo $existing_tags
 
 read_version
+is_valid_version
 check_if_releaseversion
 
 # perform checks if a released version