-
Notifications
You must be signed in to change notification settings - Fork 0
/
build.rs
37 lines (30 loc) · 1.17 KB
/
build.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
use git2::Repository;
fn main() {
let repo = Repository::open(".").unwrap();
let head = repo.head().unwrap();
let head_commit = head.peel_to_commit().unwrap();
let mut is_dirty = false;
for status in repo.statuses(None).unwrap().iter() {
let status = status.status();
if !status.contains(git2::Status::IGNORED) {
is_dirty = true;
break;
}
}
if is_dirty {
println!("cargo:rustc-env=GIT_HASH={}+dirty", &head_commit.id().to_string()[..7]);
} else {
println!("cargo:rustc-env=GIT_HASH={}", &head_commit.id().to_string()[..7]);
}
let mut revwalk = repo.revwalk().unwrap();
revwalk.set_sorting(git2::Sort::NONE).unwrap();
revwalk.push_head().unwrap();
let revwalk = revwalk.map(|id| {
let id = id.unwrap();
println!("{}", id);
let commit = repo.find_commit(id).unwrap();
return format!("[`{}`] {}: {}", &id.to_string()[..7], commit.author().name().unwrap(), commit.summary().unwrap());
}).take(5);
let latest_commits = revwalk.collect::<Vec<_>>().join("\n");
println!("cargo:rustc-env=GIT_LOG={}", latest_commits.replace("\n", "\\n"));
}