Skip to main content

Git Operations

The git_operations tool provides structured Git operations with comprehensive input sanitization and workspace scoping. Supported operations:
  • status — Show working tree status
  • diff — Show file differences
  • log — View commit history
  • branch — List branches
  • commit — Create commit
  • add — Stage files
  • checkout — Switch branches
  • stash — Stash changes
Security features:
  • Command injection prevention
  • Workspace path scoping with cwd override
  • Commit message truncation
  • Branch name validation

Parameters

string
required
Git operation: status, diff, log, branch, commit, add, checkout, or stash
string
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:
Response:
Command:

diff

Show file differences. Usage (unstaged changes):
Response:
Usage (staged changes):
Command:

log

View commit history. Usage:
Response:
Format: hash|author|email|date|message Command:

branch

List branches with current branch indicator. Usage:
Response:
Format: branch_name|* (asterisk indicates current branch) Command:

commit

Create a commit with staged changes. Usage:
Response:
Command:
Commit message truncation:
  • Messages truncated to 2000 bytes
  • UTF-8 boundary-aware truncation
  • Prevents extremely long commit messages
Example: Long message
Truncated at UTF-8 boundary near 2000 bytes.
Empty commit messages are rejected: “Commit message cannot be empty”

add

Stage files for commit. Usage:
Response:
Multiple files:
Response:
Command:
Note: -- separator prevents path injection

checkout

Switch to a different branch. Usage:
Response:
Command:
Branch name validation:
Branch names are validated to prevent injection:
  • Blocked: ;, |, `, $()
  • Allowed: a-z, A-Z, 0-9, /, -, _, .
Example: Blocked injection
Response:

stash

Stash uncommitted changes. Push/save stash:
Response:
Pop stash:
Response:
List stashes:
Response:
Default action: 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
Shell metacharacters:
  • ; — Command separator
  • | — Pipe
  • > < — Redirection
  • ` — Backtick execution
  • $() — Command substitution

Examples

Blocked: Config injection
Response:
Blocked: Command substitution
Response:
Allowed: Normal paths
Allowed

Working Directory Control

Default: workspace_dir

By default, git operations run in workspace_dir:
Runs git status in /workspace

Custom cwd (within workspace)

Runs git log in /workspace/subproject

Custom cwd (outside workspace)

Requires allowed_paths configuration:
Allowed (in allowed_paths)
Blocked:

cwd Validation Rules

  • cwd must be an absolute path
  • cwd must be within workspace_dir or allowed_paths
  • cwd is resolved with realpathAlloc() to prevent symlink escapes
  • Relative cwd is rejected: “cwd must be an absolute path”

Error Handling

Repository Not Found

Response:

Nothing to Commit

Response:

Branch Not Found

Response:

No Files to Add

Response:

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:
Tests cover:
  • 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