Skip to content

Commit

Permalink
Merge pull request #3137 from matthiaskrgr/clippy_git_version
Browse files Browse the repository at this point in the history
print git commit hash and commit date in version output
  • Loading branch information
phansch authored Sep 6, 2018
2 parents 5fca614 + fa11aad commit cafef7b
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 6 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ Cargo.lock
/clippy_lints/target
/clippy_workspace_tests/target
/clippy_dev/target
/rustc_tools_util/target

# Generated by dogfood
/target_recur/
Expand Down
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ clippy_lints = { version = "0.0.212", path = "clippy_lints" }
# end automatic update
regex = "1"
semver = "0.9"
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}

[dev-dependencies]
clippy_dev = { version = "0.0.1", path = "clippy_dev" }
Expand All @@ -60,5 +61,8 @@ derive-new = "0.5"
# for more information.
rustc-workspace-hack = "1.0.0"

[build-dependencies]
rustc_tools_util = { version = "0.1.0", path = "rustc_tools_util"}

[features]
debugging = []
13 changes: 10 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::env;

fn main() {
// Forward the profile to the main compilation
println!("cargo:rustc-env=PROFILE={}", env::var("PROFILE").unwrap());
println!("cargo:rustc-env=PROFILE={}", std::env::var("PROFILE").unwrap());
// Don't rebuild even if nothing changed
println!("cargo:rerun-if-changed=build.rs");
// forward git repo hashes we build at
println!(
"cargo:rustc-env=GIT_HASH={}",
rustc_tools_util::get_commit_hash().unwrap_or_default()
);
println!(
"cargo:rustc-env=COMMIT_DATE={}",
rustc_tools_util::get_commit_date().unwrap_or_default()
);
}
2 changes: 2 additions & 0 deletions ci/base-tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ cd clippy_workspace_tests/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../..
cd clippy_workspace_tests/subcrate/src && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ../../..
cd clippy_dev && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..
cd rustc_tools_util/ && PATH=$PATH:~/rust/cargo/bin cargo clippy -- -D clippy::all && cd ..

# test --manifest-path
PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=clippy_workspace_tests/Cargo.toml -- -D clippy::all
cd clippy_workspace_tests/subcrate && PATH=$PATH:~/rust/cargo/bin cargo clippy --manifest-path=../Cargo.toml -- -D clippy::all && cd ../..
Expand Down
8 changes: 8 additions & 0 deletions rustc_tools_util/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
cargo-features = ["edition"]

[package]
name = "rustc_tools_util"
version = "0.1.0"
authors = ["Matthias Krüger <[email protected]>"]
edition = "2018"
[dependencies]
82 changes: 82 additions & 0 deletions rustc_tools_util/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#![feature(tool_lints)]

use std::env;

#[macro_export]
macro_rules! get_version_info {
() => {{
let major = env!("CARGO_PKG_VERSION_MAJOR").parse::<u8>().unwrap();
let minor = env!("CARGO_PKG_VERSION_MINOR").parse::<u8>().unwrap();
let patch = env!("CARGO_PKG_VERSION_PATCH").parse::<u16>().unwrap();

let host_compiler = $crate::get_channel();
let commit_hash = option_env!("GIT_HASH").map(|s| s.to_string());
let commit_date = option_env!("COMMIT_DATE").map(|s| s.to_string());

VersionInfo {
major,
minor,
patch,
host_compiler,
commit_hash,
commit_date,
}
}};
}

// some code taken and adapted from RLS and cargo
pub struct VersionInfo {
pub major: u8,
pub minor: u8,
pub patch: u16,
pub host_compiler: Option<String>,
pub commit_hash: Option<String>,
pub commit_date: Option<String>,
}

impl std::fmt::Display for VersionInfo {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self.commit_hash {
Some(_) => {
write!(
f,
"clippy {}.{}.{} ({} {})",
self.major,
self.minor,
self.patch,
self.commit_hash.clone().unwrap_or_default().trim(),
self.commit_date.clone().unwrap_or_default().trim(),
)?;
},
None => {
write!(f, "clippy {}.{}.{}", self.major, self.minor, self.patch)?;
},
};
Ok(())
}
}

pub fn get_channel() -> Option<String> {
if let Ok(channel) = env::var("CFG_RELEASE_CHANNEL") {
Some(channel)
} else {
// we could ask ${RUSTC} -Vv and do some parsing and find out
Some(String::from("nightly"))
}
}

pub fn get_commit_hash() -> Option<String> {
std::process::Command::new("git")
.args(&["rev-parse", "--short", "HEAD"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
}

pub fn get_commit_date() -> Option<String> {
std::process::Command::new("git")
.args(&["log", "-1", "--date=short", "--pretty=format:%cd"])
.output()
.ok()
.and_then(|r| String::from_utf8(r.stdout).ok())
}
9 changes: 6 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
#![feature(tool_lints)]
#![allow(unknown_lints, clippy::missing_docs_in_private_items)]

use rustc_tools_util::*;

const CARGO_CLIPPY_HELP: &str = r#"Checks a package to catch common mistakes and improve your Rust code.
Usage:
Expand Down Expand Up @@ -36,7 +38,8 @@ fn show_help() {

#[allow(clippy::print_stdout)]
fn show_version() {
println!(env!("CARGO_PKG_VERSION"));
let version_info = rustc_tools_util::get_version_info!();
println!("{}", version_info);
}

pub fn main() {
Expand All @@ -45,6 +48,7 @@ pub fn main() {
show_help();
return;
}

if std::env::args().any(|a| a == "--version" || a == "-V") {
show_version();
return;
Expand Down Expand Up @@ -94,8 +98,7 @@ where
.into_os_string()
},
)
})
.map(|p| ("CARGO_TARGET_DIR", p));
}).map(|p| ("CARGO_TARGET_DIR", p));

let exit_status = std::process::Command::new("cargo")
.args(&args)
Expand Down

0 comments on commit cafef7b

Please sign in to comment.