gitrepo

package
v0.20.0 Latest Latest
Warning

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

Go to latest
Published: Jul 5, 2026 License: MIT Imports: 4 Imported by: 0

README

Git Repository

Lets human read facts about the git repository you are working in, so it can figure out where your code lives without you spelling it out. This is how human knows which forge and project to act on.

  • Reads the origin remote URL of a repo
  • Works on the current directory by default
  • Targets any repository by path
  • Feeds forge and project detection automatically
  • Reports a clear error when no remote

Documentation

Overview

Package gitrepo reads facts about the local git repository by shelling out to git. It is the single place that runs git from Go (elsewhere git is only invoked from agent prompts), so the exec is isolated and testable.

Index

Constants

This section is empty.

Variables

View Source
var DefaultBranch = func(ctx context.Context, dir string) string {
	out, err := runner(ctx, "git", "-C", dir, "symbolic-ref", "refs/remotes/origin/HEAD")
	if err != nil {
		return "main"
	}
	ref := strings.TrimSpace(string(out))
	ref = strings.TrimPrefix(ref, "refs/remotes/")
	if branch := strings.TrimPrefix(ref, "origin/"); branch != "" {
		return branch
	}
	return "main"
}

DefaultBranch returns the repository's default branch by resolving origin/HEAD (running `git -C <dir> symbolic-ref refs/remotes/origin/HEAD`) and stripping the leading "origin/". It falls back to "main" when origin/HEAD is not set locally. It is a package variable so tests can stub git access.

View Source
var OriginURL = func(ctx context.Context, dir string) (string, error) {
	out, err := runner(ctx, "git", "-C", dir, "remote", "get-url", "origin")
	if err != nil {
		return "", errors.WrapWithDetails(err, "reading git origin remote", "dir", dir)
	}
	url := strings.TrimSpace(string(out))
	if url == "" {
		return "", errors.WithDetails("git origin remote is empty", "dir", dir)
	}
	return url, nil
}

OriginURL returns the URL of the "origin" remote for the repository at dir (running `git -C <dir> remote get-url origin`). dir may be "." for the current working directory. The returned value is trimmed of surrounding whitespace.

It is a package variable so callers in other packages can stub git access in their own tests without a real repository.

View Source
var Push = func(ctx context.Context, dir, branch string) error {
	if _, err := runner(ctx, "git", "-C", dir, "push", "origin", branch); err != nil {
		return errors.WrapWithDetails(err, "pushing branch to origin", "dir", dir, "branch", branch)
	}
	return nil
}

Push pushes branch to the origin remote of the repository at dir (running `git -C <dir> push origin <branch>`). It is a package variable so callers can stub git access in tests without a real repository.

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

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