Expand description
GitHub context extraction from Bash tool events.
This module parses Bash commands and their output to extract GitHub issue/PR references and worktree information. This provides more accurate context than branch name parsing alone, capturing:
- Explicit
gh issue/prcommands - GitHub URLs in command output (e.g., from
gh pr create) Fixes #Nreferences in PR bodies- Git worktree operations
§Fields Populated
| Field | Description | Sources |
|---|---|---|
github_issue | Issue number | gh commands, URLs, PR body |
github_pr | PR number | gh commands, URLs |
github_repo | Repository (owner/repo) | gh --repo flag, GitHub URLs |
git_worktree_path | Worktree directory path | git worktree add, git -C, cd |
git_branch | Current branch | Set when capturing PR from gh pr create |
§GitHub CLI Command Parsing
The parse_gh_command function parses gh CLI commands to extract explicit
issue/PR references.
§Supported Commands
gh issue {view,checks,diff,merge,close,edit,reopen,comment} <number> [--repo owner/repo]
gh pr {view,checks,diff,merge,close,edit,reopen,comment} <number> [--repo owner/repo]Regex: gh\s+(issue|pr)\s+(view|checks|diff|merge|close|edit|reopen|comment)\s+(\d+)(?:.*--repo\s+([^\s]+))?
§PR Body References
When gh pr create --body "..." is detected, the body is scanned for issue references:
Closes #N
Fixes #N
Resolves #NRegex (case-insensitive): (?i)\b(closes?|fixes?|resolves?)\s*#(\d+)
§Examples
gh issue view 52 -> issue: 52
gh issue view 52 --repo paradigmxyz/mi6 -> issue: 52, repo: paradigmxyz/mi6
gh pr checks 86 -> pr: 86
gh pr view 64 --repo paradigmxyz/mi6 -> pr: 64, repo: paradigmxyz/mi6
gh pr create --title "Fix" --body "Fixes #52" -> issue: 52§GitHub URL Parsing
The parse_github_urls function extracts repo, issue, and PR from GitHub URLs
in command output.
Regex: https://github\.com/([^/]+/[^/]+)/(issues|pull)/(\d+)
§Examples
https://github.com/paradigmxyz/mi6/issues/236 -> issue: 236, repo: paradigmxyz/mi6
https://github.com/paradigmxyz/mi6/pull/237 -> pr: 237, repo: paradigmxyz/mi6This is particularly useful for capturing context from gh pr create output:
Created https://github.com/paradigmxyz/mi6/pull/237Note: URLs are only parsed from output of creation commands (gh issue create,
gh pr create) to avoid capturing incidental URLs from CI logs or code comments.
§Worktree and Directory Parsing
§Git Worktree Add
The parse_worktree_add function extracts path and branch from worktree commands.
Regex: git\s+worktree\s+add\s+(?:-f\s+)?([^\s]+)(?:.*-b\s+([^\s]+))?
git worktree add ../mi6-issue-42
-> path: /current/dir/../mi6-issue-42
git worktree add ../mi6-fix -b fix/issue-42 origin/main
-> path: /current/dir/../mi6-fix
-> branch: fix/issue-42§Git -C Path
The parse_git_c_path function extracts the path from git -C <path> commands.
Regex: git\s+-C\s+([^\s]+)
§CD Command
The parse_cd_path function parses cd commands with quoted/unquoted paths.
Regex: cd\s+(?:"([^"]+)"|'([^']+)'|([^\s;&|]+))
§Path Resolution
All paths are resolved:
~is expanded to$HOME- Relative paths are joined with the session’s
cwd
§Update Semantics
Updates use SQL COALESCE semantics:
- New non-NULL values overwrite existing values
- NULL values preserve existing values
- “Most recent wins” for non-NULL values
§Chained Command Support
Commands are split on &&, ||, and ; to detect references in compound commands:
echo foo && gh issue view 42 -> detected
git status; gh pr view 86 -> detected§Limitations
- Chained commands - Simple split on delimiters; doesn’t handle quoted delimiters
- Path resolution - Doesn’t canonicalize paths (may contain
..) - Tool output truncation - Very long output may be truncated before URL parsing
§Usage
use mi6_core::github::{extract_context, GitContextUpdate};
use std::path::Path;
let cwd = Path::new("/home/user/project");
let update = extract_context(
"gh issue view 52 --repo paradigmxyz/mi6",
None,
cwd,
);
assert_eq!(update.github_repo, Some("paradigmxyz/mi6".to_string()));
assert_eq!(update.github_issue, Some(52));Structs§
- GitContext
Update - Git and GitHub context update derived from a Bash tool event.
Functions§
- extract_
context - Extract all context from a Bash tool event.
- is_
gh_ pr_ view_ command - Check if the command is a gh pr view command.
- parse_
cd_ path - Parse cd command for directory changes.
- parse_
closes_ references - Parse PR body text for “Closes #N” references.
- parse_
gh_ command - Parse a Bash command for gh issue/pr references.
- parse_
gh_ pr_ view_ stats - Parse gh pr view output for additions/deletions.
- parse_
git_ c_ path - Parse
git -C <path>from a command. - parse_
github_ urls - Parse tool output for GitHub URLs.
- parse_
worktree_ add - Parse git worktree add command.
- split_
chained_ commands - Split command on
&&,||, and;to handle chained commands.