Skip to content

Commit

Permalink
Use build-info by default
Browse files Browse the repository at this point in the history
Fixed issue where commit sha was missing if crate source wasn't from a
git repository (e.g. when you download the crate from crates.io)
  • Loading branch information
epilys committed Mar 23, 2023
1 parent 960dccc commit 2f1c6a0
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 19 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ path = "src/repl.rs"
required-features = ["python"]

[dependencies]
build-info = { version = "0.0.29", default-features = false, optional = true }
build-info = { version = "0.0.29" }
generational-arena = "0.2"
gio = { version = "^0.15", features = [ "v2_56", ] }
glib = { version = "^0.15" }
Expand Down Expand Up @@ -65,11 +65,10 @@ features = [
]

[build-dependencies]
build-info-build = { version = "0.0.29", optional = true }
build-info-build = { version = "0.0.29" }

[features]
default = ["python", "git"]
build-info = ["dep:build-info", "dep:build-info-build"]
python = ["pyo3"]
git = ["git2"]

Expand Down
23 changes: 22 additions & 1 deletion build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,28 @@ fn main() -> std::io::Result<()> {
}
}
}
#[cfg(feature = "build-info")]
if let Ok(s) = std::fs::read_to_string(".cargo_vcs_info.json") {
const KEY: &str = "\"sha1\":";

fn find_tail<'str>(str: &'str str, tok: &str) -> Option<&'str str> {
let i = str.find(tok)?;
Some(&str[(i + tok.len())..])
}

if let Some(mut tail) = find_tail(&s, KEY) {
while !tail.starts_with('"') && !tail.is_empty() {
tail = &tail[1..];
}
if !tail.is_empty() {
// skip "
tail = &tail[1..];
if let Some(end) = find_tail(tail, "\"") {
let end = tail.len() - end.len();
println!("cargo:rustc-env=GIT_HASH={}", &tail[..end]);
}
}
}
}
build_info_build::build_script();
Ok(())
}
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,6 @@ pub struct Error;

impl Error {
pub fn suggest_bug_report(err: &str) -> String {
format!("Application error: {err}\n\nIf you wish to report this bug to <{}>, you can include the following build info string:\n\n{}", crate::ISSUE_TRACKER, crate::BUILD_INFO)
format!("Application error: {err}\n\nIf you wish to report this bug to <{}>, you can include the following build info string:\n\n{} {}", crate::ISSUE_TRACKER, crate::BUILD_INFO, crate::get_git_sha())
}
}
41 changes: 28 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,22 +77,37 @@ pub mod utils;
pub mod views;
pub mod window;

const fn _get_package_git_sha() -> Option<&'static str> {
option_env!("PACKAGE_GIT_SHA")
}

const _PACKAGE_COMMIT_SHA: Option<&str> = _get_package_git_sha();

pub fn get_git_sha() -> std::borrow::Cow<'static, str> {
if let Some(r) = _PACKAGE_COMMIT_SHA {
return r.into();
}
build_info::build_info!(fn build_info);
let info = build_info();
if let Some(v) = info
.version_control
.as_ref()
.and_then(|v| v.git())
.map(|g| g.commit_short_id.clone())
{
v.into()
} else {
"<unknown>".into()
}
}

pub const APPLICATION_NAME: &str = "gerb";
pub const APPLICATION_ID: &str = "com.epilys.gerb";
pub const ISSUE_TRACKER: &str = "https://github.com/epilys/gerb/issues";
#[cfg(feature = "build-info")]
pub const VERSION_INFO: &str = build_info::format!("{} v{} commit {}", $.crate_info.name, $.crate_info.version, $.version_control?.git()?.commit_short_id);
#[cfg(feature = "build-info")]
pub const BUILD_INFO: &str = build_info::format!("{}\t{}\t{}\t{}\t{}", $.crate_info.version, $.version_control?.git()?.commit_short_id, $.compiler, $.timestamp, $.crate_info.enabled_features);
#[cfg(feature = "build-info")]
pub const CLI_INFO: &str = build_info::format!("\n ,adPPYb,d8 \n a8\" `Y88 \n 8b 88 \n \"8a, ,d88 \n `\"YbbdP\"Y8 \n aa, ,88 \n \"Y8bbdP\" \n\n{} Copyright (C) 2022 Emmanouil Pitsidianakis\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to\nredistribute it under certain conditions; See\nLICENSE.md for more details.\n\nVersion: {}\nCommit SHA: {}\nAuthors: {}\nLicense: GPL version 3 or later\nCompiler: {}\nBuild-Date: {}\nEnabled-features: {}", $.crate_info.name, $.crate_info.version, $.version_control?.git()?.commit_short_id, $.crate_info.authors, $.compiler, $.timestamp, $.crate_info.enabled_features);

#[cfg(not(feature = "build-info"))]
pub const VERSION_INFO: &str = "not included if built without `build-info` feature";
#[cfg(not(feature = "build-info"))]
pub const BUILD_INFO: &str = VERSION_INFO;
#[cfg(not(feature = "build-info"))]
pub const CLI_INFO: &str = VERSION_INFO;
pub const VERSION_INFO: &str =
build_info::format!("{} v{}", $.crate_info.name, $.crate_info.version);
pub const BUILD_INFO: &str = build_info::format!("{}\t{}\t{}\t{}", $.crate_info.version, $.compiler, $.timestamp, $.crate_info.enabled_features);
pub const CLI_INFO: &str = build_info::format!("\n ,adPPYb,d8 \n a8\" `Y88 \n 8b 88 \n \"8a, ,d88 \n `\"YbbdP\"Y8 \n aa, ,88 \n \"Y8bbdP\" \n\n{} Copyright (C) 2022 Emmanouil Pitsidianakis\nThis program comes with ABSOLUTELY NO WARRANTY.\nThis is free software, and you are welcome to\nredistribute it under certain conditions; See\nLICENSE.md for more details.\n\nVersion: {}\nAuthors: {}\nLicense: GPL version 3 or later\nCompiler: {}\nBuild-Date: {}\nEnabled-features: {}", $.crate_info.name, $.crate_info.version, $.crate_info.authors, $.compiler, $.timestamp, $.crate_info.enabled_features);

/* Annotations:
*
Expand Down
4 changes: 3 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ fn main() {
.lookup_value("version", Some(glib::VariantTy::BOOLEAN))
.is_some()
{
println!("{}", crate::VERSION_INFO);
print!("{}", crate::VERSION_INFO);
println!(" {}", crate::get_git_sha());
// Exit with success code.
return 0;
}
Expand All @@ -94,6 +95,7 @@ fn main() {
.is_some()
{
println!("{}", crate::CLI_INFO);
println!("Commit SHA: {}", crate::get_git_sha());
// Exit with success code.
return 0;
}
Expand Down

0 comments on commit 2f1c6a0

Please sign in to comment.