Repository: zyedidia/micro
File: tools/build-version.go (Go)

Code before:
			log.Printf("semver.NewPRVersion(%s): %v", tagParts[1], err)
		} else if len(tagParts) == 4 {
			ahead, err := semver.NewPRVersion(tagParts[2])
			if err == nil {
				return tagParts[0] + "-" + tagParts[1], &ahead
			}
			log.Printf("semver.NewPRVersion(%s): %v", tagParts[2], err)
		}

		return string(tag), nil
	}
}

func main() {
	// Find the last vX.X.X Tag and get how many builds we are ahead of it.
	versionStr, ahead := getTag("--match", "v*")
	version, err := semver.ParseTolerant(versionStr)
	if err != nil {
		// no version tag found so just return what ever we can find.
		log.Printf("semver.ParseTolerant(%s): %v", versionStr, err)
		fmt.Println("0.0.0-unknown")
		return
	}
	// Get the tag of the current revision.
	tag, _ := getTag("--exact-match")
	if tag == versionStr || ahead == nil {
		// Seems that we are going to build a release.
		// So the version number should already be correct.
		fmt.Println(version.String())
		return
	}

	// If we don't have any tag assume "dev"
	if tag == "" || strings.HasPrefix(tag, "nightly") {
		tag = "dev"
	}
	// Get the most likely next version:
	if !strings.Contains(version.String(), "rc") {
		version.Patch = version.Patch + 1
	}

	if pr, err := semver.NewPRVersion(tag); err == nil {
		// append the tag as pre-release name
		version.Pre = append(version.Pre, pr)
	} else {
		log.Printf("semver.NewPRVersion(%s): %v", tag, err)
	}

	// append how many commits we are ahead of the last release
	version.Pre = append(version.Pre, *ahead)


Review comment (suggestion):
~~But doesn't that mean we've no `versionStr` right now?~~
I know we're coming from #3514 resp. https://github.com/termux/termux-packages/issues/21286.

Edit:
Got it...it hits when a release will be tagged later as `nightly` and this tag is checked out instead of e.g. `v2.0.14`.

Edit2:
Should work. I tried it with a local `v2.0.15` along to the actual `nightly`.

Code after:
			log.Printf("semver.NewPRVersion(%s): %v", tagParts[1], err)
		} else if len(tagParts) == 4 {
			ahead, err := semver.NewPRVersion(tagParts[2])
			if err == nil {
				return tagParts[0] + "-" + tagParts[1], &ahead
			}
			log.Printf("semver.NewPRVersion(%s): %v", tagParts[2], err)
		}

		return string(tag), nil
	}
}

func main() {
	// Find the last vX.X.X Tag and get how many builds we are ahead of it.
	versionStr, ahead := getTag("--match", "v*")
	version, err := semver.ParseTolerant(versionStr)
	if err != nil {
		// no version tag found so just return what ever we can find.
		log.Printf("semver.ParseTolerant(%s): %v", versionStr, err)
		fmt.Println("0.0.0-unknown")
		return
	}
	if ahead == nil {
		// Seems that we are going to build a release.
		// So the version number should already be correct.
		fmt.Println(version.String())
		return
	}

	// Get the tag of the current revision.
	tag, _ := getTag("--exact-match")

	// If we don't have any tag assume "dev"
	if tag == "" || strings.HasPrefix(tag, "nightly") {
		tag = "dev"
	}
	// Get the most likely next version:
	if !strings.Contains(version.String(), "rc") {
		version.Patch = version.Patch + 1
	}

	if pr, err := semver.NewPRVersion(tag); err == nil {
		// append the tag as pre-release name
		version.Pre = append(version.Pre, pr)
	} else {
		log.Printf("semver.NewPRVersion(%s): %v", tag, err)
	}

	// append how many commits we are ahead of the last release
	version.Pre = append(version.Pre, *ahead)