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

git

package
v0.66.0 Latest Latest
Warning

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

Go to latest
Published: Oct 24, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package git provides an abstraction for git operations.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func EquivalentRemote

func EquivalentRemote(left, right string) bool

EquivalentRemote reports whether two remotes refer to the same repository under RemoteIdentity.

func ExtractHost

func ExtractHost(remote string) string

ExtractHost extracts the host (without port) from a git remote URL. Handles SCP-style SSH (git@github.com:owner/repo.git), ssh:// and https:// URLs, and hosts with non-standard ports (git.example.com:2222). Returns an empty string when no host can be determined.

func ExtractOwnerRepo

func ExtractOwnerRepo(remote string) (owner, repo string)

ExtractOwnerRepo extracts owner and repo from a git remote URL. Handles SSH (git@github.com:owner/repo.git) and HTTPS (https://github.com/owner/repo.git). Returns empty strings if parsing fails.

func ExtractRepoName

func ExtractRepoName(remote string) string

ExtractRepoName extracts the repository name from a git remote URL. Handles both SSH (git@github.com:user/repo.git) and HTTPS (https://github.com/user/repo.git) formats.

func IsExplicitRemote added in v0.66.0

func IsExplicitRemote(repo string) bool

IsExplicitRemote reports whether repo is already a remote or a path rather than an owner/name pair: it contains "://", "@", ":" or "\", or starts with "/", "./", "../" or "~/".

func RemoteIdentity

func RemoteIdentity(remote string) string

RemoteIdentity returns the key under which remotes that name the same repository compare equal: host and repository path, without the transport, user, default port, or ".git" suffix. The host is lowercased everywhere but the path only on GitHub, which ignores its case; other forges may not. A local path or a remote that does not parse is returned trimmed.

func RemoteProtocol added in v0.66.0

func RemoteProtocol(remote string) (session.CloneProtocol, bool)

RemoteProtocol returns the transport of remote: https for http(s)://, ssh for ssh:// and SCP-style [user@]host:path. ok is false for anything else, such as a local path or git://.

func ValidateBranchName

func ValidateBranchName(s string) error

ValidateBranchName checks whether s is a valid git branch name according to git-check-ref-format(1) rules. Returns a descriptive error if invalid.

func WebURL added in v0.66.0

func WebURL(remote string) string

WebURL returns the browser address of the repository a remote names: https://host/path, without ".git". An http(s) remote keeps its scheme and port, which are the web server's. Any other transport's port belongs to git, so WebURL drops it. A local path or a remote that does not parse returns "".

Types

type Executor

type Executor struct {
	// contains filtered or unexported fields
}

Executor implements Git using the git command-line tool.

func NewExecutor

func NewExecutor(gitPath string, exec executil.Executor) *Executor

NewExecutor creates a new git executor with the specified git binary path.

func (*Executor) Branch

func (e *Executor) Branch(ctx context.Context, dir string) (string, error)

func (*Executor) Checkout

func (e *Executor) Checkout(ctx context.Context, dir, branch string) error

func (*Executor) Clone

func (e *Executor) Clone(ctx context.Context, url, dest string) error

func (*Executor) CloneBare

func (e *Executor) CloneBare(ctx context.Context, url, dest string) error

func (*Executor) DefaultBranch

func (e *Executor) DefaultBranch(ctx context.Context, dir string) (string, error)

func (*Executor) DiffStats

func (e *Executor) DiffStats(ctx context.Context, dir string) (additions, deletions int, err error)

func (*Executor) Fetch

func (e *Executor) Fetch(ctx context.Context, dir string) error

func (*Executor) HasUnpushedCommits

func (e *Executor) HasUnpushedCommits(ctx context.Context, dir string) (bool, error)

func (*Executor) IsClean

func (e *Executor) IsClean(ctx context.Context, dir string) (bool, error)

func (*Executor) IsValidRepo

func (e *Executor) IsValidRepo(ctx context.Context, dir string) error

func (*Executor) Pull

func (e *Executor) Pull(ctx context.Context, dir string) error

func (*Executor) RemoteURL

func (e *Executor) RemoteURL(ctx context.Context, dir string) (string, error)

func (*Executor) ResetHard

func (e *Executor) ResetHard(ctx context.Context, dir string) error

func (*Executor) SetRemoteURL added in v0.66.0

func (e *Executor) SetRemoteURL(ctx context.Context, dir, url string) error

func (*Executor) WorktreeAdd

func (e *Executor) WorktreeAdd(ctx context.Context, repoDir, path, branch string) error

func (*Executor) WorktreeRemove

func (e *Executor) WorktreeRemove(ctx context.Context, repoDir, path, branch string) error

type Git

type Git interface {
	// Clone clones a repository from url to dest.
	Clone(ctx context.Context, url, dest string) error
	// Checkout switches to the specified branch in dir.
	Checkout(ctx context.Context, dir, branch string) error
	// Pull fetches and merges changes in dir.
	Pull(ctx context.Context, dir string) error
	// ResetHard discards all local changes in dir.
	ResetHard(ctx context.Context, dir string) error
	// RemoteURL returns the origin remote URL for dir.
	RemoteURL(ctx context.Context, dir string) (string, error)
	// SetRemoteURL points origin in dir at url.
	SetRemoteURL(ctx context.Context, dir, url string) error
	// IsClean returns true if there are no uncommitted changes in dir.
	IsClean(ctx context.Context, dir string) (bool, error)
	// Branch returns the current branch name, or short commit SHA if in detached HEAD state.
	Branch(ctx context.Context, dir string) (string, error)
	// DefaultBranch returns the default branch name (e.g., "main" or "master") for the repository.
	DefaultBranch(ctx context.Context, dir string) (string, error)
	// DiffStats returns the number of lines added and deleted compared to the default branch.
	DiffStats(ctx context.Context, dir string) (additions, deletions int, err error)
	// IsValidRepo checks if dir contains a valid git repository.
	IsValidRepo(ctx context.Context, dir string) error
	// CloneBare creates a bare clone of url at dest.
	CloneBare(ctx context.Context, url, dest string) error
	// WorktreeAdd creates a new worktree at path on a new branch named branch.
	WorktreeAdd(ctx context.Context, repoDir, path, branch string) error
	// WorktreeRemove removes the worktree at path and deletes branch from repoDir when provided.
	WorktreeRemove(ctx context.Context, repoDir, path, branch string) error
	// Fetch fetches all remotes in dir.
	Fetch(ctx context.Context, dir string) error
	// HasUnpushedCommits returns true if there are local commits not yet pushed to a remote.
	// It first checks the upstream tracking branch; if none is set, it falls back to comparing
	// against origin/<default branch>. Returns false (no risk) on any git error.
	HasUnpushedCommits(ctx context.Context, dir string) (bool, error)
}

Git defines git operations needed by hive.

type RepoLocation added in v0.66.0

type RepoLocation struct {
	// Host is the hostname plus a non-default port. Only HTTPS URLs use the port.
	Host string
	// Path is owner/name or group/subgroup/name, without ".git".
	Path string
}

RepoLocation is a forge host and repository path a clone URL can be built for.

func ItemRepoLocation added in v0.66.0

func ItemRepoLocation(repo, itemURL string) (RepoLocation, bool)

ItemRepoLocation expands an item's owner/repo using the host of its http(s) web URL. ok is false for an explicit remote, a malformed repo, or a URL without a host.

func (RepoLocation) Candidates added in v0.66.0

func (l RepoLocation) Candidates() []string

Candidates returns the HTTPS and SSH URLs, so a rule pattern written for either transport matches.

func (RepoLocation) CloneURL added in v0.66.0

func (l RepoLocation) CloneURL(p session.CloneProtocol) string

CloneURL renders https://host[:port]/path.git, or git@hostname:path.git for SSH. SSH never carries a port: a forge whose SSH port differs from its web port needs an explicit repo_template.

Jump to

Keyboard shortcuts

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