Skip to main content

Objects

Struct Objects 

Source
pub struct Objects<'repo> { /* private fields */ }
Expand description

Read and write Git objects through a Repository handle.

Implementations§

Source§

impl<'repo> Objects<'repo>

Source

pub fn new(repo: &'repo Repository) -> Self

Bind object operations to repo.

Source

pub fn repository(&self) -> &'repo Repository

Underlying repository.

Source

pub fn contains(&self, oid: &ObjectId) -> Result<bool>

Returns whether oid is present in this repository’s object database.

§Errors

Propagates a backend failure when no object source has oid (see crate::odb::Odb::contains) instead of reporting it as missing.

§Examples
use grit_lib::objects::ObjectKind;
use grit_lib::repo::{InitOptions, Repository};
let dir = std::env::temp_dir().join("grit-objects-handle-doc");
let repo = Repository::init(&dir, &InitOptions::default())?;
let oid = repo.objects().write(ObjectKind::Blob, b"hello")?;
assert!(repo.objects().contains(&oid)?);
Source

pub fn read(&self, oid: &ObjectId) -> Result<Object>

Read the object named oid.

§Errors

Returns crate::error::Error::ObjectNotFound when the object is missing.

§Examples
use grit_lib::objects::ObjectKind;
use grit_lib::repo::{InitOptions, Repository};
let dir = std::env::temp_dir().join("grit-objects-read-doc");
let repo = Repository::init(&dir, &InitOptions::default())?;
let oid = repo.objects().write(ObjectKind::Blob, b"payload")?;
let obj = repo.objects().read(&oid)?;
assert_eq!(obj.data, b"payload");
Source

pub fn read_info(&self, oid: &ObjectId) -> Result<ObjectInfo>

Read object type and size without inflating full blob bodies when possible.

§Errors

Propagates object lookup failures from the ODB.

Source

pub fn read_commit(&self, oid: &ObjectId) -> Result<CommitData>

Read oid and parse it as a commit (annotated tags are not followed).

§Errors

See crate::objects::odb::Odb::read_commit: a missing object, an object that is not a commit (crate::error::Error::UnexpectedObjectKind), or a malformed commit.

§Examples
use grit_lib::objects::ObjectKind;
use grit_lib::repo::{InitOptions, Repository};
let dir = std::env::temp_dir().join("grit-objects-read-commit-doc");
let repo = Repository::init(&dir, &InitOptions::default())?;
let tree = repo.objects().write(ObjectKind::Tree, b"")?;
let body = format!(
    "tree {tree}\nauthor A <a@example.com> 0 +0000\ncommitter A <a@example.com> 0 +0000\n\nfirst\n"
);
let commit = repo.objects().write(ObjectKind::Commit, body.as_bytes())?;
assert_eq!(repo.objects().read_commit(&commit)?.message, "first\n");
assert_eq!(repo.objects().commit_tree(&commit)?, tree);
assert!(repo.objects().read_commit(&tree).is_err());
Source

pub fn commit_tree(&self, oid: &ObjectId) -> Result<ObjectId>

The root tree of the commit oid, decoding only the commit headers.

§Errors

Same as Self::read_commit.

Source

pub fn hash(&self, kind: ObjectKind, data: &[u8]) -> Result<ObjectId>

Hash data as a Git object of kind without writing to disk.

§Errors

Returns crate::error::Error::HashCollision when sha1dc detects a chosen-prefix collision.

Source

pub fn write(&self, kind: ObjectKind, data: &[u8]) -> Result<ObjectId>

Validate and store an object, returning its id.

Tree, commit and tag payloads must pass the same checks as git hash-object -t <kind> (without --literally): sorted, duplicate-free tree entries with allowed modes and names, and well-formed tree/parent/author/committer/tagger headers. Blobs are stored as given. To build trees and commits from typed values instead of raw bytes, use Self::write_tree and Self::write_commit.

§Errors
§Examples

See also the guide_objects example program.

Source

pub fn write_literally(&self, kind: ObjectKind, data: &[u8]) -> Result<ObjectId>

Store data as an object of kind without validating its structure.

The equivalent of git hash-object --literally: use it only to reproduce or test corrupt objects. Repositories containing such objects fail git fsck.

§Errors

crate::error::Error::HashCollision and zlib or filesystem errors from the ODB.

Source

pub fn write_tree( &self, entries: impl IntoIterator<Item = TreeEntry>, ) -> Result<ObjectId>

Write a tree object from entries, given in any order.

Entries are sorted into Git’s tree order. Each entry’s object must exist in this repository with the kind implied by its mode (tree or blob; gitlink 160000 entries may reference a commit that is not stored), matching git mktree without --missing. Use Self::write_tree_with with WriteTreeOptions::allow_missing when building trees bottom-up or referencing objects a partial clone has not fetched.

§Errors
§Examples
use grit_lib::objects::{ObjectKind, TreeEntry};
use grit_lib::repo::{InitOptions, Repository};
let dir = tempfile::tempdir()?;
let repo = Repository::init(dir.path(), &InitOptions::new().with_bare(true))?;
let blob = repo.objects().write(ObjectKind::Blob, b"hello\n")?;
let entry = |name: &str| TreeEntry { mode: 0o100644, name: name.into(), oid: blob };
let tree = repo.objects().write_tree([entry("b"), entry("a")])?;
let names: Vec<_> = grit_lib::objects::parse_tree(&repo.objects().read(&tree)?.data)?
    .into_iter()
    .map(|e| e.name)
    .collect();
assert_eq!(names, [b"a".to_vec(), b"b".to_vec()]);
Source

pub fn write_tree_with( &self, entries: impl IntoIterator<Item = TreeEntry>, options: WriteTreeOptions, ) -> Result<ObjectId>

Write a tree object from entries with options.

Same validation and sorting as Self::write_tree, except that missing blob and tree oids are accepted when WriteTreeOptions::allow_missing is set.

§Errors

Same as Self::write_tree.

Source

pub fn write_commit(&self, commit: &NewCommit) -> Result<ObjectId>

Write the commit described by commit and return its id.

The tree must exist and be a tree, and every parent must exist and be a commit (the checks git commit-tree makes). When grit.changeIds is enabled (the default) and no id was set with NewCommit::with_change_id, the commit gets a fresh change-id header. The commit is not signed and no ref is updated; use crate::porcelain::commit::create_commit to commit the index onto a branch.

§Errors
§Examples

See NewCommit.

Trait Implementations§

Source§

impl<'repo> Clone for Objects<'repo>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'repo> Copy for Objects<'repo>

Source§

impl<'repo> Debug for Objects<'repo>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'repo> !RefUnwindSafe for Objects<'repo>

§

impl<'repo> !UnwindSafe for Objects<'repo>

§

impl<'repo> Freeze for Objects<'repo>

§

impl<'repo> Send for Objects<'repo>

§

impl<'repo> Sync for Objects<'repo>

§

impl<'repo> Unpin for Objects<'repo>

§

impl<'repo> UnsafeUnpin for Objects<'repo>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.