Skip to content

Commit

Permalink
git_types: add object id method for GitIdentifier
Browse files Browse the repository at this point in the history
Summary: Add a method to get the object id from a Git identifier without having to match.

Reviewed By: RajivTS

Differential Revision: D64604406

fbshipit-source-id: f4817fc830c08e1b137f0ddb99514a6c55f523e7
  • Loading branch information
markbt authored and facebook-github-bot committed Oct 19, 2024
1 parent 0881b3b commit 8511fbb
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
11 changes: 9 additions & 2 deletions eden/mononoke/git/git_types/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use filestore::fetch_with_size;
use filestore::hash_bytes;
use filestore::Sha1IncrementalHasher;
use futures::TryStreamExt;
use gix_hash::ObjectId;
use gix_object::WriteTo;
use mononoke_types::hash::GitSha1;
use mononoke_types::hash::RichGitSha1;
Expand Down Expand Up @@ -117,7 +118,7 @@ where
{
// In git, empty tree is a special object: it's present in every git repo and not persisted in
// the storage.
if git_hash == gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1) {
if git_hash == ObjectId::empty_tree(gix_hash::Kind::Sha1) {
return Ok(gix_object::Object::Tree(gix_object::Tree::empty()));
}
let raw_bytes = fetch_non_blob_git_object_bytes(ctx, blobstore, git_hash).await?;
Expand Down Expand Up @@ -163,6 +164,13 @@ impl GitIdentifier {
GitIdentifier::Basic(_) => true, // May or may not be a blob, we can't know
}
}

pub fn to_object_id(&self) -> Result<ObjectId> {
match self {
GitIdentifier::Rich(rich_sha) => Ok(rich_sha.to_object_id()?),
GitIdentifier::Basic(basic_sha) => Ok(basic_sha.to_object_id()?),
}
}
}

async fn maybe_fetch_blob_bytes(
Expand Down Expand Up @@ -330,7 +338,6 @@ mod test {
use fbinit::FacebookInit;
use filestore::FilestoreConfig;
use fixtures::TestRepoFixture;
use gix_hash::ObjectId;
use gix_object::Object;
use gix_object::Tag;
use mononoke_macros::mononoke;
Expand Down
5 changes: 1 addition & 4 deletions eden/mononoke/git/protocol/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,7 @@ pub(crate) enum ObjectIdentifierType {
impl ObjectIdentifierType {
pub(crate) fn to_object_id(&self) -> Result<ObjectId> {
match self {
Self::AllObjects(ident) => match ident {
GitIdentifier::Basic(sha) => sha.to_object_id(),
GitIdentifier::Rich(rich_sha) => rich_sha.to_object_id(),
},
Self::AllObjects(ident) => ident.to_object_id(),
Self::NonBlobObjects(oid) => Ok(*oid),
}
}
Expand Down

0 comments on commit 8511fbb

Please sign in to comment.