Skip to content

Commit

Permalink
chore: Remove unused version handling
Browse files Browse the repository at this point in the history
Previously (before open sourcing Hipcheck) we'd update the `Cargo.toml`
file for the Hipcheck crate after releasing a new version to reflect that
we're now working on a pre-release of the _next_ version, and we had special
handling in Hipcheck itself to identify at build time when we were building
one of these pre-release versions and add the commit hash of the repository
itself to Hipcheck's version. This was cool, and the idea was to make it
obvious when you were running a pre-release build of Hipcheck and _which_
pre-release build you were running.

However, when we transitioned to open source, we changed how releases
happen, and stopped doing the pre-release marking, which means this special
handling code hadn't been used at all since then. What's more, it's an extra
piece of complexity that we've been carrying around. Rather than try to
once again change our release flow, I'm removing the special handling code
since it's unused.

Signed-off-by: Andrew Lilley Brinker <[email protected]>
  • Loading branch information
alilleybrinker authored and mchernicoff committed Aug 28, 2024
1 parent 3963760 commit 7a23a93
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 105 deletions.
77 changes: 1 addition & 76 deletions hipcheck/build.rs
Original file line number Diff line number Diff line change
@@ -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<P: AsRef<Path>>(path: P) -> Result<String> {
fn inner(path: &Path) -> Result<String> {
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<I, S>(repo_path: &Path, args: I) -> Result<GitCommand>
where
I: IntoIterator<Item = S> + Copy,
S: AsRef<OsStr>,
{
// 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<String> {
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)),
}
}
}
9 changes: 0 additions & 9 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,16 +170,13 @@ 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),
config.data().map(ToOwned::to_owned),
config.cache().map(ToOwned::to_owned),
config.policy().map(ToOwned::to_owned),
config.format(),
raw_version,
);

match report {
Expand Down Expand Up @@ -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();
Expand All @@ -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.
Expand Down Expand Up @@ -964,7 +957,6 @@ fn run(
home_dir: Option<PathBuf>,
policy_path: Option<PathBuf>,
format: Format,
raw_version: &str,
) -> Result<AnyReport> {
// Initialize the session.
let session = match Session::new(
Expand All @@ -974,7 +966,6 @@ fn run(
home_dir,
policy_path,
format,
raw_version,
) {
Ok(session) => session,
Err(err) => return Err(err),
Expand Down
10 changes: 2 additions & 8 deletions hipcheck/src/session/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand Down Expand Up @@ -125,7 +124,6 @@ impl Session {
home_dir: Option<PathBuf>,
policy_path: Option<PathBuf>,
format: Format,
raw_version: &str,
) -> StdResult<Session, Error> {
/*===================================================================
* Setting up the session.
Expand Down Expand Up @@ -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.
Expand Down
12 changes: 0 additions & 12 deletions hipcheck/src/version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> {
// Basic algorithm:
// 1. Check the version number in `Cargo.toml`.
Expand All @@ -16,17 +14,7 @@ pub fn get_version(raw_version: &str) -> Result<String> {
// as `<version number> (<HEAD commit>)`

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())
}

Expand Down

0 comments on commit 7a23a93

Please sign in to comment.