pub struct GitContext { /* private fields */ }Expand description
Represents a directory and selected commit within a Git repository.
Implementations§
Source§impl GitContext
impl GitContext
Sourcepub fn create(
path: impl AsRef<Path>,
committish: Option<&str>,
engine: GitEngine,
) -> Result<Self>
pub fn create( path: impl AsRef<Path>, committish: Option<&str>, engine: GitEngine, ) -> Result<Self>
Discovers a repository from path or one of its ancestors.
Git’s own discovery is used, so linked worktrees and textual .git files are
supported. A path outside a repository produces a usable GitContextKind::NoRepository
context. Bare repositories are rejected because version calculation requires a
working tree.
Sourcepub fn create_with_environment(
path: impl AsRef<Path>,
committish: Option<&str>,
default_engine: GitEngine,
) -> Result<Self>
pub fn create_with_environment( path: impl AsRef<Path>, committish: Option<&str>, default_engine: GitEngine, ) -> Result<Self>
Creates a context using the effective engine selected by environment policy.
Sourcepub fn kind(&self) -> GitContextKind
pub fn kind(&self) -> GitContextKind
Gets the kind of context that was created.
Sourcepub fn is_repository(&self) -> bool
pub fn is_repository(&self) -> bool
Gets whether an actual repository backs this context.
Sourcepub fn working_tree_path(&self) -> &Path
pub fn working_tree_path(&self) -> &Path
Gets the absolute root of the working tree.
Sourcepub fn git_directory(&self) -> Option<&Path>
pub fn git_directory(&self) -> Option<&Path>
Gets the repository metadata directory. In a linked worktree this is the
worktree-specific Git directory, not necessarily <working tree>/.git.
Sourcepub fn repo_relative_project_directory(&self) -> &Path
pub fn repo_relative_project_directory(&self) -> &Path
Gets the directory selected for version calculation, relative to the working tree.
Sourcepub fn set_repo_relative_project_directory(
&mut self,
directory: impl AsRef<Path>,
) -> Result<()>
pub fn set_repo_relative_project_directory( &mut self, directory: impl AsRef<Path>, ) -> Result<()>
Selects the directory used by higher version-file layers.
Sourcepub fn absolute_project_directory(&self) -> PathBuf
pub fn absolute_project_directory(&self) -> PathBuf
Gets the absolute directory selected for version calculation.
Sourcepub fn repository(&self) -> Option<&Repository>
pub fn repository(&self) -> Option<&Repository>
Exposes the repository to the future version-file and history layers.
Sourcepub fn git_commit_id(&self) -> Option<Oid>
pub fn git_commit_id(&self) -> Option<Oid>
Gets the selected commit ID.
Sourcepub fn git_commit_dates(&self) -> Result<Option<CommitDates>>
pub fn git_commit_dates(&self) -> Result<Option<CommitDates>>
Gets the selected commit’s author and committer timestamps.
Sourcepub fn head_canonical_name(&self) -> Result<Option<String>>
pub fn head_canonical_name(&self) -> Result<Option<String>>
Gets the canonical name of HEAD, such as refs/heads/main.
A detached HEAD is reported as HEAD; an unborn repository has no name.
Gets canonical tag names that resolve to the selected commit.
Annotated tags are peeled before comparison.
Sourcepub fn is_shallow(&self) -> bool
pub fn is_shallow(&self) -> bool
Gets whether the repository is shallow.
libgit2 checks the common repository directory, which also handles linked worktrees correctly.
Sourcepub fn is_working_tree_dirty(&self) -> Result<bool>
pub fn is_working_tree_dirty(&self) -> Result<bool>
Gets whether tracked, untracked, or staged working-tree changes exist.
Sourcepub fn is_ignored(&self, path: impl AsRef<Path>) -> Result<bool>
pub fn is_ignored(&self, path: impl AsRef<Path>) -> Result<bool>
Determines whether Git would ignore path.
Sourcepub fn try_select_commit(&mut self, committish: &str) -> Result<bool>
pub fn try_select_commit(&mut self, committish: &str) -> Result<bool>
Attempts to select a commit using any revparse-compatible committish.
Tags (including annotated tags), local and remote branches, abbreviated IDs,
and first-parent expressions such as HEAD~2 are accepted.
Sourcepub fn apply_tag(&self, name: &str) -> Result<Oid>
pub fn apply_tag(&self, name: &str) -> Result<Oid>
Creates a lightweight tag at the selected commit.
Sourcepub fn stage(&self, path: impl AsRef<Path>) -> Result<()>
pub fn stage(&self, path: impl AsRef<Path>) -> Result<()>
Adds a path to the index and writes the updated index.
Sourcepub fn short_unique_commit_id(&self, min_length: usize) -> Result<String>
pub fn short_unique_commit_id(&self, min_length: usize) -> Result<String>
Gets the shortest object ID of at least min_length characters that uniquely
identifies the selected commit in this object database.
Sourcepub fn short_unique_id(
&self,
commit_id: Oid,
min_length: usize,
) -> Result<String>
pub fn short_unique_id( &self, commit_id: Oid, min_length: usize, ) -> Result<String>
Gets a shortest unique ID for an arbitrary commit.
Sourcepub fn default_branch(&self) -> Result<String>
pub fn default_branch(&self) -> Result<String>
Detects the repository’s default branch using remote HEADs, local configuration, and conventional names, in that order.