Git Operations
Thegit_operations tool provides structured Git operations with comprehensive input sanitization and workspace scoping.
Supported operations:
status— Show working tree statusdiff— Show file differenceslog— View commit historybranch— List branchescommit— Create commitadd— Stage filescheckout— Switch branchesstash— Stash changes
- Command injection prevention
- Workspace path scoping with
cwdoverride - Commit message truncation
- Branch name validation
Parameters
string
required
Git operation:
status, diff, log, branch, commit, add, checkout, or stashstring
Commit message (required for
commit operation)string
File paths to stage (required for
add operation)string
Branch name (required for
checkout operation)string
Files to diff (defaults to
. for all files)boolean
Show staged changes for
diff operation (default: false)integer
Number of log entries (default: 10, max: 1000)
string
Repository directory (absolute path within allowed paths; defaults to workspace)
Configuration
Operations
status
Show working tree status in porcelain v2 format. Usage:diff
Show file differences. Usage (unstaged changes):log
View commit history. Usage:hash|author|email|date|message
Command:
branch
List branches with current branch indicator. Usage:branch_name|* (asterisk indicates current branch)
Command:
commit
Create a commit with staged changes. Usage:- Messages truncated to 2000 bytes
- UTF-8 boundary-aware truncation
- Prevents extremely long commit messages
add
Stage files for commit. Usage:-- separator prevents path injection
checkout
Switch to a different branch. Usage:Branch names are validated to prevent injection:
- Blocked:
;,|,`,$() - Allowed:
a-z,A-Z,0-9,/,-,_,.
stash
Stash uncommitted changes. Push/save stash:push
Command Sanitization
All string arguments are sanitized before passing to Git to prevent command injection.Blocked Patterns
Dangerous git options:--exec=cmd— Execute arbitrary command--upload-pack=cmd— Remote command execution--receive-pack=cmd— Remote command execution--pager=cmd— Pager command injection--editor=cmd— Editor command injection-c key=value— Config injection--no-verify— Bypass hooks
;— Command separator|— Pipe><— Redirection`— Backtick execution$()— Command substitution
Examples
Blocked: Config injectionWorking Directory Control
Default: workspace_dir
By default, git operations run inworkspace_dir:
git status in /workspace
Custom cwd (within workspace)
git log in /workspace/subproject
Custom cwd (outside workspace)
Requiresallowed_paths configuration:
cwd Validation Rules
cwdmust be an absolute pathcwdmust be withinworkspace_dirorallowed_pathscwdis resolved withrealpathAlloc()to prevent symlink escapes- Relative
cwdis rejected: “cwd must be an absolute path”
Error Handling
Repository Not Found
Nothing to Commit
Branch Not Found
No Files to Add
Use Cases
Check Status Before Commit
Stage and Commit Changes
View Recent Commits
Switch Branch
Stash Before Switching
Review Staged Changes
Source
src/tools/git.zig:10-335
Testing
Run git tool tests:- All operations (status, diff, log, branch, commit, add, checkout, stash)
- Command sanitization (injection prevention)
- Working directory validation
- Error cases
- Commit message truncation
- Branch name validation