func()

in common/version.go [89:126]


func (v Version) compare(v2 Version) int {
	// short-circuit if the two version have the exact same raw string, no need to compare
	if v.original == v2.original {
		return 0
	}

	// TODO: Make sure preview version is smaller than GA version

	// compare the major/minor/patch version
	// if v has a bigger number, it is newer
	for i := 0; i < 3; i++ {
		if v.segments[i] > v2.segments[i] {
			return 1
		} else if v.segments[i] < v2.segments[i] {
			return -1
		}
	}

	// if both or neither versions are previews, then they are equal
	// usually this shouldn't happen since we already checked whether the two versions have equal raw string
	// however, it is entirely possible that we have new kinds of pre-release versions that this code is not parsing correctly
	// in this case we consider both pre-release version equal anyways
	if v.preview && v2.preview {
		if v.segments[3] > v2.segments[3] {
			return 1
		} else if v.segments[3] < v2.segments[3] {
			return -1
		} else {
			return 0
		}
	} else if !v.preview && !v2.preview {
		return 0
	} else if v.preview && !v2.preview {
		return -1
	}

	return 1
}