pub struct GitContext {
pub project_id: Option<String>,
pub branch: Option<String>,
pub file_path: Option<String>,
}Expand description
Git repository context information.
Provides project identification, branch name, and optional file context derived from a git repository. All fields are optional to handle non-git directories and edge cases gracefully.
§Examples
use subcog::context::GitContext;
let ctx = GitContext::from_cwd();
match (&ctx.project_id, &ctx.branch) {
(Some(project), Some(branch)) => {
println!("Working on {project} @ {branch}");
}
(Some(project), None) => {
println!("Working on {project} (detached HEAD)");
}
(None, _) => {
println!("Not in a git repository");
}
}Fields§
§project_id: Option<String>Project identifier derived from git remote URL or repository directory name.
Format: org/repo from remote URL, or just the directory name if no remote.
Credentials are stripped from URLs for security.
branch: Option<String>Current branch name.
None if in detached HEAD state or if HEAD is unborn (empty repository).
file_path: Option<String>Optional file path context.
Can be set to provide file-specific context for operations.
Implementations§
Source§impl GitContext
impl GitContext
Sourcepub fn from_cwd() -> Self
pub fn from_cwd() -> Self
Detects git context from the current working directory.
Uses git2::Repository::discover() to find the repository root,
traversing parent directories if necessary.
§Returns
A GitContext with detected values. If not in a git repository,
all fields will be None.
§Examples
use subcog::context::GitContext;
let ctx = GitContext::from_cwd();
if ctx.project_id.is_some() {
println!("In a git repository");
}Sourcepub fn from_path(path: &Path) -> Self
pub fn from_path(path: &Path) -> Self
Detects git context from a specific path.
Uses git2::Repository::discover() to find the repository containing
the given path, traversing parent directories if necessary.
§Arguments
path- The path to detect context from. Can be any path within a repository.
§Returns
A GitContext with detected values. If the path is not in a git repository,
all fields will be None.
§Examples
use subcog::context::GitContext;
use std::path::Path;
let ctx = GitContext::from_path(Path::new("/path/to/repo/subdir"));
println!("Project: {:?}", ctx.project_id);Sourcepub fn with_file_path(self, file_path: impl Into<String>) -> Self
pub fn with_file_path(self, file_path: impl Into<String>) -> Self
Creates a new GitContext with the specified file path.
This is useful for adding file-specific context to an existing detection.
§Arguments
file_path- The file path to associate with this context.
§Examples
use subcog::context::GitContext;
let ctx = GitContext::from_cwd()
.with_file_path("src/main.rs");Sourcepub const fn is_git_repo(&self) -> bool
pub const fn is_git_repo(&self) -> bool
Returns true if this context represents a git repository.
A context is considered to be in a git repository if it has a project ID.
Sourcepub const fn is_detached(&self) -> bool
pub const fn is_detached(&self) -> bool
Returns true if the repository is in detached HEAD state.
Returns false if not in a git repository.
Trait Implementations§
Source§impl Clone for GitContext
impl Clone for GitContext
Source§fn clone(&self) -> GitContext
fn clone(&self) -> GitContext
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for GitContext
impl Debug for GitContext
Source§impl Default for GitContext
impl Default for GitContext
Source§fn default() -> GitContext
fn default() -> GitContext
Source§impl PartialEq for GitContext
impl PartialEq for GitContext
impl Eq for GitContext
impl StructuralPartialEq for GitContext
Auto Trait Implementations§
impl Freeze for GitContext
impl RefUnwindSafe for GitContext
impl Send for GitContext
impl Sync for GitContext
impl Unpin for GitContext
impl UnwindSafe for GitContext
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.§impl<T> FutureExt for T
impl<T> FutureExt for T
§fn with_context(self, otel_cx: Context) -> WithContext<Self>
fn with_context(self, otel_cx: Context) -> WithContext<Self>
§fn with_current_context(self) -> WithContext<Self>
fn with_current_context(self) -> WithContext<Self>
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more§impl<T> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::Request§impl<L> LayerExt<L> for L
impl<L> LayerExt<L> for L
§fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
fn named_layer<S>(&self, service: S) -> Layered<<L as Layer<S>>::Service, S>where
L: Layer<S>,
Layered].