backend: return CopyId along with CopyHistory

Constructing a CopyId for a given CopyHistory is backend-specific.
However, most methods of getting a CopyHistory require you to have the
CopyId already, so it's usually not necessary to create an ID from
scratch.

`get_related_copies()` in the Backend trait is an exception though.
Since it's not widely in use yet, let's change it to also return the
CopyId for each related CopyHistory.
This commit is contained in:
Josh Steadmon 2025-07-22 15:43:37 -07:00
parent 1e521f564d
commit 149de52a86
6 changed files with 42 additions and 10 deletions

View file

@ -174,7 +174,10 @@ impl Backend for JitBackend {
self.inner.write_copy(contents).await
}
async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>> {
async fn get_related_copies(
&self,
copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>> {
self.inner.get_related_copies(copy_id).await
}

View file

@ -474,7 +474,10 @@ pub trait Backend: Any + Send + Sync + Debug {
///
/// Backends that don't support copy tracking may return
/// `BackendError::Unsupported`.
async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>>;
async fn get_related_copies(
&self,
copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>>;
async fn read_tree(&self, path: &RepoPath, id: &TreeId) -> BackendResult<Tree>;

View file

@ -1067,7 +1067,10 @@ impl Backend for GitBackend {
))
}
async fn get_related_copies(&self, _copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>> {
async fn get_related_copies(
&self,
_copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>> {
Err(BackendError::Unsupported(
"The Git backend doesn't support tracked copies yet".to_string(),
))

View file

@ -168,7 +168,10 @@ impl Backend for SecretBackend {
))
}
async fn get_related_copies(&self, _copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>> {
async fn get_related_copies(
&self,
_copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>> {
Err(BackendError::Unsupported(
"The secret backend doesn't support copies".to_string(),
))

View file

@ -255,7 +255,10 @@ impl Backend for SimpleBackend {
))
}
async fn get_related_copies(&self, _copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>> {
async fn get_related_copies(
&self,
_copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>> {
Err(BackendError::Unsupported(
"The simple backend doesn't support copies".to_string(),
))

View file

@ -306,7 +306,10 @@ impl Backend for TestBackend {
.await
}
async fn get_related_copies(&self, copy_id: &CopyId) -> BackendResult<Vec<CopyHistory>> {
async fn get_related_copies(
&self,
copy_id: &CopyId,
) -> BackendResult<Vec<(CopyId, CopyHistory)>> {
let copy_id = copy_id.clone();
self.run_async(move |data| {
let copies = &data.copies;
@ -328,7 +331,7 @@ impl Backend for TestBackend {
)
.unwrap()
{
histories.push(copies.get(id).unwrap().clone());
histories.push((id.clone(), copies.get(id).unwrap().clone()));
}
Ok(histories)
})
@ -466,8 +469,22 @@ mod tests {
// Looking up by any id returns the related copies in the same order (children
// before parents)
let related = backend.get_related_copies(&copy1_id).block_on().unwrap();
assert_eq!(related, vec![copy3.clone(), copy2.clone(), copy1.clone()]);
let related: Vec<CopyHistory> = backend.get_related_copies(&copy3_id).block_on().unwrap();
assert_eq!(related, vec![copy3.clone(), copy2.clone(), copy1.clone()]);
assert_eq!(
related,
vec![
(copy3_id.clone(), copy3.clone()),
(copy2_id.clone(), copy2.clone()),
(copy1_id.clone(), copy1.clone())
]
);
let related = backend.get_related_copies(&copy3_id).block_on().unwrap();
assert_eq!(
related,
vec![
(copy3_id.clone(), copy3.clone()),
(copy2_id.clone(), copy2.clone()),
(copy1_id.clone(), copy1.clone())
]
);
}
}