ok/jj
1
0
Fork 0
forked from mirrors/jj

object_id: don't allow ObjectId::from_hex() a dynamically allocated string

This isn't technically needed, but it prevents API misuse. Another option
is to do some compile-time substitution, but most callers are tests and the
runtime performance wouldn't matter.
This commit is contained in:
Yuya Nishihara 2024-01-05 11:57:26 +09:00
parent 837ac15052
commit a6616e9cea
4 changed files with 8 additions and 7 deletions

View file

@ -930,7 +930,7 @@ mod tests {
let mut index = DefaultMutableIndex::full(3, 16);
// Long linear history with some short branches
let ids = (0..11)
.map(|n| CommitId::from_hex(&format!("{n:06x}")))
.map(|n| CommitId::try_from_hex(&format!("{n:06x}")).unwrap())
.collect_vec();
index.add_commit_data(ids[0].clone(), new_change_id(), &[]);
for i in 1..ids.len() {

View file

@ -44,8 +44,9 @@ macro_rules! impl_id_type {
/// Parses the given hex string into an ObjectId.
///
/// The given string is usually a literal, and must be valid.
pub fn from_hex(hex: &str) -> Self {
/// The given string must be valid. A static str is required to
/// prevent API misuse.
pub fn from_hex(hex: &'static str) -> Self {
Self::try_from_hex(hex).unwrap()
}

View file

@ -757,12 +757,12 @@ mod tests {
#[test]
fn test_migrate_git_refs_remote_named_git() {
let normal_ref_target = |id_hex: &str| RefTarget::normal(CommitId::from_hex(id_hex));
let normal_new_remote_ref = |id_hex: &str| RemoteRef {
let normal_ref_target = |id_hex| RefTarget::normal(CommitId::from_hex(id_hex));
let normal_new_remote_ref = |id_hex| RemoteRef {
target: normal_ref_target(id_hex),
state: RemoteRefState::New,
};
let normal_tracking_remote_ref = |id_hex: &str| RemoteRef {
let normal_tracking_remote_ref = |id_hex| RemoteRef {
target: normal_ref_target(id_hex),
state: RemoteRefState::Tracking,
};

View file

@ -2792,7 +2792,7 @@ fn test_change_id_index() {
let root_commit = repo.store().root_commit();
let mut commit_number = 0;
let mut commit_with_change_id = |change_id: &str| {
let mut commit_with_change_id = |change_id| {
commit_number += 1;
tx.mut_repo()
.new_commit(