Repository navigation
feat(fleet): derived per-app versioning from tags (#8566) #8630
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,30 +1,192 @@ | ||
| //! Bake the source revision into the binary for `fleet version`. | ||
| //! Bake the source revision and the derived version into the | ||
| //! binary for `fleet version`. | ||
| //! | ||
| //! Resolution: `$FLEET_SOURCE_REV` (the nix build passes the flake's | ||
| //! rev) → `git rev-parse` in the repo root (developer builds) → | ||
| //! "unknown". The nix sandbox has no `.git`, so without the env var a | ||
| //! packaged build honestly reports "unknown" rather than inventing one. | ||
| //! Source rev resolution: `$FLEET_SOURCE_REV` (the nix build passes | ||
| //! the flake's rev) → `git rev-parse` in the repo root (developer | ||
| //! builds) → "unknown". The nix sandbox has no `.git`, so without | ||
| //! the env var a packaged build honestly reports "unknown" rather | ||
| //! than inventing one. | ||
| //! | ||
| //! Derived version resolution (#8566; policy #5790): the version is | ||
| //! derived, never hand-maintained — | ||
| //! `$FLEET_DERIVED_VERSION` (release tooling / packaged builds, | ||
| //! computed where the git history exists, exactly the way | ||
| //! `$FLEET_SOURCE_REV` already flows) → `git describe --tags | ||
| //! --match '<app>/v*'` transformed by the shared derivation in | ||
| //! `src/derived_version.rs` → the pre-first-tag fallback | ||
| //! `0.1.0-dev` for a build with no history at all. See the | ||
| //! "Versioning" section of pkgs/fleet/README.md for the tag and | ||
| //! release procedure this implements. | ||
|
|
||
| use std::process::Command; | ||
|
|
||
| #[path = "src/derived_version.rs"] | ||
| mod derived_version; | ||
|
|
||
| use derived_version::{ReleaseVersion, branch_class, derive, parse_app_tag, parse_describe}; | ||
|
|
||
| /// The app's short name — its tag prefix and deploy name (#5790: | ||
| /// one short-name policy for all apps). | ||
| const APP_NAME: &str = "fleet"; | ||
|
|
||
| fn git(root: &std::path::Path, args: &[&str]) -> Option<String> { | ||
| Command::new("git") | ||
| .args(args) | ||
| .current_dir(root) | ||
| .output() | ||
| .ok() | ||
| .filter(|o| o.status.success()) | ||
| .and_then(|o| String::from_utf8(o.stdout).ok()) | ||
| .map(|s| s.trim().to_string()) | ||
| .filter(|s| !s.is_empty()) | ||
| } | ||
|
|
||
| /// Fallback tag selection (PR #8630 Codex finding): `git describe` | ||
| /// considers every tag matching `fleet/v*`, including malformed | ||
| /// ones (`fleet/v0.4.3-rc.1`, `fleet/v01.2.3`) that parse_describe | ||
| /// rightly rejects — and a rejected NEAREST tag must not hide an | ||
| /// older valid release tag behind it. Scan the reachable tags, | ||
| /// keep only the ones that validate as `<app>/vX.Y.Z`, and pick | ||
| /// the nearest (smallest commit distance; ties go to the highest | ||
| /// version). | ||
| fn from_validated_tags(root: &std::path::Path, branch: Option<&str>) -> Option<String> { | ||
| let tags = git( | ||
| root, | ||
| &[ | ||
| "tag", | ||
| "--list", | ||
| &format!("{APP_NAME}/v*"), | ||
| "--merged", | ||
| "HEAD", | ||
| ], | ||
| )?; | ||
| let hash = git(root, &["rev-parse", "--short=7", "HEAD"]); | ||
| let mut best: Option<(u64, ReleaseVersion)> = None; | ||
| for line in tags.lines() { | ||
| let tag = line.trim(); | ||
| let Some(version) = parse_app_tag(APP_NAME, tag) else { | ||
| continue; | ||
| }; | ||
| let Some(distance) = git(root, &["rev-list", "--count", &format!("{tag}..HEAD")]) | ||
| .and_then(|s| s.parse::<u64>().ok()) | ||
| else { | ||
| continue; | ||
| }; | ||
| let wins = match best { | ||
| None => true, | ||
| Some((bd, bv)) => { | ||
| distance < bd | ||
| || (distance == bd | ||
| && (version.major, version.minor, version.patch) | ||
| > (bv.major, bv.minor, bv.patch)) | ||
| } | ||
| }; | ||
| if wins { | ||
| best = Some((distance, version)); | ||
| } | ||
| } | ||
| let (distance, version) = best?; | ||
| let class = branch_class(branch, distance == 0); | ||
| Some(derive(Some(version), distance, hash.as_deref(), &class)) | ||
| } | ||
|
|
||
| fn derived_version(root: &std::path::Path) -> String { | ||
| if let Some(v) = std::env::var("FLEET_DERIVED_VERSION") | ||
| .ok() | ||
| .filter(|s| !s.is_empty()) | ||
| { | ||
| return v; | ||
| } | ||
| let branch = git(root, &["branch", "--show-current"]); | ||
| if let Some(described) = git( | ||
| root, | ||
| &[ | ||
| "describe", | ||
| "--tags", | ||
| "--long", | ||
| "--match", | ||
| &format!("{APP_NAME}/v*"), | ||
| ], | ||
| ) | ||
| .and_then(|out| parse_describe(APP_NAME, &out)) | ||
|
dezren39 marked this conversation as resolved.
Resolved
|
||
| { | ||
| let class = branch_class(branch.as_deref(), described.distance == 0); | ||
| return derive( | ||
| Some(described.tag), | ||
| described.distance, | ||
| described.hash.as_deref(), | ||
| &class, | ||
| ); | ||
| } | ||
| if let Some(v) = from_validated_tags(root, branch.as_deref()) { | ||
| return v; | ||
| } | ||
| // No `<app>/v*` tag yet: the 0.1.0-dev fallback family, with the | ||
| // repo-wide commit count as the (monotonic) distance and the | ||
| // HEAD hash as the uniqueness suffix, when git can prove them. | ||
| let hash = git(root, &["rev-parse", "--short=7", "HEAD"]); | ||
| let distance = git(root, &["rev-list", "--count", "HEAD"]) | ||
| .and_then(|s| s.parse::<u64>().ok()) | ||
| .unwrap_or(0); | ||
| let class = branch_class(branch.as_deref(), false); | ||
| derive(None, distance, hash.as_deref(), &class) | ||
| } | ||
|
|
||
| /// Register the git state the bake reads as build-script inputs | ||
| /// (PR #8630 Codex finding): without this, cutting a tag on an | ||
| /// already-built tree changes no env var and no source file, and | ||
| /// cargo keeps the stale baked version. Tag refs live in the | ||
| /// common git dir (shared across worktrees); HEAD is per-git-dir. | ||
| fn rerun_on_git_refs(root: &std::path::Path) { | ||
| let resolve = |args: &[&str]| -> Option<std::path::PathBuf> { | ||
| let dir = git(root, args)?; | ||
| let base = if std::path::Path::new(&dir).is_absolute() { | ||
| std::path::PathBuf::from(dir) | ||
| } else { | ||
| root.join(dir) | ||
| }; | ||
| Some(base) | ||
| }; | ||
| let mut watched: Vec<std::path::PathBuf> = Vec::new(); | ||
| if let Some(common) = resolve(&["rev-parse", "--git-common-dir"]) { | ||
| watched.push(common.join("refs/tags")); | ||
| watched.push(common.join("refs/heads")); | ||
| watched.push(common.join("packed-refs")); | ||
| // The current branch's own ref (PR #8630 Codex round 2): | ||
| // on an attached HEAD, an ordinary commit updates the | ||
| // loose branch ref under the COMMON dir — HEAD itself is | ||
| // just the symref naming it and never changes content — | ||
| // so watch the resolved ref as well as its directory | ||
| // (the directory watch covers its first loose creation | ||
| // when it previously lived only in packed-refs). | ||
| if let Some(branch_ref) = git(root, &["symbolic-ref", "-q", "HEAD"]) { | ||
| watched.push(common.join(branch_ref)); | ||
| } | ||
| } | ||
| if let Some(gitdir) = resolve(&["rev-parse", "--git-dir"]) { | ||
| watched.push(gitdir.join("HEAD")); | ||
| } | ||
| for path in watched { | ||
| if path.exists() { | ||
| println!("cargo:rerun-if-changed={}", path.display()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn main() { | ||
| println!("cargo:rerun-if-env-changed=FLEET_SOURCE_REV"); | ||
| println!("cargo:rerun-if-env-changed=FLEET_DERIVED_VERSION"); | ||
|
dezren39 marked this conversation as resolved.
Resolved
|
||
| let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default(); | ||
| let root = std::path::Path::new(&manifest).join("../../.."); | ||
| rerun_on_git_refs(&root); | ||
| let rev = std::env::var("FLEET_SOURCE_REV") | ||
| .ok() | ||
| .filter(|s| !s.is_empty()) | ||
| .unwrap_or_else(|| { | ||
| let manifest = std::env::var("CARGO_MANIFEST_DIR").unwrap_or_default(); | ||
| let root = std::path::Path::new(&manifest).join("../../.."); | ||
| Command::new("git") | ||
| .args(["rev-parse", "--short=12", "HEAD"]) | ||
| .current_dir(root) | ||
| .output() | ||
| .ok() | ||
| .filter(|o| o.status.success()) | ||
| .and_then(|o| String::from_utf8(o.stdout).ok()) | ||
| .map(|s| s.trim().to_string()) | ||
| .filter(|s| !s.is_empty()) | ||
| git(&root, &["rev-parse", "--short=12", "HEAD"]) | ||
| .unwrap_or_else(|| "unknown".to_string()) | ||
| }); | ||
| println!("cargo:rustc-env=FLEET_SOURCE_REV={rev}"); | ||
| let derived = derived_version(&root); | ||
| println!("cargo:rustc-env=FLEET_DERIVED_VERSION={derived}"); | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.