Skip to content
This repository has been archived by the owner on Oct 5, 2018. It is now read-only.

Commit

Permalink
Don't fail version inference if no commits
Browse files Browse the repository at this point in the history
Fix #112 by falling back to 0.0.0 if no commits exist.
  • Loading branch information
ajoberstar committed Aug 23, 2015
1 parent 7d87e24 commit 1923b80
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 1923b80

Please # to comment.