Skip to content

Commit

Permalink
refactor: build.rs (#944)
Browse files Browse the repository at this point in the history
  • Loading branch information
cm-ayf authored Aug 31, 2024
1 parent 59ee1a7 commit f61e6aa
Showing 1 changed file with 20 additions and 27 deletions.
47 changes: 20 additions & 27 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,23 @@
use git2::{DescribeFormatOptions, DescribeOptions, Repository};
use git2::{DescribeFormatOptions, DescribeOptions, Error, Repository};

fn main() {
let Ok(repo) = Repository::open(".").map_err(|e| {
println!("cargo::warning={}", e);
e
}) else {
return;
};
let Ok(git_describe_result) = repo
.describe(
DescribeOptions::new()
.describe_tags()
.show_commit_oid_as_fallback(true),
)
.and_then(|describe| {
describe.format(Some(
DescribeFormatOptions::new()
.always_use_long_format(true)
.dirty_suffix("-dirty"),
))
}).map_err(|e| {
println!("cargo::warning={}", e);
e
})
else {
return;
};
println!("cargo::rustc-env=GIT_DESCRIBE={git_describe_result}")
match get_git_describe_result() {
Ok(result) => println!("cargo::rustc-env=GIT_DESCRIBE={result}"),
Err(e) => println!("cargo::warning={}", e),
}
}

fn get_git_describe_result() -> Result<String, Error> {
let repo = Repository::open(".")?;
let describe = repo.describe(
DescribeOptions::new()
.describe_tags()
.show_commit_oid_as_fallback(true),
)?;
let formatted = describe.format(Some(
DescribeFormatOptions::new()
.always_use_long_format(true)
.dirty_suffix("-dirty"),
))?;
Ok(formatted)
}

0 comments on commit f61e6aa

Please sign in to comment.