Help us shape the future of Go! Take the 2026 Go Developer Survey

git

package
v1.1.0 Latest Latest
Warning

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

Go to latest
Published: Oct 25, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package git is a thin wrapper around the git binary.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Ref

func Ref(name string) string

Ref returns the full ref of local branch name. Passing it instead of the bare name keeps a tag of the same name from being picked.

func SamePath

func SamePath(a, b string) bool

SamePath reports whether two paths name the same directory, resolving symlinks.

Types

type Commit

type Commit struct {
	SHA     string
	Subject string
	Time    time.Time
}

Commit is a one-line summary of a commit.

type Error

type Error struct {
	Args   []string
	Stderr string
	Err    error
}

Error is returned when git exits non-zero.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Repo

type Repo struct {
	Dir string
	// HooksPath, if set, is passed to every git command as core.hooksPath;
	// pointing it at a missing directory runs git without hooks. It only
	// lives on the command line, so no config is ever changed.
	HooksPath string
}

Repo runs git commands in Dir.

func (*Repo) At

func (r *Repo) At(dir string) *Repo

At returns a Repo rooted at dir, e.g. another worktree.

func (*Repo) BranchExists

func (r *Repo) BranchExists(name string) bool

BranchExists reports whether a local branch exists.

func (*Repo) Checkout

func (r *Repo) Checkout(branch string) error

Checkout switches to branch. The "--" keeps git from taking a missing branch's name as a path and restoring that path from the index instead.

func (*Repo) CommitCount

func (r *Repo) CommitCount(base, head string) (int, error)

CommitCount returns the number of commits in base..head.

func (*Repo) CommonDir

func (r *Repo) CommonDir() (string, error)

CommonDir returns the absolute git common dir, shared by all worktrees.

func (*Repo) ConflictLines

func (r *Repo) ConflictLines(file string) []int

ConflictLines returns the 1-based line numbers of conflict markers in file, a path relative to the worktree root as ConflictedFiles returns it.

func (*Repo) ConflictedFiles

func (r *Repo) ConflictedFiles() ([]string, error)

ConflictedFiles lists paths with unresolved conflicts.

func (*Repo) CreateBranch

func (r *Repo) CreateBranch(name, start string) error

CreateBranch creates name at start without checking it out.

func (*Repo) CurrentBranch

func (r *Repo) CurrentBranch() (string, error)

CurrentBranch returns the checked-out branch, or "" when HEAD is detached. It strips refs/heads/ itself: `--short` yields "heads/x" when a tag x exists.

func (*Repo) DefaultBranch

func (r *Repo) DefaultBranch(remote string) (string, error)

DefaultBranch returns the remote's default branch from refs/remotes/<remote>/HEAD, falling back to main or master.

func (*Repo) DefaultRemote

func (r *Repo) DefaultRemote() (string, error)

DefaultRemote returns "origin" when present, else the only remote.

func (*Repo) EnableRerere

func (r *Repo) EnableRerere() error

EnableRerere turns on rerere for this repository.

func (*Repo) Fetch

func (r *Repo) Fetch(remote string) error

Fetch fetches from remote.

func (*Repo) ForkPoint

func (r *Repo) ForkPoint(base, branch string) (string, error)

ForkPoint returns `git merge-base --fork-point base branch`.

func (*Repo) GitDir

func (r *Repo) GitDir() (string, error)

GitDir returns the absolute per-worktree git dir.

func (*Repo) HasPatches added in v1.0.2

func (r *Repo) HasPatches(branch, sha string) bool

HasPatches reports whether every commit sha has and the local branch lacks makes a change the branch already has, as when a host rebased the branch onto the same base itself. Merge commits never count.

func (*Repo) Includes

func (r *Repo) Includes(branch, sha string) bool

Includes reports whether sha is, or once was, part of the local branch: an ancestor of its tip or of one of its reflog entries. A remote commit that is not included was never integrated locally.

func (*Repo) IsAncestor

func (r *Repo) IsAncestor(a, b string) bool

IsAncestor reports whether a is an ancestor of b.

func (*Repo) IsClean

func (r *Repo) IsClean() (bool, error)

IsClean reports whether the worktree has no staged or unstaged changes to tracked files.

func (*Repo) LastCommit

func (r *Repo) LastCommit(rev string) (Commit, error)

LastCommit returns the tip commit of rev.

func (*Repo) MergeBase

func (r *Repo) MergeBase(a, b string) (string, error)

MergeBase returns the best common ancestor of a and b.

func (*Repo) RebaseInProgress

func (r *Repo) RebaseInProgress() (bool, error)

RebaseInProgress reports whether this worktree is mid-rebase.

func (*Repo) Remotes

func (r *Repo) Remotes() ([]string, error)

Remotes lists configured remotes.

func (*Repo) RevParse

func (r *Repo) RevParse(rev string) (string, error)

RevParse resolves rev to a full SHA.

func (*Repo) Run

func (r *Repo) Run(args ...string) (string, error)

Run executes git with args and returns trimmed stdout.

func (*Repo) RunEnv

func (r *Repo) RunEnv(env []string, args ...string) (string, error)

RunEnv executes git with extra environment variables.

func (*Repo) SetBranchSHA

func (r *Repo) SetBranchSHA(branch, sha string) error

SetBranchSHA force-moves a branch that is not checked out.

func (*Repo) TopLevel

func (r *Repo) TopLevel() (string, error)

TopLevel returns the root of the current worktree.

func (*Repo) ValidBranchName

func (r *Repo) ValidBranchName(name string) bool

ValidBranchName reports whether name is an acceptable branch name. `check-ref-format --branch` expands @{-N} and accepts "@", so those are rejected here.

func (*Repo) WorktreeFor

func (r *Repo) WorktreeFor(branch string) (string, error)

WorktreeFor returns the path of the worktree that has branch checked out, or "".

func (*Repo) Worktrees

func (r *Repo) Worktrees() ([]Worktree, error)

Worktrees lists all worktrees of the repository.

type Worktree

type Worktree struct {
	Path   string
	Branch string // short name; "" when detached
}

Worktree describes one entry of `git worktree list`.

Jump to

Keyboard shortcuts

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