git

package
v0.3.1 Latest Latest
Warning

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

Go to latest
Published: Feb 3, 2026 License: MIT Imports: 10 Imported by: 0

Documentation

Overview

Package git provides a git client with credential helper support. Pattern inspired by github.com/cli/cli

Index

Constants

This section is empty.

Variables

View Source
var AllMatchingCredentialsPattern = CredentialPattern{/* contains filtered or unexported fields */}

AllMatchingCredentialsPattern matches all hosts

Functions

func ExtractRepoName

func ExtractRepoName(rawURL string) string

ExtractRepoName extracts the repository name from a URL

func FindGitDir

func FindGitDir(startPath string) (string, error)

FindGitDir finds the .git directory from a path

func GetExitCode added in v0.3.0

func GetExitCode(err error) int

GetExitCode returns the exit code from a git error, or -1 if not available

func IsAlreadyExists added in v0.3.0

func IsAlreadyExists(err error) bool

IsAlreadyExists checks if the error indicates something already exists

func IsAuthRequired added in v0.3.0

func IsAuthRequired(err error) bool

IsAuthRequired checks if the error indicates authentication is required

func IsConflict added in v0.3.0

func IsConflict(err error) bool

IsConflict checks if the error indicates a merge conflict

func IsDetachedHead added in v0.3.0

func IsDetachedHead(err error) bool

IsDetachedHead checks if the error indicates HEAD is detached

func IsNoUpstream added in v0.3.0

func IsNoUpstream(err error) bool

IsNoUpstream checks if the error indicates no upstream branch configured

func IsNotRepository added in v0.3.0

func IsNotRepository(err error) bool

IsNotRepository checks if the error indicates not a git repository

func IsNothingToCommit added in v0.3.0

func IsNothingToCommit(err error) bool

IsNothingToCommit checks if the error indicates nothing to commit

func IsRefNotFound added in v0.3.0

func IsRefNotFound(err error) bool

IsRefNotFound checks if the error indicates a ref was not found

func ParseURL

func ParseURL(rawURL string) (*url.URL, error)

ParseURL parses a git URL, including SCP-like syntax

func ReadCredentialRequest

func ReadCredentialRequest(r io.Reader) (map[string]string, error)

ReadCredentialRequest reads a credential request from git

func RepoRoot

func RepoRoot(ctx context.Context) (string, error)

RepoRoot finds the root directory of the current git repository

func WriteCredential

func WriteCredential(w io.Writer, host, username, password string) error

WriteCredential writes credentials in git credential helper format

Types

type BranchInfo added in v0.3.0

type BranchInfo struct {
	Name       string
	Current    bool
	Upstream   string
	LastCommit string
	Gone       bool // Upstream is gone
}

BranchInfo contains detailed branch information

type CheckoutOptions

type CheckoutOptions struct {
	Create bool // Create new branch
	Force  bool // Force checkout
}

CheckoutOptions configures checkout behavior

type Client

type Client struct {
	ClonrPath string // Path to clonr executable (for credential helper)
	RepoDir   string // Repository directory
	GitPath   string // Path to git executable
	Stderr    io.Writer
	Stdin     io.Reader
	Stdout    io.Writer
}

Client wraps git operations with authentication support

func NewClient

func NewClient() *Client

NewClient creates a new git client

func NewClientForRepo

func NewClientForRepo(repoDir string) *Client

NewClientForRepo creates a client for a specific repository

func (*Client) AddRemote added in v0.3.0

func (c *Client) AddRemote(ctx context.Context, name, url string) error

AddRemote adds a new remote

func (*Client) AuthenticatedCommand

func (c *Client) AuthenticatedCommand(ctx context.Context, pattern CredentialPattern, args ...string) *exec.Cmd

AuthenticatedCommand creates a git command with credential helper configured Note: Do not set Stdout/Stderr if you plan to use CombinedOutput()

func (*Client) Checkout

func (c *Client) Checkout(ctx context.Context, target string, opts CheckoutOptions) error

Checkout checks out a branch or commit

func (*Client) Clone

func (c *Client) Clone(ctx context.Context, cloneURL, targetPath string) error

