Skip to content

Commit 0c8c683

Browse files
committed
xtask: Inject timestamp into version
I was hitting an issue with our COPR builds where we were getting *earlier* builds because the git hash was the first component of the version, and the leading numbers there don't increment, and RPM wants higher versions. Force newer-is-better by injecting the date (up to the minute, not second because we're not going to build more than one a second). Signed-off-by: Colin Walters <[email protected]>
1 parent adf16d5 commit 0c8c683

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

xtask/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ path = "src/xtask.rs"
1414
[dependencies]
1515
anyhow = "1.0.68"
1616
camino = "1.0"
17+
chrono = { version = "0.4.23", default_features = false, features = ["std"] }
1718
fn-error-context = "0.2.0"
1819
tempfile = "3.3"
1920
xshell = { version = "0.2" }

xtask/src/xtask.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ fn gitrev(sh: &Shell) -> Result<String> {
6666
}
6767
}
6868

69+
/// Return a string formatted version of the git commit timestamp, up to the minute
70+
/// but not second because, well, we're not going to build more than once a second.
71+
#[context("Finding git timestamp")]
72+
fn git_timestamp(sh: &Shell) -> Result<String> {
73+
let ts = cmd!(sh, "git show --format=%ct").read()?;
74+
let ts = ts.trim().parse::<i64>()?;
75+
let ts = chrono::NaiveDateTime::from_timestamp_opt(ts, 0)
76+
.ok_or_else(|| anyhow::anyhow!("Failed to parse timestamp"))?;
77+
Ok(ts.format("%Y%m%d%H%M").to_string())
78+
}
79+
6980
struct Package {
7081
version: String,
7182
srcpath: Utf8PathBuf,
@@ -74,6 +85,9 @@ struct Package {
7485
#[context("Packaging")]
7586
fn impl_package(sh: &Shell) -> Result<Package> {
7687
let v = gitrev(sh)?;
88+
let timestamp = git_timestamp(sh)?;
89+
// We always inject the timestamp first to ensure that newer is better.
90+
let v = format!("{timestamp}.{v}");
7791
let namev = format!("{NAME}-{v}");
7892
let p = Utf8Path::new("target").join(format!("{namev}.tar.zstd"));
7993
let o = File::create(&p)?;

0 commit comments

Comments
 (0)