go-git(docs) gomatic manual

Name

go-gitPublic documentation for the project.

go-git exposes git repository facts — the current branch, the origin remote URL, and the repository owner — plus a forward-only check, all read through an injected Runner so callers are testable without invoking the real git binary. The functionality lives in the facts package.

Install

sh
go get github.com/gomatic/go-git

Usage

Everything is driven by a Runner, which runs a git subcommand and returns its stdout. In production, pass NewExecRunner() — the real-git implementation; in tests, pass a fake.

go
package main

import (
	"fmt"
	"log"

	"github.com/gomatic/go-git/facts"
)

func main() {
	r := facts.NewExecRunner()

	branch, err := facts.Branch(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("branch:", branch)

	origin, err := facts.Origin(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("origin:", origin)

	owner, err := facts.OwnerOf(r)
	if err != nil {
		log.Fatal(err)
	}
	fmt.Println("owner:", owner)
}

Functions

Errors

Both sentinels are gomatic/go-error constants, matched with errors.Is — never by string:

go
if _, err := facts.Branch(r); errors.Is(err, facts.ErrDetachedHead) {
	// HEAD is detached
}

Testing with a fake Runner

Because every function takes a Runner, tests never touch a real repository — supply canned output:

go
type fakeRunner struct {
	out facts.CommandOutput
	err error
}

func (f fakeRunner) Run(args ...facts.Arg) (facts.CommandOutput, error) {
	return f.out, f.err
}

func TestBranch(t *testing.T) {
	branch, err := facts.Branch(fakeRunner{out: "main\n"})
	// branch == "main", err == nil
}

Design

  • Injected runner, no hidden globals. The git binary is reached only through the Runner interface; ExecRunner is the sole real implementation, so every code path is reachable from a test with a fake.
  • Named domain types. Arguments and results use dedicated newtypes — Arg, CommandOutput, BranchName, OriginURL, Owner — rather than bare strings.
  • Value receivers, immutable, private by default. ExecRunner is an empty value type; only what callers need is exported.
  • Extracted from skykernel/skym’s internal/git — generalized and moved to gomatic for reuse.