git

package
v1.8.1 Latest Latest
Warning

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

Go to latest
Published: Jun 24, 2026 License: Apache-2.0 Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertToSSH

func ConvertToSSH(httpsURL string) (string, error)

ConvertToSSH converts HTTPS git URLs to SSH format Example: https://github.com/owner/repo.git → git@github.com:owner/repo.git

func DefaultHTTPAuthUsername added in v1.3.0

func DefaultHTTPAuthUsername(host, explicit string) string

DefaultHTTPAuthUsername returns a reasonable basic-auth username for a git HTTP(S) token when the caller did not configure one explicitly.

Host-based detection only covers gitlab.com and *.gitlab.com — self-hosted GitLab instances on custom domains (gitlab.example.com, etc.) fall back to "x-access-token", which GitLab rejects. Callers pointing at self-hosted GitLab must pass explicit = "oauth2" to override.

func GetSSHKeyPath

func GetSSHKeyPath() string

GetSSHKeyPath returns the global SSH key path

func IsHTTPSURL

func IsHTTPSURL(url string) bool

IsHTTPSURL checks if URL is in HTTPS format

func IsSSHURL

func IsSSHURL(url string) bool

IsSSHURL checks if URL is in SSH format (git@host:path)

func LooksLikeHTTPRemote added in v1.3.0

func LooksLikeHTTPRemote(repoURL string) bool

LooksLikeHTTPRemote reports whether repoURL uses an HTTP(S) scheme even when the URL is malformed enough that ParseRemoteAuthInfo cannot extract a host.

func SetSSHKeyPath

func SetSSHKeyPath(cmd *cobra.Command)

SetSSHKeyPath sets the global SSH key path from either the flag or environment variable This should be called once at startup from the root command

func ValidateSSHKey

func ValidateSSHKey(keyPath string) error

ValidateSSHKey validates SSH key file exists, readable, and has proper permissions If the keyPath looks like actual key content (starts with "-----BEGIN"), it's considered valid

Types

type Client

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

Client provides high-level git operations with SSH key support

func NewClient

func NewClient() *Client

NewClient creates a new git client using the global SSH key path

func NewClientWithOptions added in v1.3.0

func NewClientWithOptions(opts ...ClientOption) *Client

func (*Client) Add

func (c *Client) Add(ctx context.Context, repoPath string, paths ...string) error

Add stages files for commit

func (*Client) Checkout

func (c *Client) Checkout(ctx context.Context, repoPath, ref string) error

Checkout checks out a specific ref (branch, tag, or commit)

func (*Client) CheckoutNewBranch added in v0.12.7

func (c *Client) CheckoutNewBranch(ctx context.Context, repoPath, branch string) error

CheckoutNewBranch creates and switches to a new branch

func (*Client) CheckoutNewBranchForce added in v1.8.0

func (c *Client) CheckoutNewBranchForce(ctx context.Context, repoPath, branch string) error

CheckoutNewBranchForce creates (or resets, if it already exists) a branch at the current HEAD and switches to it. Unlike CheckoutNewBranch it does not fail when the branch name is already present locally — important for the cached vault clone, which is reused across runs and may carry a leftover branch from a previous attempt.

func (*Client) Clone

func (c *Client) Clone(ctx context.Context, repoURL, destPath string) error

Clone clones a git repository to the specified destination path

func (*Client) Commit

func (c *Client) Commit(ctx context.Context, repoPath, message string) error

Commit creates a commit with the given message

func (*Client) ExtraEnv added in v1.3.0

func (c *Client) ExtraEnv() []string

func (*Client) Fetch

func (c *Client) Fetch(ctx context.Context, repoPath string) error

Fetch fetches all remotes in the repository

func (*Client) GetCurrentBranch

func (c *Client) GetCurrentBranch(ctx context.Context, repoPath string) (string, error)

GetCurrentBranch returns the current branch name

func (*Client) GetCurrentBranchSymbolic added in v0.12.7

func (c *Client) GetCurrentBranchSymbolic(ctx context.Context, repoPath string) (string, error)

GetCurrentBranchSymbolic returns the current branch name using symbolic-ref, which works even on empty repos (no commits yet).

func (*Client) GetDefaultBranch added in v1.8.0

func (c *Client) GetDefaultBranch(ctx context.Context, repoPath string) (string, error)

