infrastructure

package
v4.3.4 Latest Latest
Warning

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

Go to latest
Published: Nov 16, 2026 License: MIT Imports: 22 Imported by: 3 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Index

Constants

View Source
const (
	DefaultGitTag               = "0.1.0"
	MaxAcceptableInitialCommits = 5
)

Variables

View Source
var (
	ErrNoAuthMethodFound  = errors.New("no authentication method found")
	ErrAuthNotImplemented = errors.New("authentication method not implemented")
	ErrNoRemoteURL        = errors.New("no remote URL found for repository")
	ErrNoTagsFound        = errors.New("no tags found in Git history")
)

Functions

func CheckBranchExists

func CheckBranchExists(repo *git.Repository, branchName string) (bool, error)

CheckBranchExists checks if a given Git branch exists (local or remote). Exported for use by autobump (github.com/rios0rios0/autobump).

func CheckoutBranch

func CheckoutBranch(w *git.Worktree, branchName string) error

CheckoutBranch switches to the given branch. It never forces the checkout: a forced go-git checkout resets the working tree hard, deleting untracked and ignored files. Exported for use by autobump (github.com/rios0rios0/autobump).

func CommitChanges

func CommitChanges(
	repo *git.Repository,
	workTree *git.Worktree,
	commitMessage string,
	signer globalEntities.CommitSigner,
	name string,
	email string,
) (plumbing.Hash, error)

CommitChanges commits the changes in the given worktree with optional signing. The repo parameter is required when using a CommitSigner that produces post-commit signatures (e.g. SSH). Exported for use by autobump (github.com/rios0rios0/autobump).

func CreateAndSwitchBranch

func CreateAndSwitchBranch(
	repo *git.Repository,
	workTree *git.Worktree,
	branchName string,
	hash plumbing.Hash,
) error

CreateAndSwitchBranch creates a new branch at hash and makes it the current branch. Exported for use by autobump (github.com/rios0rios0/autobump).

When hash is the commit HEAD already points to -- what every caller passes -- the switch needs nothing but HEAD itself: the branch is created and HEAD is pointed at it, leaving the index and the working tree exactly as they are, as `git switch -c` does. A go-git checkout is not used for that, in either of its forms. The plain one rejects the switch when the index differs from the working tree for reasons that would not stop git, such as line-ending normalisation after a native clone; and the forced one resets the working tree hard, which discards local changes and deletes every untracked and ignored file -- in an operator's own checkout, their `.env`, build output and editor settings -- even though no file needs to change.

A hash HEAD does not point to needs the working tree to change, so it gets a regular checkout, which refuses when local changes are in the way instead of discarding them. HEAD is compared before the branch is written: when branchName is the current branch, writing it first makes HEAD resolve to hash, which passes a move to another commit off as a switch with no file to change.

func DeleteLocalBranch

func DeleteLocalBranch(repo *git.Repository, branchName string) error

DeleteLocalBranch removes the local refs/heads/<branchName> reference when it exists.

This is the necessary companion to DeleteRemoteBranch: deleting a branch on the remote leaves the local branch untouched, and CheckBranchExists reports a branch as existing when it finds either one. A caller that deletes a branch remotely and then asks whether it exists would still be told "yes", and so would never recreate it.

A branch that is absent is not an error. The currently checked-out branch is never removed, because dropping HEAD's reference leaves the repository unusable.

Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate).

func DeleteRemoteBranch

func DeleteRemoteBranch(
	repo *git.Repository,
	branchName string,
	authMethods []transport.AuthMethod,
) error

DeleteRemoteBranch deletes the given branch on the origin remote by pushing an empty source to its ref. go-git drops the matching remote-tracking reference as part of that push, so only the remote side is handled here; a local branch of the same name survives and must be removed with DeleteLocalBranch.

Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate).

func GetAmountCommits

func GetAmountCommits(repo *git.Repository) (int, error)

