Skip to content

Commit ceb485d

Browse files
committed
address auto-review
- clarify ChangeIDs - they actually have a hash-independent format.
1 parent 056fdf6 commit ceb485d

5 files changed

Lines changed: 17 additions & 22 deletions

File tree

crates/but-core/src/change_id.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,7 @@ impl ChangeId {
2929
}
3030

3131
/// Creates a random length 32 reverse hex ChangeId (SHA-1).
32-
///
33-
/// As opposed to [`crate::commit::Headers::synthetic_change_id_from_commit_id`], these
34-
/// have 4 bytes more than synthetic ones, which could indicate that this was a newly
35-
/// created commit that was imbued with a change-id from the get-go.
36-
pub fn generate_sha1() -> Self {
32+
pub fn generate() -> Self {
3733
let mut rng = rand::rng();
3834
let bytes: [u8; CHANGE_ID_REVERSE_BYTE_LEN] = rng.random();
3935
ChangeId::from_bytes(&bytes)

crates/but-core/src/commit.rs

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@ impl Headers {
3838
///
3939
/// Useful for when [`Self::change_id`] is `None`.
4040
///
41-
/// These synthesized IDs are compatible with Jujutsu's deterministic scheme, including for
42-
/// SHA-256 object IDs.
41+
/// These synthesized IDs are compatible with Jujutsu's deterministic scheme,
42+
/// and JJ would create exactly the same change-id if given the `commit_id`.
4343
pub fn synthetic_change_id_from_commit_id(commit_id: gix::ObjectId) -> ChangeId {
44-
let bytes: Vec<_> = commit_id.as_bytes()[4..commit_id.kind().len_in_bytes()]
44+
let bytes: Vec<_> = commit_id.as_bytes()[4..gix::hash::Kind::Sha1.len_in_bytes()]
4545
.iter()
4646
.rev()
4747
.map(|byte| byte.reverse_bits())
@@ -59,10 +59,7 @@ impl Headers {
5959
if self.change_id.is_none() {
6060
self.change_id = commit_id
6161
.into()
62-
.map_or_else(
63-
ChangeId::generate_sha1,
64-
Self::synthetic_change_id_from_commit_id,
65-
)
62+
.map_or_else(ChangeId::generate, Self::synthetic_change_id_from_commit_id)
6663
.into();
6764
}
6865
self
@@ -73,7 +70,7 @@ impl Headers {
7370
#[deprecated = "We want deterministic change-ids, use Headers::synthetic_change_id_from_commit_id() instead."]
7471
pub fn new_with_random_change_id() -> Self {
7572
Self {
76-
change_id: Some(ChangeId::generate_sha1()),
73+
change_id: Some(ChangeId::generate()),
7774
conflicted: None,
7875
}
7976
}
@@ -94,7 +91,7 @@ impl Headers {
9491
.ok()
9592
.map(ChangeId::from_number_for_testing)
9693
})
97-
.unwrap_or_else(ChangeId::generate_sha1),
94+
.unwrap_or_else(ChangeId::generate),
9895
),
9996
conflicted: None,
10097
}

crates/but-core/tests/core/change_id.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ mod generate {
33

44
#[test]
55
fn returns_a_32_character_random_string() {
6-
let a = ChangeId::generate_sha1();
6+
let a = ChangeId::generate();
77
assert_eq!(a.to_string().len(), 32);
8-
let b = ChangeId::generate_sha1();
8+
let b = ChangeId::generate();
99
assert_ne!(a, b, "these are always different");
1010
}
1111
}

crates/but-gerrit/src/lib.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,7 @@ mod tests {
262262

263263
#[test]
264264
fn output_is_41_characters_long() {
265-
let commit_change_id = ChangeId::generate_sha1();
265+
let commit_change_id = ChangeId::generate();
266266
let change_id = GerritChangeId::from(&commit_change_id);
267267
let output = format!("{change_id}");
268268
assert_eq!(output.len(), 41); // "I" + 40 hex chars
@@ -271,7 +271,7 @@ mod tests {
271271

272272
#[test]
273273
fn test_add_trailer_no_existing_trailers() {
274-
let commit_change_id = ChangeId::generate_sha1();
274+
let commit_change_id = ChangeId::generate();
275275
let change_id = GerritChangeId::from(&commit_change_id);
276276
let change_id_line = format!("Change-Id: {change_id}\n");
277277

@@ -287,7 +287,7 @@ mod tests {
287287

288288
#[test]
289289
fn test_add_trailer_already_has_change_id() {
290-
let commit_change_id = ChangeId::generate_sha1();
290+
let commit_change_id = ChangeId::generate();
291291
let change_id = GerritChangeId::from(&commit_change_id);
292292
let change_id_line = format!("Change-Id: {change_id}\n");
293293

@@ -298,7 +298,7 @@ mod tests {
298298

299299
#[test]
300300
fn test_add_trailer_with_signed_off_by() {
301-
let commit_change_id = ChangeId::generate_sha1();
301+
let commit_change_id = ChangeId::generate();
302302
let change_id = GerritChangeId::from(&commit_change_id);
303303
let change_id_line = format!("Change-Id: {change_id}\n");
304304

packages/core/src/generated/workspace/index.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,11 @@ export type Commit = {
131131
*/
132132
author: Author;
133133
/**
134-
* The GitButler change-id associated with this commit, if available.
134+
* The GitButler change-id associated with this commit.
135+
* It always exists as we either read it from the [headers][but_core::commit::Headers], or we
136+
* synthesize one from [the commit id][but_core::commit::Headers::synthetic_change_id_from_commit_id()].
135137
*/
136-
changeId: string | null;
138+
changeId: string;
137139
/**
138140
* Optional URL to the Gerrit review for this commit, if applicable.
139141
* Only populated if Gerrit mode is enabled and the commit has an associated review.

0 commit comments

Comments
 (0)