Git Provider¶
codebrief.tools.git_provider
¶
Git Context Provider for CodeBrief.
This module provides functions to extract Git context information from a project repository, including current branch, status, uncommitted changes, and recent commits. It uses subprocess calls to interact with Git and formats the output as structured Markdown suitable for inclusion in context bundles.
Core functionalities: - Extract current branch name - Get Git status summary - List uncommitted changes with file status - Retrieve recent commit history - Support for optional diff output (controlled by parameters) - Graceful handling of non-Git repositories - Structured Markdown output with clear sectioning
Functions¶
get_git_context(project_root, diff_options=None, log_count=5, full_diff=False)
¶
Extract Git context information from a project repository.
This function executes various Git commands to gather comprehensive context about the repository state, including branch information, file changes, and commit history. All output is formatted as structured Markdown.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
project_root | Path | The root directory of the Git repository | required |
diff_options | Optional[str] | Optional string for advanced git diff options (e.g., "--stat", "src/myfile.py") | None |
log_count | int | Number of recent commits to include (default: 5) | 5 |
full_diff | bool | If True, include full diff output (default: False) | False |
Returns:
Type | Description |
---|---|
str | Formatted Markdown string containing Git context information |
Note
No exceptions are raised; all errors are captured and included in the output
Source code in src/codebrief/tools/git_provider.py
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
|
Overview¶
The git_provider
module provides comprehensive Git repository context extraction functionality. It safely executes Git commands to gather repository information including branch status, commit history, and change diffs.
Key Features¶
- Safe Git Command Execution: Robust subprocess handling with timeout protection
- Comprehensive Context: Branch info, status, commits, and diffs
- Error Resilience: Graceful handling of non-Git repos and command failures
- Security Compliant: Bandit-approved subprocess usage for Git operations
- Configurable Output: Flexible diff options and commit count limits
Functions¶
get_git_context¶
Extract complete Git repository context information.
Parameters: - project_root
(Path): Root directory of the project/repository - log_count
(int, optional): Number of recent commits to include (default: 10) - full_diff
(bool, optional): Include full diff of uncommitted changes (default: False) - diff_options
(str, optional): Custom git diff options (default: None)
Returns: - str
: Formatted Markdown string containing Git context
Raises: - No exceptions raised - all errors are handled gracefully and returned as informative messages
Usage Examples¶
Basic Usage¶
from pathlib import Path
from codebrief.tools.git_provider import get_git_context
# Get basic Git context
context = get_git_context(Path("."))
print(context)
Advanced Usage¶
# Get detailed context with full diff
context = get_git_context(
project_root=Path("/path/to/repo"),
log_count=5,
full_diff=True,
diff_options="--stat --color=never"
)
CLI Integration¶
# This is how the CLI command uses the function
from codebrief.tools.git_provider import get_git_context
def git_info_command(
root_dir: Path,
output: Optional[Path] = None,
log_count: int = 10,
full_diff: bool = False,
diff_options: Optional[str] = None,
):
context = get_git_context(
project_root=root_dir,
log_count=log_count,
full_diff=full_diff,
diff_options=diff_options,
)
if output:
output.write_text(context, encoding="utf-8")
else:
console.print(context)
Output Format¶
The function returns a structured Markdown document:
# Git Context
## Repository Information
- **Current Branch:** main
- **Repository Status:** Clean working directory
## Recent Commits (Last 10)
1. **feat: add new feature** (2024-01-15 14:30:25)
- Author: Developer <dev@example.com>
- Hash: abc123f
2. **fix: resolve parsing issue** (2024-01-14 09:15:42)
- Author: Developer <dev@example.com>
- Hash: def456a
## Uncommitted Changes
*No uncommitted changes*
## Full Diff
[Included when full_diff=True]
## Diff (--stat)
[Included when diff_options provided]
Error Handling¶
The module handles various error scenarios gracefully:
Non-Git Repository¶
context = get_git_context(Path("/tmp"))
# Returns: "# Git Context\n\nNot a Git repository or no Git history.\n"
Git Not Available¶
Permission Issues¶
Security Considerations¶
- All subprocess calls use explicit command arrays (not shell strings)
- Timeout protection prevents hanging on slow Git operations
- Input validation ensures safe path handling
- Bandit security compliance with appropriate
# nosec
annotations
Testing¶
The module includes comprehensive test coverage:
- 13 test cases covering all functionality
- Error scenario testing for robustness
- Integration testing with real Git repositories
- Parameter validation testing
- Security compliance verification
Configuration Integration¶
Works seamlessly with codebrief's configuration system:
Dependencies¶
- subprocess: For Git command execution
- pathlib: For path handling
- typing: For type annotations
Related Modules¶
bundler
: Uses git_provider for bundle Git sectionsconfig_manager
: Provides configuration defaultsmain
: CLI integration and command handling