Skip to main content

lattice_vcs/
blob.rs

1use std::path::Path;
2
3use ropey::Rope;
4
5use crate::{Repository, Result};
6
7/// Read the content of a git blob (file at a specific revision).
8pub struct GitBlob;
9
10impl GitBlob {
11    /// Read the blob identified by `oid` and return its content as a
12    /// [`Rope`]. Uses `git cat-file -p <oid>`.
13    pub fn read(repo: &Repository, oid: &gix::ObjectId) -> Result<Rope> {
14        let oid_str = oid.to_string();
15        let text = repo.run_git_str(["cat-file", "-p", &oid_str])?;
16        Ok(Rope::from(text))
17    }
18
19    /// Read the file at `path` from a specific revision (e.g., `"HEAD"`,
20    /// `"main~3"`). Uses `git show <rev>:<path>`.
21    pub fn read_path(repo: &Repository, rev: &str, path: impl AsRef<Path>) -> Result<Rope> {
22        let spec = format!("{}:{}", rev, path.as_ref().display());
23        let text = repo.run_git_str(["show", &spec])?;
24        Ok(Rope::from(text))
25    }
26}