-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Add ruff version
with long version display
#8034
Merged
Merged
Changes from 8 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 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,79 @@ | ||
use std::{fs, path::Path, path::PathBuf, 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: PathBuf) { | ||
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>"" | ||
// "ref <ref>"" | ||
// If a commit, checking if the HEAD file has changed is sufficient | ||
// If a ref we want 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") | ||
zanieb marked this conversation as resolved.
Show resolved
Hide resolved
|
||
.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 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 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 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, "{}", &version_info)?; | ||
} | ||
HelpFormat::Json => { | ||
serde_json::to_writer_pretty(stdout, &version_info)?; | ||
} | ||
}; | ||
Ok(()) | ||
} |
This file contains 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 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,69 @@ | ||
//! 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: 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 { | ||
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").unwrap(), | ||
commits_since_last_tag: option_env_str!("RUFF_LAST_TAG_DISTANCE") | ||
.unwrap() | ||
.parse::<u32>() | ||
.unwrap(), | ||
}); | ||
|
||
VersionInfo { | ||
version, | ||
commit_info, | ||
} | ||
} |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay this is a little bit much but i tested this out and basically without this, if you change any file in the
ruff_cli
crate e.g. add a filefoo
to the crate root then a rebuild is forced — this is entirely unnecessary so, here, if a git directory is present we will use commit information from it to determine if rebuilding should occur.