diff --git a/build.rs b/build.rs index 9f86919..96312bd 100644 --- a/build.rs +++ b/build.rs @@ -20,6 +20,7 @@ fn main() -> Result<(), Error> { println!("cargo:rerun-if-changed=src/cli.rs"); println!("cargo:rerun-if-changed=build.rs"); emit_version_info(); + emit_core_rev(); let Some(out_dir) = env::var_os("OUT_DIR").map(PathBuf::from) else { return Ok(()); @@ -78,6 +79,58 @@ fn emit_version_info() { println!("cargo:rustc-env=COMLINE_BUILD_DATE={build_date}"); } +/// Sets `COMLINE_CORE_REV` to the `comline-core` commit this build links, for +/// `--version`'s `core-rev:` line (`main.rs`). It is the `#` of the +/// `comline-core` git source in `Cargo.lock`; a build that doesn't see a +/// lock (`cargo install` without `--locked`) reads the full `rev` of +/// `Cargo.toml`'s `comline-core` line instead, and a build with neither says +/// `unknown`. +fn emit_core_rev() { + println!("cargo:rerun-if-changed=Cargo.toml"); + println!("cargo:rerun-if-changed=Cargo.lock"); + + let rev = fs::read_to_string("Cargo.lock") + .ok() + .and_then(|lock| core_rev_from_lock(&lock)) + .or_else(|| { + let manifest = fs::read_to_string("Cargo.toml").ok()?; + core_rev_from_manifest(&manifest) + }) + .unwrap_or_else(|| "unknown".to_string()); + println!("cargo:rustc-env=COMLINE_CORE_REV={rev}"); +} + +fn is_sha(s: &str) -> bool { + s.len() == 40 && s.bytes().all(|b| b.is_ascii_hexdigit()) +} + +/// `source = "git+https://github.com/ComlineProject/core?rev=...#"` of +/// the `comline-core` package. +fn core_rev_from_lock(lock: &str) -> Option { + let mut package = ""; + for line in lock.lines() { + if let Some(name) = line.strip_prefix("name = \"") { + package = name.trim_end_matches('"'); + } else if package == "comline-core" { + if let Some(source) = line.strip_prefix("source = \"git+") { + let sha = source.rsplit('#').next()?.trim_end_matches('"'); + return is_sha(sha).then(|| sha.to_ascii_lowercase()); + } + } + } + None +} + +/// `comline-core = { git = "...", rev = "", ... }`. +fn core_rev_from_manifest(manifest: &str) -> Option { + let line = manifest + .lines() + .find(|l| l.trim_start().starts_with("comline-core"))?; + let rest = line.split("rev = \"").nth(1)?; + let sha = rest.split('"').next()?; + is_sha(sha).then(|| sha.to_ascii_lowercase()) +} + /// Run a command, returning trimmed stdout on success - `None` on any /// failure (missing binary, non-zero exit, not a git repo, ...). fn run(args: &[&str]) -> Option { diff --git a/src/main.rs b/src/main.rs index 97a98e8..b830573 100644 --- a/src/main.rs +++ b/src/main.rs @@ -47,7 +47,9 @@ fn main() -> ExitCode { } /// Parse CLI args with an enriched `--version` (`-V` stays the bare -/// `CARGO_PKG_VERSION` `clap`'s derive attribute already gives `Cli`). +/// `CARGO_PKG_VERSION` `clap`'s derive attribute already gives `Cli`). Its +/// `core-rev:` line is the `comline-core` commit the CLI links, 40 hex digits, +/// which an editor compares with the one its language server reports. /// Built as a `Command`, not the `#[command(long_version = ...)]` derive /// attribute, because `build.rs` also compiles `cli.rs` (via `#[path]`, for /// man-page/completion generation) *before* it has emitted these env vars @@ -58,12 +60,13 @@ fn main() -> ExitCode { /// (`emit_version_info`) came up empty, rather than failing anything. fn parse_cli() -> Cli { let long_version = format!( - "{}\ncommit-hash: {}\ncommit-branch: {}\ncommit-count: {}\nbuild-date: {}", + "{}\ncommit-hash: {}\ncommit-branch: {}\ncommit-count: {}\nbuild-date: {}\ncore-rev: {}", env!("CARGO_PKG_VERSION"), option_env!("COMLINE_GIT_HASH").unwrap_or("unknown"), option_env!("COMLINE_GIT_BRANCH").unwrap_or("unknown"), option_env!("COMLINE_COMMIT_COUNT").unwrap_or("unknown"), option_env!("COMLINE_BUILD_DATE").unwrap_or("unknown"), + option_env!("COMLINE_CORE_REV").unwrap_or("unknown"), ); // `Command::long_version` needs a `&'static str`, not an owned // `String` - leaking it is fine here, it lives for the process anyway. diff --git a/tests/cli/global.rs b/tests/cli/global.rs index d0c67e5..1446c6f 100644 --- a/tests/cli/global.rs +++ b/tests/cli/global.rs @@ -28,6 +28,30 @@ fn plain_output_has_no_color_or_symbols() { assert!(stderr.contains("Project built")); } +/// `--version` ends with the `comline-core` commit the CLI links, as a +/// `core-rev: <40 hex>` line an editor reads, and it is the one `Cargo.toml` +/// pins. +#[test] +fn version_reports_the_core_rev_it_links() { + let manifest = include_str!("../../Cargo.toml"); + let pinned = manifest + .lines() + .find(|l| l.trim_start().starts_with("comline-core")) + .and_then(|l| l.split("rev = \"").nth(1)) + .and_then(|rest| rest.split('"').next()) + .expect("a pinned comline-core rev"); + + let out = comline_cmd().arg("--version").assert().success(); + let stdout = String::from_utf8_lossy(&out.get_output().stdout).into_owned(); + let reported = stdout + .lines() + .find_map(|l| l.strip_prefix("core-rev: ")) + .unwrap_or_else(|| panic!("a `core-rev:` line in {stdout:?}")); + + assert_eq!(reported.len(), 40, "{reported}"); + assert!(reported.starts_with(pinned), "{reported} vs {pinned}"); +} + #[test] fn completions_are_written_to_stdout() { comline_cmd()