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

git_backend: use non-owned str::from_utf8() to decode symlink target

Just for consistency with the other changes. str::Utf8Error is 2 words long,
so I removed the boxing.
This commit is contained in:
Yuya Nishihara 2023-10-30 06:33:14 +09:00
parent d1c71c05c9
commit 06c254e742
2 changed files with 13 additions and 11 deletions

View file

@ -251,9 +251,7 @@ pub enum BackendError {
InvalidUtf8 {
object_type: String,
hash: String,
// Box to reduce size for other error types that include this one:
// https://rust-lang.github.io/rust-clippy/master/index.html#result_large_err
source: Box<std::string::FromUtf8Error>,
source: std::str::Utf8Error,
},
#[error("Object {hash} of type {object_type} not found: {source}")]
ObjectNotFound {

View file

@ -16,11 +16,11 @@
use std::any::Any;
use std::fmt::{Debug, Error, Formatter};
use std::fs;
use std::io::{Cursor, Read};
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, MutexGuard};
use std::{fs, str};
use async_trait::async_trait;
use git2::Oid;
@ -468,6 +468,14 @@ fn map_not_found_err(err: git2::Error, id: &impl ObjectId) -> BackendError {
}
}
fn to_invalid_utf8_err(source: str::Utf8Error, id: &impl ObjectId) -> BackendError {
BackendError::InvalidUtf8 {
object_type: id.object_type(),
hash: id.hex(),
source,
}
}
fn import_extra_metadata_entries_from_heads(
git_repo: &git2::Repository,
mut_table: &mut MutableTable,
@ -556,13 +564,9 @@ impl Backend for GitBackend {
let blob = locked_repo
.find_blob(git_blob_id)
.map_err(|err| map_not_found_err(err, id))?;
let target = String::from_utf8(blob.content().to_owned()).map_err(|err| {
BackendError::InvalidUtf8 {
object_type: id.object_type(),
hash: id.hex(),
source: Box::new(err),
}
})?;
let target = str::from_utf8(blob.content())
.map_err(|err| to_invalid_utf8_err(err, id))?
.to_owned();
Ok(target)
}