git

package
v0.13.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 8, 2026 License: MIT Imports: 5 Imported by: 0

README

pkg/git

Extracts git repository information from the working directory or from transcript files.

Files

File Role
git.go Live git commands (DetectGitInfo, GetHeadSHA, GetRepoURL, ToGitHubURL)

Key API

  • DetectGitInfo(cwd) — Returns *GitInfo with repo URL, branch, commit SHA, message, author, and dirty status. Returns nil (not error) if not in a git repo.
  • GetHeadSHA(cwd) — Returns the full 40-char HEAD commit SHA.
  • GetRepoURL(cwd) — Returns remote.origin.url.
  • ToGitHubURL(gitURL) — Converts git remote URLs (SSH, HTTPS, git@) to https://github.com/owner/repo. Returns empty string for non-GitHub URLs.
  • ExtractGitInfoFromTranscript(path) — Parses a JSONL transcript to find gitBranch and cwd fields. Used when the working directory may no longer exist.

How to Extend

Adding a new git field: Add the field to the GitInfo struct, then add the corresponding git command in DetectGitInfo(). Follow the existing pattern: run gitCommand(), check for errors, assign to struct.

Invariants

  • All git command arguments must be hardcoded string literals. Never pass user input to gitCommand() / exec.Command. This prevents command injection.
  • DetectGitInfo returns nil, not error, when not in a git repo. Callers check for nil, not err. Being outside a git repo is normal (not an error condition).
  • Two extraction paths exist intentionally. Live git commands (DetectGitInfo) work when the repo is available. Transcript parsing (ExtractGitInfoFromTranscript) works when replaying sessions where the repo may be gone. Don't merge these — they serve different lifecycle phases.

Testing

go test ./pkg/git/...

Tests create temporary git repositories with git init and verify extraction. ExtractGitInfoFromTranscript tests use synthetic JSONL files.

Dependencies

Uses: standard library (os/exec for git commands), pkg/types (JSONL scanner)

Used by: cmd/ (post-tool-use hook for commit linking), pkg/sync/ (metadata extraction)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func GetHeadSHA

func GetHeadSHA(cwd string) (string, error)

GetHeadSHA returns the full SHA of the HEAD commit Returns empty string and error if not a git repo or no commits

func GetRepoURL

func GetRepoURL(cwd string) (string, error)

GetRepoURL returns the remote origin URL for a git repository Returns empty string if not a git repo or no remote configured

func ToGitHubURL

func ToGitHubURL(gitURL string) string

ToGitHubURL converts a git remote URL to a GitHub HTTPS URL. Handles: git@github.com:owner/repo.git, https://github.com/owner/repo.git, ssh://git@github.com/owner/repo.git Returns empty string if not a GitHub URL.

Types

type GitInfo

type GitInfo struct {
	RepoURL       string `json:"repo_url,omitempty"`
	Branch        string `json:"branch,omitempty"`
	CommitSHA     string `json:"commit_sha,omitempty"`
	CommitMessage string `json:"commit_message,omitempty"`
	Author        string `json:"author,omitempty"`
	IsDirty       bool   `json:"is_dirty"`
}

GitInfo contains git repository information

func DetectGitInfo

func DetectGitInfo(cwd string) (*GitInfo, error)

DetectGitInfo detects git information from the given directory Returns nil if not in a git repository (this is not an error)

func ExtractGitInfoFromTranscript

func ExtractGitInfoFromTranscript(transcriptPath string) (*GitInfo, error)

ExtractGitInfoFromTranscript parses a Claude Code transcript file to extract git information This is useful for uploading sessions where the original directory may not exist

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL