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: PathBufAbsolute 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: OdbLoose object database.
explicit_git_dir: boolDiscovery 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: boolGIT_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
impl Repository
Sourcepub fn open(git_dir: &Path, work_tree: Option<&Path>) -> Result<Self>
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.).
Sourcepub fn open_skipping_format_validation(
git_dir: &Path,
work_tree: Option<&Path>,
) -> Result<Self>
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).
Sourcepub fn discover(start: Option<&Path>) -> Result<Self>
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.
Sourcepub fn effective_pathspec_cwd(&self) -> PathBuf
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.
Sourcepub fn index_path(&self) -> PathBuf
pub fn index_path(&self) -> PathBuf
Path to the index file.
Sourcepub fn load_index(&self) -> Result<Index>
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.
Sourcepub fn load_index_at(&self, path: &Path) -> Result<Index>
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).
Sourcepub fn write_index(&self, index: &mut Index) -> Result<()>
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).
Sourcepub fn write_index_at(&self, path: &Path, index: &mut Index) -> Result<()>
pub fn write_index_at(&self, path: &Path, index: &mut Index) -> Result<()>
Like Repository::write_index, but writes to an explicit index file path.
Sourcepub fn read_replaced(&self, oid: &ObjectId) -> Result<Object>
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
impl Repository
Sourcepub fn enforce_safe_directory(&self) -> Result<()>
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).
Sourcepub fn enforce_safe_directory_git_dir(&self) -> Result<()>
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).
Sourcepub fn enforce_safe_directory_git_dir_with_path(
&self,
checked: &Path,
) -> Result<()>
pub fn enforce_safe_directory_git_dir_with_path( &self, checked: &Path, ) -> Result<()>
Enforce safe.directory checks against an explicit checked path.
Sourcepub fn verify_safe_for_clone_source(&self) -> Result<()>
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).