Skip to main content

subcog/context/
mod.rs

1//! Context detection for git repositories.
2//!
3//! This module provides automatic detection of project context from git repositories,
4//! including project identification, branch names, and file paths.
5//!
6//! # Overview
7//!
8//! The context detector uses git2 to discover repository information from the
9//! current working directory or a specified path. It handles edge cases gracefully:
10//!
11//! - Non-git directories (returns `GitContext` with all fields `None`)
12//! - Detached HEAD state (branch is `None`)
13//! - Git worktrees (correctly identifies the main repository)
14//! - Credentials in remote URLs (automatically sanitized)
15//!
16//! # Example
17//!
18//! ```rust,ignore
19//! use subcog::context::GitContext;
20//!
21//! // Detect from current working directory
22//! let ctx = GitContext::from_cwd();
23//! if let Some(project) = ctx.project_id {
24//!     println!("Project: {project}");
25//! }
26//!
27//! // Detect from specific path
28//! let ctx = GitContext::from_path("/path/to/repo");
29//! println!("Branch: {:?}", ctx.branch);
30//! ```
31//!
32//! # Security
33//!
34//! Remote URLs are automatically sanitized to remove credentials:
35//!
36//! | Input | Output |
37//! |-------|--------|
38//! | `https://user:pass@github.com/org/repo` | `github.com/org/repo` |
39//! | `git@github.com:org/repo.git` | `github.com/org/repo` |
40//! | `https://github.com/org/repo.git` | `github.com/org/repo` |
41
42mod detector;
43
44pub use detector::GitContext;