From edef49d1ef58c5ee5766d933081f83fdce739bf2 Mon Sep 17 00:00:00 2001 From: Slack Coder Date: Thu, 15 Dec 2022 14:03:16 -0500 Subject: [PATCH] Improve client example test determinism Collaborative object IDs are derived from their commit oid. These IDs must be reproducible to support testing the client's example. All but one of the parameters used to derive the Object ID is the commit's Author and committer timestamp. Introduce an environment variable 'RAD_COMMIT_TIME' to set both Author and Committer timestamp when creating a commit in the cob module. Use single variable, instead of Git's two (for author and committer), to keep the changes simple. The function being modified returns a single timestamp which is derived from the author's. Signed-off-by: Slack Coder --- radicle-cob/src/backend/git.rs | 5 +++++ radicle-cob/src/backend/git/change.rs | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/radicle-cob/src/backend/git.rs b/radicle-cob/src/backend/git.rs index 4cd70cb2..131f4057 100644 --- a/radicle-cob/src/backend/git.rs +++ b/radicle-cob/src/backend/git.rs @@ -4,3 +4,8 @@ // Linking Exception. For full terms see the included LICENSE file. pub mod change; + +/// Environment variable to set to overwrite the commit date for both the author and the committer. +/// +/// The format must be a unix timestamp. +pub const RAD_COMMIT_TIME: &str = "RAD_COMMIT_TIME"; diff --git a/radicle-cob/src/backend/git/change.rs b/radicle-cob/src/backend/git/change.rs index fbe16c54..8aec7f83 100644 --- a/radicle-cob/src/backend/git/change.rs +++ b/radicle-cob/src/backend/git/change.rs @@ -11,6 +11,7 @@ use git_ext::Oid; use git_trailers::OwnedTrailer; use nonempty::NonEmpty; +use crate::git; use crate::history::entry::Timestamp; use crate::{ change::{self, store, Change}, @@ -235,13 +236,23 @@ where { let author = repo.signature()?; - let timestamp = author.when().seconds() as Timestamp; + let mut timestamp = author.when().seconds(); + let mut headers = commit::Headers::new(); headers.push( "gpgsig", &String::from_utf8(crypto::ssh::ExtendedSignature::from(signature).to_armored())?, ); - let author = commit::Author::try_from(&author)?; + let mut author = commit::Author::try_from(&author)?; + + #[cfg(debug_assertions)] + if let Ok(s) = std::env::var(git::RAD_COMMIT_TIME) { + if let Ok(v) = s.trim().parse::() { + author.time = git_commit::author::Time::new(v, 0); + timestamp = v; + } + } + let oid = Commit::new( tree.id(), parents, @@ -253,7 +264,7 @@ where ) .write(repo)?; - Ok((Oid::from(oid), timestamp)) + Ok((Oid::from(oid), timestamp as u64)) } }