Derive a version string from `git describe --tags`. Implements the "guess-next-dev" convention used by the release pipeline (`setuptools-scm`): when there are commits past the last tag, the patch version is bumped and `-dev. +g ` is appended. Examples: on tag v0.0.3 → "0.0.3" 3 commits past v0.0.3 → "0.0.4-dev.3+g2bf9969" Returns `None` when git is unavailable or the repo has no
()
| 84 | /// |
| 85 | /// Returns `None` when git is unavailable or the repo has no matching tags. |
| 86 | fn git_version() -> Option<String> { |
| 87 | // Match numeric release tags only (e.g. `v0.0.29`). The bare glob `v*` |
| 88 | // also matches non-release tags like `vm-dev` or `vm-prod`; when one of |
| 89 | // those lands on the same commit as a release tag, `git describe` picks |
| 90 | // it and the resulting version string collapses to `m-dev` after the |
| 91 | // leading `v` is stripped below. Requiring a digit after `v` excludes |
| 92 | // those development tags without losing any release tag. |
| 93 | let output = std::process::Command::new("git") |
| 94 | .args(["describe", "--tags", "--long", "--match", "v[0-9]*"]) |
| 95 | .output() |
| 96 | .ok()?; |
| 97 | |
| 98 | if !output.status.success() { |
| 99 | return None; |
| 100 | } |
| 101 | |
| 102 | let desc = String::from_utf8(output.stdout).ok()?; |
| 103 | let desc = desc.trim(); |
| 104 | let desc = desc.strip_prefix('v').unwrap_or(desc); |
| 105 | |
| 106 | // `git describe --long` format: <tag>-<N>-g<sha> |
| 107 | // Split from the right to handle tags that contain hyphens. |
| 108 | let (rest, sha) = desc.rsplit_once('-')?; |
| 109 | let (tag, commits_str) = rest.rsplit_once('-')?; |
| 110 | let commits: u32 = commits_str.parse().ok()?; |
| 111 | |
| 112 | if commits == 0 { |
| 113 | // Exactly on a tag — use the tag version as-is. |
| 114 | return Some(tag.to_string()); |
| 115 | } |
| 116 | |
| 117 | // Bump patch version (guess-next-dev scheme). |
| 118 | let mut parts = tag.splitn(3, '.'); |
| 119 | let major = parts.next()?; |
| 120 | let minor = parts.next()?; |
| 121 | let patch: u32 = parts.next()?.parse().ok()?; |
| 122 | |
| 123 | Some(format!("{major}.{minor}.{}-dev.{commits}+{sha}", patch + 1)) |
| 124 | } |