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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
use std::env;
use std::process::Command;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Set a build-time `GIT_HEAD` env var which includes the commit id;
// such that we can tell which code is running.
let hash = env::var("GIT_HEAD").unwrap_or_else(|_| {
Command::new("git")
.arg("rev-parse")
.arg("--short")
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or("unknown".into())
});
let version = env::var("RADICLE_VERSION").unwrap_or_else(|_| {
// If `RADICLE_VERSION` is not set, we still try our best to
// describe this version by asking git. The result will in many
// cases be a reference to the last released version, and how
// many commits we are ahead, plus a short version of the
// object ID of `HEAD`, e.g. `releases/x.y.z-80-gefe10f95be-dirty`
// which would mean that we built 80 commits ahead of release
// x.y.z, with efe10f95be being a unique prefix of the OID of
// `HEAD`, and the working directory was dirty.
// If this is a build pointing to a commit that has release tag, this
// will just return the tag name itself, e.g. `releases/x.y.z`.
// If all fails, we just use `hash`, which, in the worst case is
// still "unknown" (see above) but in most cases will just be
// the short OID of `HEAD`.
Command::new("git")
.arg("describe")
.arg("--always")
.arg("--broken")
.arg("--dirty")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or(hash.clone())
});
// Since in the previous step we are likely to almost always end up with
// a prefix of `releases/`, as this is the scheme we use in this
// repository, we remove this common prefix, to get nice version numbers.
let version = if let Some(stripped) = version.strip_prefix("releases/") {
stripped.to_owned()
} else {
version
};
// Set a build-time `SOURCE_DATE_EPOCH` env var which includes the commit time.
let commit_time = env::var("SOURCE_DATE_EPOCH").unwrap_or_else(|_| {
Command::new("git")
.arg("log")
.arg("-1")
.arg("--pretty=%ct")
.arg("HEAD")
.output()
.ok()
.and_then(|output| {
if output.status.success() {
String::from_utf8(output.stdout).ok()
} else {
None
}
})
.unwrap_or(0.to_string())
});
println!("cargo::rustc-env=RADICLE_VERSION={version}");
println!("cargo::rustc-env=SOURCE_DATE_EPOCH={commit_time}");
println!("cargo::rustc-env=GIT_HEAD={hash}");
Ok(())
}