Skip to content

Commit 164ec1e

Browse files
committed
Use git2 crate to get commit hash
1 parent 75a6298 commit 164ec1e

File tree

2 files changed

+24
-10
lines changed

2 files changed

+24
-10
lines changed

Cargo.toml

+1
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ tempdir = "0.3"
5252

5353
[build-dependencies]
5454
time = "0.1"
55+
git2 = "0.6"
5556
sass-rs = "0.0.18"
5657

5758
[[bin]]

build.rs

+23-10
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,43 @@
11

22
extern crate time;
33
extern crate sass_rs;
4+
extern crate git2;
45

56
use std::env;
67
use std::path::Path;
78
use std::fs::File;
89
use std::io::Write;
9-
use std::process::Command;
10+
use git2::Repository;
11+
1012

1113
fn main() {
12-
let git_hash = match Command::new("git")
13-
.args(&["log", "--pretty=format:%h", "-n", "1"])
14-
.output() {
15-
Ok(output) => String::from_utf8_lossy(&output.stdout).into_owned(),
16-
Err(_) => "???????".to_string(),
17-
};
14+
write_git_version();
15+
compile_sass();
16+
}
17+
18+
19+
fn write_git_version() {
20+
let git_hash = get_git_hash().unwrap_or("???????".to_owned());
1821
let build_date = time::strftime("%Y-%m-%d", &time::now_utc()).unwrap();
1922
let dest_path = Path::new(&env::var("OUT_DIR").unwrap()).join("git_version");
2023
let mut file = File::create(&dest_path).unwrap();
2124
write!(file, "({} {})", git_hash, build_date).unwrap();
22-
23-
// compile style.scss
24-
compile_sass();
2525
}
2626

2727

28+
fn get_git_hash() -> Option<String> {
29+
let repo = match Repository::open(env::current_dir().unwrap()) {
30+
Ok(repo) => repo,
31+
Err(_) => return None,
32+
};
33+
let head = repo.head().unwrap();
34+
head.target().map(|h| {
35+
let mut h = format!("{}", h);
36+
h.truncate(7);
37+
h
38+
})
39+
}
40+
2841

2942
fn compile_sass() {
3043
use sass_rs::sass_context::SassFileContext;

0 commit comments

Comments
 (0)