forked from mirrors/jj
backend: create BackendError::InvalidHashLength
Strictly speaking, we could rely on e.g. `git2::Oid::from_str` to produce an error, but I figure that having an explicit error for a mismatching hash length might demystify some error condition in the future, since commit IDs and change IDs and potentially other backends' IDs may have different lengths, so this could flag a mismatch earlier/more obviously.
This commit is contained in:
parent
7f8a196ab2
commit
456be4cc73
3 changed files with 37 additions and 4 deletions
|
@ -173,6 +173,16 @@ content_hash! {
|
|||
|
||||
#[derive(Debug, Error, PartialEq, Eq)]
|
||||
pub enum BackendError {
|
||||
#[error(
|
||||
"Invalid hash for object of type {object_type}: {hash} (expected {expected} bytes, got \
|
||||
{actual} bytes)"
|
||||
)]
|
||||
InvalidHashLength {
|
||||
expected: usize,
|
||||
actual: usize,
|
||||
object_type: &'static str,
|
||||
hash: String,
|
||||
},
|
||||
#[error("Object not found")]
|
||||
NotFound,
|
||||
#[error("Error: {0}")]
|
||||
|
|
|
@ -180,7 +180,12 @@ impl Backend for GitBackend {
|
|||
|
||||
fn read_file(&self, _path: &RepoPath, id: &FileId) -> BackendResult<Box<dyn Read>> {
|
||||
if id.as_bytes().len() != self.hash_length() {
|
||||
return Err(BackendError::NotFound);
|
||||
return Err(BackendError::InvalidHashLength {
|
||||
expected: self.hash_length(),
|
||||
actual: id.as_bytes().len(),
|
||||
object_type: "file",
|
||||
hash: id.hex(),
|
||||
});
|
||||
}
|
||||
let locked_repo = self.repo.lock().unwrap();
|
||||
let blob = locked_repo
|
||||
|
@ -200,7 +205,12 @@ impl Backend for GitBackend {
|
|||
|
||||
fn read_symlink(&self, _path: &RepoPath, id: &SymlinkId) -> Result<String, BackendError> {
|
||||
if id.as_bytes().len() != self.hash_length() {
|
||||
return Err(BackendError::NotFound);
|
||||
return Err(BackendError::InvalidHashLength {
|
||||
expected: self.hash_length(),
|
||||
actual: id.as_bytes().len(),
|
||||
object_type: "symlink",
|
||||
hash: id.hex(),
|
||||
});
|
||||
}
|
||||
let locked_repo = self.repo.lock().unwrap();
|
||||
let blob = locked_repo
|
||||
|
@ -229,7 +239,12 @@ impl Backend for GitBackend {
|
|||
return Ok(Tree::default());
|
||||
}
|
||||
if id.as_bytes().len() != self.hash_length() {
|
||||
return Err(BackendError::NotFound);
|
||||
return Err(BackendError::InvalidHashLength {
|
||||
expected: self.hash_length(),
|
||||
actual: id.as_bytes().len(),
|
||||
object_type: "tree",
|
||||
hash: id.hex(),
|
||||
});
|
||||
}
|
||||
|
||||
let locked_repo = self.repo.lock().unwrap();
|
||||
|
@ -348,7 +363,12 @@ impl Backend for GitBackend {
|
|||
|
||||
fn read_commit(&self, id: &CommitId) -> BackendResult<Commit> {
|
||||
if id.as_bytes().len() != self.hash_length() {
|
||||
return Err(BackendError::NotFound);
|
||||
return Err(BackendError::InvalidHashLength {
|
||||
expected: self.hash_length(),
|
||||
actual: id.as_bytes().len(),
|
||||
object_type: "commit",
|
||||
hash: id.hex(),
|
||||
});
|
||||
}
|
||||
|
||||
if *id == self.root_commit_id {
|
||||
|
|
|
@ -86,6 +86,9 @@ fn resolve_full_commit_id(
|
|||
symbol: &str,
|
||||
) -> Result<Option<Vec<CommitId>>, RevsetError> {
|
||||
if let Ok(binary_commit_id) = hex::decode(symbol) {
|
||||
if repo.store().hash_length() != binary_commit_id.len() {
|
||||
return Ok(None);
|
||||
}
|
||||
let commit_id = CommitId::new(binary_commit_id);
|
||||
match repo.store().get_commit(&commit_id) {
|
||||
Ok(_) => Ok(Some(vec![commit_id])),
|
||||
|
|
Loading…
Reference in a new issue