prefer-go-git-over-subshellslisted
Install: claude install-skill tstapler/stapler-squad
# Prefer go-git Over Shelling Out
When a git operation can be done with `github.com/go-git/go-git/v5`, use it instead of shelling out via `safeexec.CommandContext("git", ...)`. Reduce subshells wherever a native Go integration exists.
## When to Use This Skill
- Writing new Go code that needs to inspect or mutate a git repository
- Reviewing a diff that adds a `safeexec.CommandContext(ctx, "git", ...)` call
- Deciding whether an existing `git` subshell can be replaced with a native go-git call
**Wrong:**
```go
func IsCommitOnMain(repoPath, mainBranch, sha string) (bool, error) {
ctx, cancel := context.WithTimeout(context.Background(), 15*time.Second)
defer cancel()
cmd := safeexec.CommandContext(ctx, "git", "-C", repoPath, "merge-base", "--is-ancestor", sha, mainBranch)
err := cmd.Run()
// ...parse exit code...
}
```
**Right:**
```go
func IsCommitOnMain(repoPath, mainBranch, sha string) (bool, error) {
repo, err := git.PlainOpenWithOptions(repoPath, &git.PlainOpenOptions{DetectDotGit: true})
if err != nil {
return false, fmt.Errorf("failed to open git repo at %s: %w", repoPath, err)
}
shaCommit, err := repo.CommitObject(plumbing.NewHash(sha))
if err != nil {
return false, fmt.Errorf("failed to resolve commit %s: %w", sha, err)
}
mainCommit, err := repo.CommitObject(mainRef.Hash())
if err != nil {
return false, err
}
return shaCommit.IsAncestor(mainCommit)
}
```
See `session/git/ops.go`'s `IsCommitOnMain` for the full version (with an `origin/main` f