-
Notifications
You must be signed in to change notification settings - Fork 1.6k
Add ruff version with long version display
#8034
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
13 commits
Select commit
Hold shift + click to select a range
9853a22
Add `ruff version` with long version display
zanieb 0b1b490
Drop `shadow-rs` in favor of a way cooler hand-coded implementation
zanieb fb690af
Move "version" to bottom of commands
zanieb 25c071f
Remove `ruff_version`
zanieb 5e0f016
Lint
zanieb 2d1a199
Oops; commit version file
zanieb 355aba9
Remove unncessary `build.rs` specifier
zanieb 1208df9
Use git references to trigger rebuilds
zanieb 53b0f5d
Improve comments
zanieb 944b3e4
Add snapshots for version formatting
zanieb f8655ea
Handle edge cases when describe fails; add serialization test; lint
zanieb 996ddf1
Add ruff prefix
zanieb d685384
Commit snapshot
zanieb 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 |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| use std::{fs, path::Path, process::Command}; | ||
|
|
||
| fn main() { | ||
| // The workspace root directory is not available without walking up the tree | ||
| // https://github.com/rust-lang/cargo/issues/3946 | ||
| let workspace_root = Path::new(&std::env::var("CARGO_MANIFEST_DIR").unwrap()) | ||
| .join("..") | ||
| .join(".."); | ||
|
|
||
| commit_info(&workspace_root); | ||
|
|
||
| #[allow(clippy::disallowed_methods)] | ||
| let target = std::env::var("TARGET").unwrap(); | ||
| println!("cargo:rustc-env=RUST_HOST_TARGET={target}"); | ||
| } | ||
|
|
||
| fn commit_info(workspace_root: &Path) { | ||
| // If not in a git repository, do not attempt to retrieve commit information | ||
| let git_dir = workspace_root.join(".git"); | ||
| if !git_dir.exists() { | ||
| return; | ||
| } | ||
|
|
||
| let git_head_path = git_dir.join("HEAD"); | ||
| println!( | ||
| "cargo:rerun-if-changed={}", | ||
| git_head_path.as_path().display() | ||
| ); | ||
|
|
||
| let git_head_contents = fs::read_to_string(git_head_path); | ||
| if let Ok(git_head_contents) = git_head_contents { | ||
| // The contents are either a commit or a reference in the following formats | ||
| // - "<commit>" when the head is detached | ||
| // - "ref <ref>" when working on a branch | ||
| // If a commit, checking if the HEAD file has changed is sufficient | ||
| // If a ref, we need to add the head file for that ref to rebuild on commit | ||
| let mut git_ref_parts = git_head_contents.split_whitespace(); | ||
| git_ref_parts.next(); | ||
| if let Some(git_ref) = git_ref_parts.next() { | ||
| let git_ref_path = git_dir.join(git_ref); | ||
| println!( | ||
| "cargo:rerun-if-changed={}", | ||
| git_ref_path.as_path().display() | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| let output = match Command::new("git") | ||
| .arg("log") | ||
| .arg("-1") | ||
| .arg("--date=short") | ||
| .arg("--abbrev=9") | ||
| .arg("--format=%H %h %cd %(describe)") | ||
| .output() | ||
| { | ||
| Ok(output) if output.status.success() => output, | ||
| _ => return, | ||
| }; | ||
| let stdout = String::from_utf8(output.stdout).unwrap(); | ||
| let mut parts = stdout.split_whitespace(); | ||
| let mut next = || parts.next().unwrap(); | ||
| println!("cargo:rustc-env=RUFF_COMMIT_HASH={}", next()); | ||
| println!("cargo:rustc-env=RUFF_COMMIT_SHORT_HASH={}", next()); | ||
| println!("cargo:rustc-env=RUFF_COMMIT_DATE={}", next()); | ||
|
|
||
| // Describe can fail for some commits | ||
| // https://git-scm.com/docs/pretty-formats#Documentation/pretty-formats.txt-emdescribeoptionsem | ||
| if let Some(describe) = parts.next() { | ||
| let mut describe_parts = describe.split('-'); | ||
| println!( | ||
| "cargo:rustc-env=RUFF_LAST_TAG={}", | ||
| describe_parts.next().unwrap() | ||
| ); | ||
| // If this is the tagged commit, this component will be missing | ||
| println!( | ||
| "cargo:rustc-env=RUFF_LAST_TAG_DISTANCE={}", | ||
| describe_parts.next().unwrap_or("0") | ||
| ); | ||
| } | ||
| } | ||
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
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 |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| use std::io::{self, BufWriter, Write}; | ||
|
|
||
| use anyhow::Result; | ||
|
|
||
| use crate::args::HelpFormat; | ||
|
|
||
| /// Display version information | ||
| pub(crate) fn version(output_format: HelpFormat) -> Result<()> { | ||
| let mut stdout = BufWriter::new(io::stdout().lock()); | ||
| let version_info = crate::version::version(); | ||
|
|
||
| match output_format { | ||
| HelpFormat::Text => { | ||
| writeln!(stdout, "ruff {}", &version_info)?; | ||
| } | ||
| HelpFormat::Json => { | ||
| serde_json::to_writer_pretty(stdout, &version_info)?; | ||
| } | ||
| }; | ||
| Ok(()) | ||
| } |
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
5 changes: 5 additions & 0 deletions
5
crates/ruff_cli/src/snapshots/ruff_cli__version__tests__version_formatting.snap
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| source: crates/ruff_cli/src/version.rs | ||
| expression: version | ||
| --- | ||
| 0.0.0 |
5 changes: 5 additions & 0 deletions
5
...ruff_cli/src/snapshots/ruff_cli__version__tests__version_formatting_with_commit_info.snap
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| source: crates/ruff_cli/src/version.rs | ||
| expression: version | ||
| --- | ||
| 0.0.0 (53b0f5d92 2023-10-19) |
5 changes: 5 additions & 0 deletions
5
...c/snapshots/ruff_cli__version__tests__version_formatting_with_commits_since_last_tag.snap
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 |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| --- | ||
| source: crates/ruff_cli/src/version.rs | ||
| expression: version | ||
| --- | ||
| 0.0.0+24 (53b0f5d92 2023-10-19) |
14 changes: 14 additions & 0 deletions
14
crates/ruff_cli/src/snapshots/ruff_cli__version__tests__version_serializable.snap
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| source: crates/ruff_cli/src/version.rs | ||
| expression: version | ||
| --- | ||
| { | ||
| "version": "0.0.0", | ||
| "commit_info": { | ||
| "short_commit_hash": "53b0f5d92", | ||
| "commit_hash": "53b0f5d924110e5b26fbf09f6fd3a03d67b475b7", | ||
| "commit_date": "2023-10-19", | ||
| "last_tag": "v0.0.1", | ||
| "commits_since_last_tag": 0 | ||
| } | ||
| } |
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 |
|---|---|---|
| @@ -0,0 +1,130 @@ | ||
| //! Code for representing Ruff's release version number. | ||
| use serde::Serialize; | ||
| use std::fmt; | ||
|
|
||
| /// Information about the git repository where Ruff was built from. | ||
| #[derive(Serialize)] | ||
| pub(crate) struct CommitInfo { | ||
| short_commit_hash: String, | ||
| commit_hash: String, | ||
| commit_date: String, | ||
| last_tag: Option<String>, | ||
| commits_since_last_tag: u32, | ||
| } | ||
|
|
||
| /// Ruff's version. | ||
| #[derive(Serialize)] | ||
| pub(crate) struct VersionInfo { | ||
| /// Ruff's version, such as "0.5.1" | ||
| version: String, | ||
| /// Information about the git commit we may have been built from. | ||
| /// | ||
| /// `None` if not built from a git repo or if retrieval failed. | ||
| commit_info: Option<CommitInfo>, | ||
| } | ||
|
|
||
| impl fmt::Display for VersionInfo { | ||
| /// Formatted version information: "<version>[+<commits>] (<commit> <date>)" | ||
| fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
| write!(f, "{}", self.version)?; | ||
|
|
||
| if let Some(ref ci) = self.commit_info { | ||
| if ci.commits_since_last_tag > 0 { | ||
| write!(f, "+{}", ci.commits_since_last_tag)?; | ||
| } | ||
| write!(f, " ({} {})", ci.short_commit_hash, ci.commit_date)?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
| } | ||
|
|
||
| /// Returns information about Ruff's version. | ||
| pub(crate) fn version() -> VersionInfo { | ||
| // Environment variables are only read at compile-time | ||
| macro_rules! option_env_str { | ||
| ($name:expr) => { | ||
| option_env!($name).map(|s| s.to_string()) | ||
| }; | ||
| } | ||
|
|
||
| // This version is pulled from Cargo.toml and set by Cargo | ||
| let version = option_env_str!("CARGO_PKG_VERSION").unwrap(); | ||
|
|
||
| // Commit info is pulled from git and set by `build.rs` | ||
| let commit_info = option_env_str!("RUFF_COMMIT_HASH").map(|commit_hash| CommitInfo { | ||
| short_commit_hash: option_env_str!("RUFF_COMMIT_SHORT_HASH").unwrap(), | ||
| commit_hash, | ||
| commit_date: option_env_str!("RUFF_COMMIT_DATE").unwrap(), | ||
| last_tag: option_env_str!("RUFF_LAST_TAG"), | ||
| commits_since_last_tag: option_env_str!("RUFF_LAST_TAG_DISTANCE") | ||
| .as_deref() | ||
| .map_or(0, |value| value.parse::<u32>().unwrap_or(0)), | ||
| }); | ||
|
|
||
| VersionInfo { | ||
| version, | ||
| commit_info, | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use insta::{assert_display_snapshot, assert_json_snapshot}; | ||
|
|
||
| use super::{CommitInfo, VersionInfo}; | ||
|
|
||
| #[test] | ||
| fn version_formatting() { | ||
| let version = VersionInfo { | ||
| version: "0.0.0".to_string(), | ||
| commit_info: None, | ||
| }; | ||
| assert_display_snapshot!(version); | ||
| } | ||
|
|
||
| #[test] | ||
| fn version_formatting_with_commit_info() { | ||
| let version = VersionInfo { | ||
| version: "0.0.0".to_string(), | ||
| commit_info: Some(CommitInfo { | ||
| short_commit_hash: "53b0f5d92".to_string(), | ||
| commit_hash: "53b0f5d924110e5b26fbf09f6fd3a03d67b475b7".to_string(), | ||
| last_tag: Some("v0.0.1".to_string()), | ||
| commit_date: "2023-10-19".to_string(), | ||
| commits_since_last_tag: 0, | ||
| }), | ||
| }; | ||
| assert_display_snapshot!(version); | ||
| } | ||
|
|
||
| #[test] | ||
| fn version_formatting_with_commits_since_last_tag() { | ||
| let version = VersionInfo { | ||
| version: "0.0.0".to_string(), | ||
| commit_info: Some(CommitInfo { | ||
| short_commit_hash: "53b0f5d92".to_string(), | ||
| commit_hash: "53b0f5d924110e5b26fbf09f6fd3a03d67b475b7".to_string(), | ||
| last_tag: Some("v0.0.1".to_string()), | ||
| commit_date: "2023-10-19".to_string(), | ||
| commits_since_last_tag: 24, | ||
| }), | ||
| }; | ||
| assert_display_snapshot!(version); | ||
| } | ||
|
|
||
| #[test] | ||
| fn version_serializable() { | ||
| let version = VersionInfo { | ||
| version: "0.0.0".to_string(), | ||
| commit_info: Some(CommitInfo { | ||
| short_commit_hash: "53b0f5d92".to_string(), | ||
| commit_hash: "53b0f5d924110e5b26fbf09f6fd3a03d67b475b7".to_string(), | ||
| last_tag: Some("v0.0.1".to_string()), | ||
| commit_date: "2023-10-19".to_string(), | ||
| commits_since_last_tag: 0, | ||
| }), | ||
| }; | ||
| assert_json_snapshot!(version); | ||
| } | ||
| } |
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
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.