Clone clones a repository with authentication

func (*Client) Command

func (c *Client) Command(ctx context.Context, args ...string) *exec.Cmd

Command creates a git command without authentication Note: Do not set Stdout/Stderr if you plan to use CombinedOutput()

func (*Client) CommandInteractive

func (c *Client) CommandInteractive(ctx context.Context, args ...string) *exec.Cmd

CommandInteractive creates a git command with stdio attached for interactive use

func (*Client) Commit

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

Commit creates a commit

func (*Client) CurrentBranch

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

CurrentBranch returns the current branch name

func (*Client) DeleteBranch added in v0.3.0

func (c *Client) DeleteBranch(ctx context.Context, name string, force bool) error

DeleteBranch deletes a branch

func (*Client) Diff added in v0.3.0

func (c *Client) Diff(ctx context.Context, opts DiffOptions) (string, error)

Diff returns the diff output

func (*Client) Fetch

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

Fetch fetches changes with authentication

func (*Client) GetHead added in v0.3.0

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

GetHead returns the current HEAD reference

func (*Client) GetRemoteURL

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

GetRemoteURL returns the URL for a remote

func (*Client) GetShortHead added in v0.3.0

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

GetShortHead returns the short form of current HEAD

func (*Client) IsRepository

func (c *Client) IsRepository(ctx context.Context) bool

IsRepository checks if the current directory is a git repository

func (*Client) ListBranches

func (c *Client) ListBranches(ctx context.Context, all bool) ([]string, error)

ListBranches lists branches

func (*Client) ListBranchesDetailed added in v0.3.0

func (c *Client) ListBranchesDetailed(ctx context.Context, all bool) ([]BranchInfo, error)

ListBranchesDetailed lists branches with detailed information

func (*Client) ListRemotes added in v0.3.0

func (c *Client) ListRemotes(ctx context.Context) ([]Remote, error)

ListRemotes lists all configured remotes

func (*Client) Log added in v0.3.0

func (c *Client) Log(ctx context.Context, opts LogOptions) ([]Commit, error)

Log returns the commit log

func (*Client) LogOneline added in v0.3.0

func (c *Client) LogOneline(ctx context.Context, limit int) (string, error)

LogOneline returns a simple one-line log output

func (*Client) Merge

func (c *Client) Merge(ctx context.Context, branch string, opts MergeOptions) error

Merge merges a branch

func (*Client) NewGitCommand added in v0.3.0

func (c *Client) NewGitCommand(ctx context.Context, args ...string) *GitCommand

NewGitCommand creates a new GitCommand

func (*Client) Pull

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

Pull pulls changes with authentication

func (*Client) Push

func (c *Client) Push(ctx context.Context, remote, branch string, opts PushOptions) error

Push pushes changes with authentication

func (*Client) RemoveRemote added in v0.3.0

func (c *Client) RemoveRemote(ctx context.Context, name string) error

RemoveRemote removes a remote

func (*Client) Stash

func (c *Client) Stash(ctx context.Context, opts StashOptions) error

Stash stashes changes

func (*Client) StashDrop

func (c *Client) StashDrop(ctx context.Context, stash string) error

StashDrop drops a stash

func (*Client) StashList

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

StashList lists all stashes

func (*Client) StashPop

func (c *Client) StashPop(ctx context.Context) error

StashPop pops the latest stash

func (*Client) Status

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

Status returns the git status

func (*Client) StatusPorcelain added in v0.3.0

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

StatusPorcelain returns machine-readable status

func (*Client) Tag

func (c *Client) Tag(ctx context.Context, name, message string) error

Tag creates a git tag

type CommandModifier added in v0.3.0

type CommandModifier func(*exec.Cmd)

CommandModifier is a function that can modify a git command before execution

type Commit added in v0.3.0

type Commit struct {
	SHA      string
	ShortSHA string
	Author   string
	Email    string
	Date     string
	Subject  string
	Body     string
	Refs     string
}

Commit represents a git commit

type CommitOptions

type CommitOptions struct {
	All bool // Stage all modified files before committing
}

CommitOptions configures commit behavior

type CredentialPattern

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

CredentialPattern represents a pattern for credential matching

