Skip to content

Commit

Permalink
Implement support for fetching raw Git object bytes
Browse files Browse the repository at this point in the history
Summary: Extend the existing admin command to allow fetching the raw bytes of a Git object. This is useful for debugging.

Reviewed By: andreacampi

Differential Revision: D68717363

fbshipit-source-id: 36a39b4d18ff1299dbfb1d0dc7dae85da909f98e
  • Loading branch information
RajivTS authored and facebook-github-bot committed Jan 28, 2025
1 parent 05c453c commit 5f177bb
Showing 1 changed file with 36 additions and 1 deletion.
37 changes: 36 additions & 1 deletion eden/mononoke/tools/admin/src/commands/git_objects/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,19 @@
* GNU General Public License version 2.
*/

use std::fs::File;
use std::io::Write;
use std::path::PathBuf;

use anyhow::Context;
use anyhow::Result;
use clap::Args;
use context::CoreContext;
use git_types::fetch_git_object;
use git_types::fetch_git_object_bytes;
use git_types::fetch_non_blob_git_object;
use git_types::GitIdentifier;
use git_types::HeaderState;
use gix_object::Object::Blob;
use gix_object::Object::Commit;
use gix_object::Object::Tag;
Expand All @@ -26,6 +32,9 @@ pub struct FetchArgs {
/// The Git SHA1 object id (in hex form) of the object that is to be fetched
#[clap(long)]
id: GitSha1,
/// Store the raw bytes of the git object in the specified file instead of printing the parsed object
#[clap(long)]
raw_bytes_file: Option<PathBuf>,
/// The type of the git object to be fetched. Required if the object can be git blob
#[clap(long, requires = "size")]
ty: Option<String>,
Expand All @@ -34,7 +43,16 @@ pub struct FetchArgs {
size: Option<u64>,
}

pub async fn fetch(repo: &Repo, ctx: &CoreContext, mut fetch_args: FetchArgs) -> Result<()> {
pub async fn fetch(repo: &Repo, ctx: &CoreContext, fetch_args: FetchArgs) -> Result<()> {
if fetch_args.raw_bytes_file.is_some() {
fetch_bytes(repo, ctx, fetch_args).await?;
} else {
fetch_object(repo, ctx, fetch_args).await?;
}
Ok(())
}

async fn fetch_object(repo: &Repo, ctx: &CoreContext, mut fetch_args: FetchArgs) -> Result<()> {
let ty = fetch_args.ty.take();
let size = fetch_args.size.take();
let git_object = match (ty, size) {
Expand Down Expand Up @@ -62,3 +80,20 @@ pub async fn fetch(repo: &Repo, ctx: &CoreContext, mut fetch_args: FetchArgs) ->
};
Ok(())
}

async fn fetch_bytes(repo: &Repo, ctx: &CoreContext, fetch_args: FetchArgs) -> Result<()> {
let git_object_bytes = fetch_git_object_bytes(
ctx,
repo.repo_blobstore.clone(),
&GitIdentifier::Basic(fetch_args.id),
HeaderState::Included,
)
.await?;
let mut file = File::create(
fetch_args
.raw_bytes_file
.ok_or_else(|| anyhow::anyhow!("Path is required for writing raw output bytes"))?,
)?;
file.write_all(&git_object_bytes)?;
Ok(())
}

0 comments on commit 5f177bb

Please sign in to comment.