object_id: add ChangeId::reverse_hex() for convenience

Borrowed from #4470.
This commit is contained in:
Yuya Nishihara 2024-10-15 17:29:00 +09:00
parent e415c09ede
commit 59c635bfd0
4 changed files with 14 additions and 12 deletions

View file

@ -64,7 +64,6 @@ use jj_lib::git;
use jj_lib::git_backend::GitBackend;
use jj_lib::gitignore::GitIgnoreError;
use jj_lib::gitignore::GitIgnoreFile;
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::matchers::Matcher;
use jj_lib::merge::MergedTreeValue;
@ -2657,9 +2656,7 @@ pub fn short_commit_hash(commit_id: &CommitId) -> String {
}
pub fn short_change_hash(change_id: &ChangeId) -> String {
// TODO: We could avoid the unwrap() and make this more efficient by converting
// straight from binary.
to_reverse_hex(&change_id.hex()[0..12]).unwrap()
change_id.reverse_hex()[0..12].to_string()
}
pub fn short_operation_hash(operation_id: &OperationId) -> String {

View file

@ -31,7 +31,6 @@ use jj_lib::fileset;
use jj_lib::fileset::FilesetDiagnostics;
use jj_lib::fileset::FilesetExpression;
use jj_lib::git;
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::id_prefix::IdPrefixIndex;
use jj_lib::matchers::Matcher;
@ -1249,11 +1248,7 @@ impl CommitOrChangeId {
pub fn hex(&self) -> String {
match self {
CommitOrChangeId::Commit(id) => id.hex(),
CommitOrChangeId::Change(id) => {
// TODO: We can avoid the unwrap() and make this more efficient by converting
// straight from bytes.
to_reverse_hex(&id.hex()).unwrap()
}
CommitOrChangeId::Change(id) => id.reverse_hex(),
}
}

View file

@ -25,6 +25,7 @@ use futures::stream::BoxStream;
use thiserror::Error;
use crate::content_hash::ContentHash;
use crate::hex_util;
use crate::index::Index;
use crate::merge::Merge;
use crate::object_id::id_type;
@ -50,6 +51,16 @@ id_type!(pub FileId);
id_type!(pub SymlinkId);
id_type!(pub ConflictId);
impl ChangeId {
/// Returns the hex string representation of this ID, which uses `z-k`
/// "digits" instead of `0-9a-f`.
pub fn reverse_hex(&self) -> String {
// TODO: We can avoid the unwrap() and make this more efficient by
// converting straight from bytes.
hex_util::to_reverse_hex(&self.hex()).unwrap()
}
}
#[derive(ContentHash, Debug, PartialEq, Eq, Clone, Copy, PartialOrd, Ord)]
pub struct MillisSinceEpoch(pub i64);

View file

@ -29,7 +29,6 @@ use jj_lib::git;
use jj_lib::git_backend::GitBackend;
use jj_lib::graph::GraphEdge;
use jj_lib::graph::ReverseGraphIterator;
use jj_lib::hex_util::to_reverse_hex;
use jj_lib::id_prefix::IdPrefixContext;
use jj_lib::object_id::ObjectId;
use jj_lib::op_store::RefTarget;
@ -403,7 +402,7 @@ fn test_resolve_symbol_in_different_disambiguation_context() {
.with_id_prefix_context(&id_prefix_context);
// Sanity check
let change_hex = &to_reverse_hex(&commit2.change_id().hex()).unwrap();
let change_hex = commit2.change_id().reverse_hex();
assert_eq!(
symbol_resolver
.resolve_symbol(repo2.as_ref(), &change_hex[0..1])