func CredentialPatternFromGitURL

func CredentialPatternFromGitURL(gitURL string) (CredentialPattern, error)

CredentialPatternFromGitURL derives a credential pattern from a git URL

func CredentialPatternFromHost

func CredentialPatternFromHost(host string) CredentialPattern

CredentialPatternFromHost creates a pattern for a specific host

type CredentialStore

type CredentialStore interface {
	Get(host string) (username, password string, err error)
}

CredentialStore provides credential storage interface

type DiffOptions added in v0.3.0

type DiffOptions struct {
	Staged     bool   // Show staged changes (--cached)
	Stat       bool   // Show diffstat (--stat)
	NameOnly   bool   // Only show file names
	NameStatus bool   // Show file names and status
	Commit     string // Diff against a specific commit
	Path       string // Limit to a specific path
}

DiffOptions configures diff output

type FileStatus added in v0.3.0

type FileStatus struct {
	Path   string
	Status string // M=modified, A=added, D=deleted, R=renamed, C=copied, U=updated
}

FileStatus represents the status of a single file

type GitCommand added in v0.3.0

type GitCommand struct {
	*exec.Cmd
	// contains filtered or unexported fields
}

GitCommand wraps exec.Cmd with additional functionality

func (*GitCommand) Apply added in v0.3.0

func (gc *GitCommand) Apply() *exec.Cmd

Apply applies all modifiers to the command

func (*GitCommand) CombinedOutput added in v0.3.0

func (gc *GitCommand) CombinedOutput() ([]byte, error)

CombinedOutput applies modifiers and returns combined output

func (*GitCommand) Output added in v0.3.0

func (gc *GitCommand) Output() ([]byte, error)

Output applies modifiers and returns the output

func (*GitCommand) Run added in v0.3.0

func (gc *GitCommand) Run() error

Run applies modifiers and runs the command

func (*GitCommand) WithDir added in v0.3.0

func (gc *GitCommand) WithDir(dir string) *GitCommand

WithDir sets the working directory for the command

func (*GitCommand) WithEnv added in v0.3.0

func (gc *GitCommand) WithEnv(env ...string) *GitCommand

WithEnv adds environment variables to the command

func (*GitCommand) WithStdio added in v0.3.0

func (gc *GitCommand) WithStdio(stdin io.Reader, stdout, stderr io.Writer) *GitCommand

WithStdio attaches stdin, stdout, stderr to the command

type GitError

type GitError struct {
	ExitCode int
	Stderr   string
	Args     []string
	// contains filtered or unexported fields
}

GitError represents a git command error

func NewGitError added in v0.3.0

func NewGitError(args []string, stderr string, err error) *GitError

NewGitError creates a GitError from command output and error

func (*GitError) Error

func (e *GitError) Error() string

func (*GitError) Unwrap

func (e *GitError) Unwrap() error

type LogOptions added in v0.3.0

type LogOptions struct {
	Limit   int
	Oneline bool
	All     bool
	Format  string // Custom format string
	Since   string // Show commits more recent than this date
	Until   string // Show commits older than this date
	Author  string // Filter by author
	Grep    string // Filter by commit message
}

LogOptions configures log output

type MergeOptions

type MergeOptions struct {
	NoFastForward bool
	Squash        bool
	Message       string
}

MergeOptions configures merge behavior

type PushOptions

type PushOptions struct {
	SetUpstream bool
	Force       bool
	Tags        bool
}

PushOptions configures push behavior

type Remote added in v0.3.0

type Remote struct {
	Name     string
	FetchURL string
	PushURL  string
}

Remote represents a git remote

type StashOptions

type StashOptions struct {
	Message          string
	IncludeUntracked bool
	KeepIndex        bool
}

StashOptions configures stash behavior

type StatusInfo added in v0.3.0

type StatusInfo struct {
	Branch       string
	Upstream     string
	Ahead        int
	Behind       int
	Staged       []FileStatus
	Unstaged     []FileStatus
	Untracked    []string
	HasChanges   bool
	HasUntracked bool
	HasConflicts bool
}

StatusInfo contains detailed status information

Jump to

Keyboard shortcuts

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