Skip to content

Commit b2eae78

Browse files
committed
Ensure build script doesn't fail outside git
1 parent a7d4407 commit b2eae78

File tree

1 file changed

+14
-13
lines changed

1 file changed

+14
-13
lines changed

tool/build.rs

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,22 @@
11
use anyhow::Result;
22

3+
fn describe_git() -> Result<String> {
4+
let dir = std::env::current_dir()?;
5+
let repo = git2::Repository::discover(dir)?;
6+
let mut desc_opts = git2::DescribeOptions::new();
7+
desc_opts.describe_tags();
8+
let desc = repo.describe(&desc_opts)?;
9+
let mut fmt_opts = git2::DescribeFormatOptions::new();
10+
fmt_opts.dirty_suffix("-dirty");
11+
Ok(desc.format(Some(&fmt_opts))?)
12+
}
13+
314
fn main() -> Result<()> {
415
println!("cargo:rerun-if-env-changed=TOOL_VERSION");
516

6-
let version = match dbg!(std::env::var("TOOL_VERSION")) {
7-
Ok(value) => value,
8-
_ => {
9-
let dir = std::env::current_dir()?;
10-
let repo = git2::Repository::discover(dir)?;
11-
let mut desc_opts = git2::DescribeOptions::new();
12-
desc_opts.describe_tags();
13-
let desc = repo.describe(&desc_opts)?;
14-
let mut fmt_opts = git2::DescribeFormatOptions::new();
15-
fmt_opts.dirty_suffix("-dirty");
16-
desc.format(Some(&fmt_opts))?
17-
}
18-
};
17+
let version = std::env::var("TOOL_VERSION")
18+
.or_else(|_| describe_git())
19+
.unwrap_or_else(|_| "unknown".into());
1920

2021
println!("cargo:rustc-env=TOOL_VERSION={version}");
2122

0 commit comments

Comments
 (0)