Module git

Module git 

Source
Expand description

Git branch tracking utilities for mi6.

This module provides functionality to:

  • Capture the current git branch from a working directory
  • Parse branch names to extract PR/issue numbers
  • Detect git branch-changing commands in shell commands

§Branch Name Parsing

The parse_branch_info function extracts issue and PR numbers from branch names using common naming conventions.

§PR Number Patterns

PatternExampleExtracted
pr-Npr-100pr: 100
prNfeature/pr200pr: 200

§Issue Number Patterns (in priority order)

PatternExampleExtracted
issue-Nfeature/issue-123issue: 123
issueNfeature/issue456issue: 456
gh-Nfix/GH-456issue: 456
fix-Nfix-789-descriptionissue: 789
Leading number123-add-featureissue: 123
Trailing numberfeature-something-42issue: 42

Notes:

  • Matching is case-insensitive
  • Only the last path component after / is parsed (e.g., feature/issue-123 parses issue-123)
  • PR patterns are checked before issue patterns
  • First matching pattern wins
  • Branches like fix/123 work via the “leading number” pattern (the fix/ prefix is stripped when extracting the last component)

§Examples

main                        -> (no extraction)
feature/issue-123           -> issue: 123
fix/GH-456                  -> issue: 456
pr-100                      -> pr: 100
feature/pr-200              -> pr: 200
fix-789-description         -> issue: 789
fix/123-some-bug            -> issue: 123 (via leading number)
123-add-feature             -> issue: 123
feature-add-something-42    -> issue: 42
feature/add-new-thing       -> (no extraction)

§Branch-Changing Command Detection

The is_branch_changing_command function detects git commands that change the current branch, triggering re-capture of branch info.

§Detected Commands

CommandDescription
git checkout <branch>Switch branches
git checkout -b <branch>Create and switch
git switch <branch>Modern branch switch
git worktree add <path>Create worktree
git merge <branch>Merge (may fast-forward)
git rebase <branch>Rebase onto branch
git pullPull (may merge/rebase)

§Chained Command Support

Commands are split on &, |, and ; to detect branch changes in compound commands:

echo foo && git checkout main   -> detected
git status; git checkout main   -> detected
cargo build && git switch dev   -> detected

§Limitations

  1. Branch parsing is heuristic - May not match all naming conventions
  2. Chained commands - Simple split on delimiters; doesn’t handle quoted delimiters

Structs§

GitBranchInfo
Result of parsing a git branch name for PR/issue numbers.

Functions§

get_branch_info
Get git branch info for a directory.
get_current_branch
Get the current git branch for a directory.
get_github_repo
Get the GitHub repository from git remote origin.
get_local_git_dir
Get the absolute path to the .git directory for a working directory.
get_worktree_root
Get the worktree root path if the current directory is within a git worktree.
is_branch_changing_command
Check if a shell command might change the git branch.
parse_branch_info
Parse a branch name to extract PR and issue numbers.
parse_github_repo_from_remote_url
Parse GitHub repo from a git remote URL.