diff --git a/hipcheck/build.rs b/hipcheck/build.rs index a140c7c1..39eceaa8 100644 --- a/hipcheck/build.rs +++ b/hipcheck/build.rs @@ -1,81 +1,6 @@ // SPDX-License-Identifier: Apache-2.0 -use anyhow::anyhow; -use anyhow::Context as _; -use anyhow::Result; -use std::convert::AsRef; -use std::ffi::OsStr; -use std::iter::IntoIterator; -use std::ops::Not as _; -use std::path::Path; -use std::process::Command; -use which::which; - -fn main() -> Result<()> { - let repo_dir = env!("CARGO_MANIFEST_DIR", "can't find Cargo manifest directory"); - let head = get_head_commit(repo_dir).unwrap_or_default(); - +fn main() -> anyhow::Result<()> { tonic_build::compile_protos("proto/hipcheck.proto")?; - - println!("cargo:rustc-env=HC_HEAD_COMMIT={}", head); Ok(()) } - -fn get_head_commit>(path: P) -> Result { - fn inner(path: &Path) -> Result { - let output = GitCommand::for_repo(path, ["rev-parse", "--short", "HEAD"])? - .output() - .context("can't get HEAD commit hash")?; - - Ok(output.trim().to_owned()) - } - - inner(path.as_ref()) -} - -struct GitCommand { - command: Command, -} - -impl GitCommand { - fn for_repo(repo_path: &Path, args: I) -> Result - where - I: IntoIterator + Copy, - S: AsRef, - { - // Init the command. - let git_path = which("git").context("can't find git command")?; - let mut command = Command::new(git_path); - command.args(args); - - // Set the path if necessary - command.current_dir(repo_path); - - if cfg!(windows) { - // this method is broken on Windows. See: https://github.com/rust-lang/rust/issues/31259 - //command.env_clear() - } else { - command.env_clear(); - }; - - Ok(GitCommand { command }) - } - - fn output(&mut self) -> Result { - let output = self.command.output()?; - - if output.status.success() { - let output_text = String::from_utf8_lossy(&output.stdout).to_string(); - return Ok(output_text); - } - - match String::from_utf8(output.stderr) { - Ok(msg) if msg.is_empty().not() => Err(anyhow!( - "git failed with message '{}' [status: {}]", - msg.trim(), - output.status - )), - _ => Err(anyhow!("git failed [status: {}]", output.status)), - } - } -} diff --git a/hipcheck/src/main.rs b/hipcheck/src/main.rs index 083eb0e5..4c4fc861 100644 --- a/hipcheck/src/main.rs +++ b/hipcheck/src/main.rs @@ -170,8 +170,6 @@ fn cmd_check(args: &CheckArgs, config: &CliConfig) -> ExitCode { } }; - let raw_version = env!("CARGO_PKG_VERSION", "can't find Hipcheck package version"); - let report = run( target, config.config().map(ToOwned::to_owned), @@ -179,7 +177,6 @@ fn cmd_check(args: &CheckArgs, config: &CliConfig) -> ExitCode { config.cache().map(ToOwned::to_owned), config.policy().map(ToOwned::to_owned), config.format(), - raw_version, ); match report { @@ -207,9 +204,6 @@ fn cmd_schema(args: &SchemaArgs) { } fn cmd_print_weights(config: &CliConfig) -> Result<()> { - // Get the raw hipcheck version. - let raw_version = env!("CARGO_PKG_VERSION", "can't find Hipcheck package version"); - // Silence the global shell while we're checking the dummy repo to prevent progress bars and // title messages from displaying while calculating the weight tree. let silence_guard = Shell::silence(); @@ -229,7 +223,6 @@ fn cmd_print_weights(config: &CliConfig) -> Result<()> { config.cache().map(ToOwned::to_owned), config.policy().map(ToOwned::to_owned), config.format(), - raw_version, )?; // Get the weight tree and print it. @@ -964,7 +957,6 @@ fn run( home_dir: Option, policy_path: Option, format: Format, - raw_version: &str, ) -> Result { // Initialize the session. let session = match Session::new( @@ -974,7 +966,6 @@ fn run( home_dir, policy_path, format, - raw_version, ) { Ok(session) => session, Err(err) => return Err(err), diff --git a/hipcheck/src/session/mod.rs b/hipcheck/src/session/mod.rs index 474dd048..e76fd9b5 100644 --- a/hipcheck/src/session/mod.rs +++ b/hipcheck/src/session/mod.rs @@ -48,7 +48,6 @@ use crate::source::SourceQuery; use crate::source::SourceQueryStorage; use crate::target::SbomStandard; use crate::target::{Target, TargetSeed, TargetSeedKind}; -use crate::version::get_version; use crate::version::VersionQuery; use crate::version::VersionQueryStorage; use chrono::prelude::*; @@ -125,7 +124,6 @@ impl Session { home_dir: Option, policy_path: Option, format: Format, - raw_version: &str, ) -> StdResult { /*=================================================================== * Setting up the session. @@ -211,12 +209,8 @@ impl Session { * Resolving the Hipcheck version. *-----------------------------------------------------------------*/ - let version = match get_version(raw_version) { - Ok(version) => version, - Err(err) => return Err(err), - }; - - session.set_hc_version(Rc::new(version)); + let raw_version = env!("CARGO_PKG_VERSION", "can't find Hipcheck package version"); + session.set_hc_version(Rc::new(raw_version.to_string())); /*=================================================================== * Remaining input queries. diff --git a/hipcheck/src/version.rs b/hipcheck/src/version.rs index 7d8f357e..a354a87b 100644 --- a/hipcheck/src/version.rs +++ b/hipcheck/src/version.rs @@ -3,10 +3,8 @@ use crate::context::Context; use crate::error::Result; use semver::Version; -use std::ops::Not as _; use std::rc::Rc; -/// Query the environment to identify the proper version string. pub fn get_version(raw_version: &str) -> Result { // Basic algorithm: // 1. Check the version number in `Cargo.toml`. @@ -16,17 +14,7 @@ pub fn get_version(raw_version: &str) -> Result { // as ` ()` let version = Version::parse(raw_version).context("can't parse version in Cargo.toml")?; - log::debug!("detected Hipcheck version [version='{:?}']", version); - - if version.pre.as_str().starts_with("alpha") { - let head = option_env!("HC_HEAD_COMMIT").unwrap_or(""); - - if head.is_empty().not() { - return Ok(format!("{} ({})", raw_version, head)); - } - } - Ok(raw_version.to_string()) }