Module github

Module github 

Source
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/pr commands
  • GitHub URLs in command output (e.g., from gh pr create)
  • Fixes #N references in PR bodies
  • Git worktree operations

§Fields Populated

FieldDescriptionSources
github_issueIssue numbergh commands, URLs, PR body
github_prPR numbergh commands, URLs
github_repoRepository (owner/repo)gh --repo flag, GitHub URLs
git_worktree_pathWorktree directory pathgit worktree add, git -C, cd
git_branchCurrent branchSet 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 #N

Regex (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/mi6

This is particularly useful for capturing context from gh pr create output:

Created https://github.com/paradigmxyz/mi6/pull/237

Note: 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

  1. Chained commands - Simple split on delimiters; doesn’t handle quoted delimiters
  2. Path resolution - Doesn’t canonicalize paths (may contain ..)
  3. 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§

GitContextUpdate
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.