GetAmountCommits returns the number of commits in the repository.

func GetLatestTag

func GetLatestTag(repo *git.Repository) (*globalEntities.LatestTag, error)

GetLatestTag finds the latest release tag in the Git history: the highest tag that reads as a semantic version, together with the date of the commit it points to. Exported for use by autobump (github.com/rios0rios0/autobump).

The highest version, not the tag the repository lists last. Tags are listed in no particular version order -- `1.10.0` sorts before `1.9.0` as text -- and a tag that is not a version, such as the four-part tags a fork carries from its upstream, used to come back with a nil version that callers printed as `<nil>`. Tags that are not versions are ignored, so a repository holding nothing else is treated like one without tags.

func GetRemoteRepoURL

func GetRemoteRepoURL(repo *git.Repository) (string, error)

GetRemoteRepoURL returns the URL of the remote repository. Exported for use by autobump (github.com/rios0rios0/autobump).

func ListRemoteBranches

func ListRemoteBranches(repo *git.Repository, authMethods []transport.AuthMethod) ([]string, error)

ListRemoteBranches returns the short names of every branch present on the origin remote. It queries the remote directly rather than reading remote-tracking references, so the result reflects the current server state even when the local clone is stale.

Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate).

func OpenRepo

func OpenRepo(projectPath string) (*git.Repository, error)

OpenRepo opens a git repository at the given path. Exported for use by autobump (github.com/rios0rios0/autobump).

func PushChangesSSH

func PushChangesSSH(repo *git.Repository, refSpec config.RefSpec, authMethods []transport.AuthMethod) error

PushChangesSSH pushes the changes to the remote repository over SSH. If authMethods is non-empty, each method is tried in order until one succeeds. If all explicit methods fail or authMethods is empty, it falls back to go-git's default SSH agent behaviour (SSH_AUTH_SOCK), carrying the host key callback installed via SetDefaultSSHHostKeyCallback so the fallback verifies hosts against the consumer's own known_hosts discovery rather than go-git's.

When both an explicit method and the fallback fail, the returned error names the explicit failure too: the fallback's own error (often "unable to find any valid known_hosts file", or an agent that answered nothing) otherwise masks the credential problem the operator actually has to fix.

func PushWithTransportDetection

func PushWithTransportDetection(
	repo *git.Repository,
	refSpec config.RefSpec,
	authMethods []transport.AuthMethod,
) error

PushWithTransportDetection pushes the given refSpec to the origin remote, auto-detecting the transport (SSH or HTTPS) from the remote URL.

For SSH remotes (git@ or ssh://), authMethods are tried in order; if all fail or none are provided, it falls back to go-git's default SSH agent.

For HTTPS/HTTP remotes, authMethods are tried in order until one succeeds. An empty authMethods slice returns an error for HTTPS remotes.

Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate).

func SetDefaultSSHHostKeyCallback added in v4.3.0

func SetDefaultSSHHostKeyCallback(callback ssh.HostKeyCallback)

SetDefaultSSHHostKeyCallback installs the host key callback used by the default SSH agent fallback in PushChangesSSH and remote reference listing.

Without it the fallback pushes with a nil auth method, and go-git then builds its own host key verification from SSH_KNOWN_HOSTS or ~/.ssh/known_hosts alone. On a host that keeps its known_hosts somewhere else — a WSL machine whose file belongs to the Windows profile is the common case — every fallback fails with "unable to find any valid known_hosts file" even though the consumer already discovered the file and verified against it for the explicit auth methods. A consumer that does its own discovery hands the resulting callback here so the fallback verifies hosts against the same data.

Passing nil restores go-git's own default behaviour.

func StageAll

func StageAll(workTree *git.Worktree) error

StageAll adds all changes in the working tree to the index, equivalent to `git add -A`. It stages new, modified, and deleted files. Exported for use by autoupdate (github.com/rios0rios0/autoupdate).

func WorktreeIsClean

