diff --git a/lib/src/commit_builder.rs b/lib/src/commit_builder.rs index 21f1c06b29..97d15f5f78 100644 --- a/lib/src/commit_builder.rs +++ b/lib/src/commit_builder.rs @@ -12,25 +12,21 @@ // See the License for the specific language governing permissions and // limitations under the License. -use uuid::Uuid; +use std::sync::Arc; -use crate::backend::{self, BackendResult, ChangeId, CommitId, ObjectId, Signature, TreeId}; +use crate::backend::{self, BackendResult, ChangeId, CommitId, Signature, TreeId}; use crate::commit::Commit; use crate::repo::MutableRepo; -use crate::settings::UserSettings; +use crate::settings::{JJRng, UserSettings}; #[must_use] pub struct CommitBuilder<'repo> { mut_repo: &'repo mut MutableRepo, + rng: Arc, commit: backend::Commit, rewrite_source: Option, } -// TODO: This function will be replaced in the next commit. -pub fn new_change_id() -> ChangeId { - ChangeId::from_bytes(Uuid::new_v4().as_bytes()) -} - impl CommitBuilder<'_> { pub fn for_new_commit<'repo>( mut_repo: &'repo mut MutableRepo, @@ -40,17 +36,19 @@ impl CommitBuilder<'_> { ) -> CommitBuilder<'repo> { let signature = settings.signature(); assert!(!parents.is_empty()); + let rng = settings.get_rng(); let commit = backend::Commit { parents, predecessors: vec![], root_tree: tree_id, - change_id: new_change_id(), + change_id: rng.new_change_id(), description: String::new(), author: signature.clone(), committer: signature, }; CommitBuilder { mut_repo, + rng, commit, rewrite_source: None, } @@ -75,6 +73,7 @@ impl CommitBuilder<'_> { CommitBuilder { mut_repo, commit, + rng: settings.get_rng(), rewrite_source: Some(predecessor.clone()), } } @@ -101,7 +100,7 @@ impl CommitBuilder<'_> { } pub fn generate_new_change_id(mut self) -> Self { - self.commit.change_id = new_change_id(); + self.commit.change_id = self.rng.new_change_id(); self } diff --git a/lib/src/settings.rs b/lib/src/settings.rs index dccf901000..860a6d0444 100644 --- a/lib/src/settings.rs +++ b/lib/src/settings.rs @@ -19,7 +19,7 @@ use chrono::DateTime; use rand::prelude::*; use rand_chacha::ChaCha20Rng; -use crate::backend::{Signature, Timestamp}; +use crate::backend::{ChangeId, ObjectId, Signature, Timestamp}; #[derive(Debug, Clone)] pub struct UserSettings { @@ -178,8 +178,19 @@ impl UserSettings { #[derive(Debug)] pub struct JJRng(Mutex); impl JJRng { - /// Wraps Rng::gen but only requires an immutable reference. - pub fn gen(&self) -> T + pub fn new_change_id(&self) -> ChangeId { + let random_bytes = self.gen(); + // The `uuid` crate is used merely to specify the number of random bytes (16) + ChangeId::from_bytes( + uuid::Builder::from_random_bytes(random_bytes) + .into_uuid() + .as_bytes(), + ) + } + + /// Wraps Rng::gen but only requires an immutable reference. Can be made + /// public if there's a use for it. + fn gen(&self) -> T where rand::distributions::Standard: rand::distributions::Distribution, { diff --git a/tests/test_log_command.rs b/tests/test_log_command.rs index f9ca2a266a..39490f4919 100644 --- a/tests/test_log_command.rs +++ b/tests/test_log_command.rs @@ -61,6 +61,16 @@ fn test_log_with_or_without_diff() { o (no description set) "###); + // Test default log output format + insta::assert_snapshot!(test_env.jj_cmd_success(&repo_path, &["log"]), @r###" + @ ffdaa62087a2 test.user@example.com 2001-02-03 04:05:10.000 +07:00 789e536fd2e0 + | a new commit + o 9a45c67d3e96 test.user@example.com 2001-02-03 04:05:08.000 +07:00 4291e264ae97 + | add a file + o 000000000000 1970-01-01 00:00:00.000 +00:00 000000000000 + (no description set) + "###); + let stdout = test_env.jj_cmd_success(&repo_path, &["log", "-T", "description", "--no-graph"]); insta::assert_snapshot!(stdout, @r###" a new commit