From 1923b808018bf379426ca81a51f9ef845fb6392d Mon Sep 17 00:00:00 2001 From: Andrew Oberstar Date: Sun, 23 Aug 2015 15:34:19 -0500 Subject: [PATCH] Don't fail version inference if no commits Fix #112 by falling back to 0.0.0 if no commits exist. --- .../semver/NearestVersionLocator.groovy | 33 ++++++++++++++++--- .../semver/NearestVersionLocatorSpec.groovy | 11 +++++++ 2 files changed, 39 insertions(+), 5 deletions(-) diff --git a/src/main/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocator.groovy b/src/main/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocator.groovy index c8c2a8a..9565fcc 100644 --- a/src/main/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocator.groovy +++ b/src/main/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocator.groovy @@ -19,6 +19,7 @@ import com.github.zafarkhaja.semver.Version import org.ajoberstar.grgit.Commit import org.ajoberstar.grgit.Grgit +import org.ajoberstar.grgit.exception.GrgitException import org.slf4j.Logger import org.slf4j.LoggerFactory @@ -73,7 +74,14 @@ class NearestVersionLocator { */ NearestVersion locate(Grgit grgit) { logger.debug('Locate beginning on branch: {}', grgit.branch.current.fullName) - Commit head = grgit.head() + + Commit head + try { + head = grgit.head() + } catch (GrgitException e) { + head = null + } + List versionTags = grgit.tag.list().inject([]) { list, tag -> Version version = TagUtil.parseAsVersion(tag) logger.debug('Tag {} ({}) parsed as {} version.', tag.name, tag.commit.abbreviatedId, version) @@ -113,10 +121,25 @@ class NearestVersionLocator { a.distance <=> b.distance ?: (a.version <=> b.version) * -1 } - Version anyVersion = any ? any.version : Version.valueOf('0.0.0') - Version normalVersion = normal ? normal.version : Version.valueOf('0.0.0') - int distanceFromAny = any ? any.distance : grgit.log(includes: [head.id]).size() - int distanceFromNormal = normal ? normal.distance : grgit.log(includes: [head.id]).size() + Version anyVersion + int distanceFromAny + if (any) { + anyVersion = any.version + distanceFromAny = any.distance + } else { + anyVersion = Version.valueOf('0.0.0') + distanceFromAny = head ? grgit.log(includes: [head.id]).size() : 0 + } + + Version normalVersion + int distanceFromNormal + if (normal) { + normalVersion = normal.version + distanceFromNormal = normal.distance + } else { + normalVersion = Version.valueOf('0.0.0') + distanceFromNormal = head ? grgit.log(includes: [head.id]).size() : 0 + } return new NearestVersion(anyVersion, normalVersion, distanceFromAny, distanceFromNormal) } diff --git a/src/test/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocatorSpec.groovy b/src/test/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocatorSpec.groovy index 5148b3d..d3b25b0 100644 --- a/src/test/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocatorSpec.groovy +++ b/src/test/groovy/org/ajoberstar/gradle/git/release/semver/NearestVersionLocatorSpec.groovy @@ -116,6 +116,17 @@ class NearestVersionLocatorSpec extends Specification { 'unreachable' | '0.0.0' | '0.0.0' | 2 } + def 'locator returns 0.0.0 when no commits'() { + given: + repoDir = Files.createTempDirectory('empty-repo').toFile() + grgit = Grgit.init(dir: repoDir) + expect: + def nearest = new NearestVersionLocator().locate(grgit) + nearest.any == Version.valueOf('0.0.0') + nearest.normal == Version.valueOf('0.0.0') + nearest.distanceFromNormal == 0 + } + private void commit() { byte[] bytes = new byte[128] random.nextBytes(bytes)