func WorktreeIsClean(workTree *git.Worktree) (bool, error)

WorktreeIsClean returns true when the working tree has no uncommitted changes (staged or unstaged). This is the go-git equivalent of `git status --porcelain` returning empty output. Exported for use by autoupdate (github.com/rios0rios0/autoupdate).

Types

type GitOperations

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

GitOperations encapsulates git operations with an adapter finder for service type resolution.

func NewGitOperations

func NewGitOperations(finder gitEntities.AdapterFinder) *GitOperations

NewGitOperations creates a new GitOperations with the given adapter finder.

func (*GitOperations) CloneRepo

func (o *GitOperations) CloneRepo(
	url, dir string,
	authMethods []transport.AuthMethod,
) (*git.Repository, error)

CloneRepo clones a remote repository into the given directory. It prepares the clone URL via the adapter finder (if one matches the URL) and tries each provided authentication method until one succeeds.

func (*GitOperations) GetAuthMethods

func (o *GitOperations) GetAuthMethods(
	service globalEntities.ServiceType,
	username string,
) ([]transport.AuthMethod, error)

GetAuthMethods returns the authentication methods for the given service type. It delegates to the appropriate adapter based on the service type. Exported for use by autobump (github.com/rios0rios0/autobump).

func (*GitOperations) GetRemoteServiceType

func (o *GitOperations) GetRemoteServiceType(repo *git.Repository) (globalEntities.ServiceType, error)

GetRemoteServiceType returns the type of the remote service (e.g. GitHub, GitLab). Exported for use by autobump (github.com/rios0rios0/autobump).

func (*GitOperations) GetServiceTypeByURL

func (o *GitOperations) GetServiceTypeByURL(remoteURL string) globalEntities.ServiceType

GetServiceTypeByURL returns the type of the remote service by URL. Exported for use by autobump (github.com/rios0rios0/autobump).

func (*GitOperations) PushChangesHTTPS

func (o *GitOperations) PushChangesHTTPS(
	repo *git.Repository,
	username string,
	refSpec config.RefSpec,
) error

PushChangesHTTPS pushes the changes to the remote repository over HTTPS. It tries each authentication method returned by the adapter until one succeeds.

type PullRequestURLInfo

type PullRequestURLInfo struct {
	ServiceType  globalEntities.ServiceType
	Organization string
	Project      string // Azure DevOps only; empty for GitHub/GitLab
	RepoName     string
	PRID         int
}

PullRequestURLInfo holds the parsed components of a pull request URL.

func ParsePullRequestURL

func ParsePullRequestURL(rawURL string) (*PullRequestURLInfo, error)

ParsePullRequestURL extracts provider, organization, project, repository, and PR ID from a PR URL. Supported formats:

type RemoteURLInfo

type RemoteURLInfo struct {
	ServiceType  globalEntities.ServiceType
	Organization string
	Project      string // Azure DevOps only; empty for GitHub/GitLab
	RepoName     string
}

RemoteURLInfo holds the parsed components of a Git remote URL.

func ParseRemoteURL

func ParseRemoteURL(rawURL string) (*RemoteURLInfo, error)

ParseRemoteURL extracts provider, organization, project, and repository name from a Git remote URL. Supported formats:

type UserConfig

type UserConfig struct {
	Name          string
	Email         string
	SigningKey    string
	SigningFormat string
	SSHProgram    string // gpg.ssh.program — custom signing binary (e.g. op-ssh-sign-wsl)
}

UserConfig holds user-specific git configuration values read from local and global git config. Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate) as the canonical way to read user identity and signing settings before committing.

func ReadUserConfig

func ReadUserConfig(repo *git.Repository) (*UserConfig, error)

ReadUserConfig reads user name, email, signing key, and signing format from the repository's local config, falling back to the global ~/.gitconfig. Exported for use by autobump (github.com/rios0rios0/autobump) and autoupdate (github.com/rios0rios0/autoupdate).

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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