Skip to main content

Repository

Struct Repository 

Source
pub struct Repository {
    pub git_dir: PathBuf,
    pub work_tree: Option<PathBuf>,
    pub odb: Odb,
    pub explicit_git_dir: bool,
    pub discovery_root: Option<PathBuf>,
    pub work_tree_from_env: bool,
    pub discovery_via_gitfile: bool,
}
Expand description

A handle to an open Git repository.

Fields§

§git_dir: PathBuf

Absolute path to the git directory (.git/ or bare repo root).

§work_tree: Option<PathBuf>

Absolute path to the working tree, or None for bare repos.

§odb: Odb

Loose object database.

§explicit_git_dir: bool

Discovery provenance: true when opened via GIT_DIR env or explicit API.

This suppresses safe.bareRepository implicit checks.

§discovery_root: Option<PathBuf>

When the repo was found by walking from a directory containing .git / a gitfile, that directory (matches Git’s setup trace using .git for the default git-dir).

§work_tree_from_env: bool

GIT_WORK_TREE was set without GIT_DIR and applied after discovery (t1510 #1, #5, …).

§discovery_via_gitfile: bool

.git was a gitfile (not a directory) when the repo was discovered.

Implementations§

Source§

impl Repository

Source

pub fn open(git_dir: &Path, work_tree: Option<&Path>) -> Result<Self>

Open a repository from an explicit git-dir and optional work-tree.

§Errors

Returns Error::NotARepository if git_dir does not look like a valid git directory (missing objects/, HEAD, etc.).

Source

pub fn open_skipping_format_validation( git_dir: &Path, work_tree: Option<&Path>, ) -> Result<Self>

Like Self::open but skips [validate_repository_format].

Used after repository discovery when the format is unsupported so callers still learn the git directory (Git GIT_DIR_INVALID_FORMAT still records gitdir for read_early_config).

Source

pub fn discover(start: Option<&Path>) -> Result<Self>

Discover the repository starting from start (defaults to cwd if None).

Checks GIT_DIR first; if set, uses it directly. Otherwise walks up the directory tree looking for .git (regular directory or gitfile).

§Errors

Returns Error::NotARepository if no repository can be found.

Source

pub fn effective_pathspec_cwd(&self) -> PathBuf

Current directory to use for pathspec / cwd-prefix logic.

When GIT_WORK_TREE points at a directory that does not contain the process cwd (alternate work tree + index from the main repo directory), Git treats pathspecs as relative to the work tree root — use that root as the effective cwd.

Source

pub fn index_path(&self) -> PathBuf

Path to the index file.

Source

pub fn load_index(&self) -> Result<Index>

Load the index, expanding sparse-directory placeholders from the object database.

Commands that operate on individual paths should use this instead of Index::load.

Source

pub fn load_index_at(&self, path: &Path) -> Result<Index>

Like Repository::load_index, but reads from an explicit index file path (e.g. GIT_INDEX_FILE or a worktree-specific index).

Source

pub fn write_index(&self, index: &mut Index) -> Result<()>

Write the index to the default path after optionally collapsing skip-worktree subtrees into sparse-directory placeholders (when sparse index is enabled).

Source

pub fn write_index_at(&self, path: &Path, index: &mut Index) -> Result<()>

Like Repository::write_index, but writes to an explicit index file path.

Source

pub fn refs_dir(&self) -> PathBuf

Path to the refs/ directory.

Source

pub fn head_path(&self) -> PathBuf

Path to HEAD.

Source

pub fn is_bare(&self) -> bool

Whether this is a bare repository (no working tree).

Source

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

Read an object, transparently following replace refs.

If refs/replace/<hex> exists for the requested OID and GIT_NO_REPLACE_OBJECTS is not set, this reads the replacement object instead. Otherwise it behaves identically to self.odb.read(oid).

Source§

impl Repository

Source

pub fn enforce_safe_directory(&self) -> Result<()>

Enforce safe.directory ownership checks, matching upstream behavior.

When GIT_TEST_ASSUME_DIFFERENT_OWNER=1, ownership is considered unsafe unless a matching safe.directory value is configured in system/global/ command scopes (repository-local config is ignored).

Source

pub fn enforce_safe_directory_git_dir(&self) -> Result<()>

Enforce safe.directory checks using the repository git-dir path.

Used by operations that explicitly open another repository by path (e.g. local clone source).

Source

pub fn enforce_safe_directory_git_dir_with_path( &self, checked: &Path, ) -> Result<()>

Enforce safe.directory checks against an explicit checked path.

Source

pub fn verify_safe_for_clone_source(&self) -> Result<()>

Verify the repository is safe to use as a git clone source (local clone).

When GIT_TEST_ASSUME_DIFFERENT_OWNER is set, applies the same safe.directory rules as discovery. Otherwise checks filesystem ownership of the git directory only (matching Git’s die_upon_dubious_ownership for clone).

Trait Implementations§

Source§

impl Debug for Repository

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

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> 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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

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

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

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.