GetDefaultBranch returns the remote's default branch (e.g. "main") by reading the origin/HEAD symbolic ref that `git clone` records. Use this instead of the clone's current HEAD when you need the repo's real base branch: the cached clone is long-lived and may be left checked out on some other branch.

func (*Client) GetRemoteURL

func (c *Client) GetRemoteURL(ctx context.Context, repoPath string) (string, error)

GetRemoteURL returns the remote URL for the repository (typically 'origin')

func (*Client) HasStagedChanges added in v0.5.5

func (c *Client) HasStagedChanges(ctx context.Context, repoPath string) (bool, error)

HasStagedChanges checks if there are staged changes ready to be committed

func (*Client) IsEmpty added in v0.12.7

func (c *Client) IsEmpty(ctx context.Context, repoPath string) (bool, error)

IsEmpty checks if a repository has no commits (e.g., freshly cloned empty repo)

func (*Client) LsRemote

func (c *Client) LsRemote(ctx context.Context, repoURL, ref string) (string, error)

LsRemote queries a remote repository for a specific ref Returns the commit hash for the ref

func (*Client) Pull

func (c *Client) Pull(ctx context.Context, repoPath string) error

Pull pulls changes from the remote repository

func (*Client) PullRebase added in v0.15.0

func (c *Client) PullRebase(ctx context.Context, repoPath string) error

PullRebase pulls changes from the remote repository and rebases local commits on top. Used by runInVaultTx to resolve concurrent pushes from multiple sx processes writing to the same management files.

func (*Client) Push

func (c *Client) Push(ctx context.Context, repoPath string) error

Push pushes changes to the remote repository

func (*Client) PushSetUpstream added in v0.12.7

func (c *Client) PushSetUpstream(ctx context.Context, repoPath, branch string) error

PushSetUpstream pushes a branch and sets its upstream tracking ref. Used for the first push to an empty repo and for the (uniquely named, never-preexisting) PR branch — neither needs --force, so this is a plain non-forcing push and must stay that way; force-pushing here would let a caller silently clobber a remote branch that already exists.

func (*Client) Reset added in v1.5.0

func (c *Client) Reset(ctx context.Context, repoPath, mode, ref string) error

Reset runs `git reset --<mode> <ref>` in repoPath. Used by vault-clone repair to move the branch pointer back to the remote tip.

func (*Client) RestoreExcept added in v1.5.0

func (c *Client) RestoreExcept(ctx context.Context, repoPath, excludeDir string) error

RestoreExcept restores all tracked files in both the index and worktree to HEAD, except those under excludeDir. Used by vault-clone repair to discard local manifest/asset divergence while leaving the queued usage appends under excludeDir staged for a follow-up commit.

func (*Client) RevParse

func (c *Client) RevParse(ctx context.Context, repoPath, ref string) (string, error)

RevParse resolves a ref to a commit hash in a local repository

type ClientOption added in v1.3.0

type ClientOption func(*Client)

func WithCommitActor added in v1.3.0

func WithCommitActor(name, email string) ClientOption

func WithEnv added in v1.3.0

func WithEnv(env ...string) ClientOption

func WithHTTPBasicAuth added in v1.3.0

func WithHTTPBasicAuth(scheme, host, username, password string) ClientOption

func WithHTTPSBasicAuth added in v1.3.0

func WithHTTPSBasicAuth(host, username, password string) ClientOption

func WithSSHKey added in v1.3.0

func WithSSHKey(path string) ClientOption

WithSSHKey overrides the SSH key path that would otherwise be inherited from the process-global value set by SetSSHKeyPath. Library consumers that don't go through the CLI flag/env wiring should use this to scope an SSH key to a single git.Client.

type RemoteAuthInfo added in v1.3.0

type RemoteAuthInfo struct {
	Host   string
	Scheme string
	HTTP   bool
	SSH    bool
}

RemoteAuthInfo describes the host and transport parsed from a git remote URL.

func ParseRemoteAuthInfo added in v1.3.0

func ParseRemoteAuthInfo(repoURL string) RemoteAuthInfo

ParseRemoteAuthInfo extracts the auth-relevant host and transport from a git remote URL. It supports HTTP(S) URLs, ssh:// URLs, and scp-like SSH remotes such as git@github.com:owner/repo.git.

Jump to

Keyboard shortcuts

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