git

package
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Jun 21, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type CommitInfo

type CommitInfo = types.CommitInfo

CommitInfo is a type alias for the canonical definition in internal/types.

type FileStatus

type FileStatus struct {
	Status    string // e.g. "M", "A", "D", "R", "C", "U", "?"
	Path      string
	OldPath   string // for renames
	Additions int
	Deletions int
}

FileStatus represents a single file's git status.

type Git

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

Git wraps git operations for a working directory.

func New

func New(workDir string) *Git

New creates a Git instance for the given working directory.

func (*Git) AbsPath

func (g *Git) AbsPath(rel string) string

AbsPath returns the absolute path for a relative path within the working directory.

func (*Git) Add

func (g *Git) Add(paths ...string) error

Add stages the given paths.

func (*Git) AddAll

func (g *Git) AddAll() error

AddAll stages all changes including deletions.

func (*Git) BranchList

func (g *Git) BranchList() ([]string, error)

BranchList returns a list of local branches.

func (*Git) CheckoutBranch

func (g *Git) CheckoutBranch(name string, create bool) error

CheckoutBranch checks out a branch, optionally creating it.

func (*Git) Commit

func (g *Git) Commit(message string) error

Commit stages all changes and creates a commit with the given message. WARNING: This stages the entire worktree. Use CommitWithFiles or CommitStaged for scoped commits.

func (*Git) CommitStaged

func (g *Git) CommitStaged(message string) (string, error)

CommitStaged creates a commit from already-staged changes only. Does not stage any additional files.

func (*Git) CommitWithFiles

func (g *Git) CommitWithFiles(message string, paths ...string) (string, error)

CommitWithFiles stages and commits only the given paths with the given message. Returns the commit hash on success.

func (*Git) ConfigUser

func (g *Git) ConfigUser(name, email string) error

ConfigUser sets the git user name and email for the repository.

func (*Git) CountCommits

func (g *Git) CountCommits(startHash, endHash string) (int, error)

CountCommits returns the number of commits between two hashes using `git rev-list --count`. startHash is the older commit, endHash the newer.

func (*Git) CreateBranch

func (g *Git) CreateBranch(name string) error

CreateBranch creates a new branch with the given name.

func (*Git) CurrentBranch

func (g *Git) CurrentBranch() (string, error)

CurrentBranch returns the name of the current branch.

func (*Git) Diff

func (g *Git) Diff(target ...string) (string, error)

Diff returns git diff output based on the number of target refs provided:

  • 0 args: unstaged working-tree diff (plain `git diff`)
  • 1 arg: diff between target and HEAD
  • 2 args: diff between the two refs

func (*Git) DiffFile

func (g *Git) DiffFile(path string, status string) (string, error)

DiffFile returns the diff for a single file. For untracked files ("?"), it returns the file contents as an "added" diff. For staged files it uses --cached; otherwise it shows unstaged working-tree changes.

func (*Git) DiffRefs

func (g *Git) DiffRefs(ref1, ref2 string) (string, error)

DiffRefs returns the diff between two refs. If both refs are empty, it runs plain `git diff` to show unstaged working-tree changes. If only ref2 is empty, it produces "ref1.." which in git means "changes reachable from ref1 that are not in the current working tree state" — callers that want the contents of a single commit should pass ref1^ and ref1 explicitly.

func (*Git) DiffStaged

func (g *Git) DiffStaged() (string, error)

DiffStaged returns the diff of staged but uncommitted changes.

func (*Git) DiffStat

func (g *Git) DiffStat(target ...string) (string, error)

DiffStat returns diff stat (summary) based on the number of target refs provided.

func (*Git) Fetch

func (g *Git) Fetch(remote string) error

Fetch fetches from a remote repository.

func (*Git) HasUncommittedChanges

func (g *Git) HasUncommittedChanges() (bool, error)

HasUncommittedChanges returns true if the working tree has uncommitted changes (modified, staged, untracked, or deleted files).

func (*Git) HeadHash

func (g *Git) HeadHash() (string, error)

HeadHash returns the current HEAD commit hash.

func (*Git) Init

func (g *Git) Init() error

Init initializes a new git repository in the working directory.

func (*Git) IsDirty

func (g *Git) IsDirty() (bool, error)

IsDirty returns true if the working tree has any uncommitted changes.

func (*Git) IsRepo

func (g *Git) IsRepo() bool

IsRepo returns true if the working directory is a git repository.

func (*Git) Log

func (g *Git) Log(lastN int) ([]CommitInfo, error)

Log returns the lastN commits (newest first). If lastN <= 0, returns all.

func (*Git) LogAll

func (g *Git) LogAll() ([]CommitInfo, error)

LogAll returns full commit history (newest first).

func (*Git) LogSince

func (g *Git) LogSince(since time.Time) ([]CommitInfo, error)

LogSince returns commits since the given time (newest first).

func (*Git) Merge

func (g *Git) Merge(branch string) error

Merge merges a branch into the current branch.

func (*Git) Pull

func (g *Git) Pull(remote, branch string, rebase bool) error

Pull pulls from a remote, optionally rebasing.

func (*Git) Push

func (g *Git) Push(remote, branch string) error

Push pushes to a remote repository.

func (*Git) RemoteTracking

func (g *Git) RemoteTracking() (string, error)

RemoteTracking returns the remote tracking branch, e.g. "origin/main". Returns empty string if no upstream is configured.

func (*Git) ResetHard

func (g *Git) ResetHard(commit string) error

ResetHard resets HEAD to the given commit, discarding all changes. Creates a backup branch for rollback safety.

func (*Git) ResetSoft

func (g *Git) ResetSoft(commit string) error

ResetSoft resets HEAD to the given commit, keeping changes staged. Creates a backup branch for rollback safety.

func (*Git) RevParse

func (g *Git) RevParse(ref string) (string, error)

RevParse resolves a git ref to its full SHA hash.

func (*Git) Run

func (g *Git) Run(args ...string) (string, error)

Run executes a git command and returns the output. This is the exported version of run for commands not covered by specific wrapper methods.

func (*Git) StashApply

func (g *Git) StashApply(index int) error

StashApply applies a stash entry by index (0 = most recent).

func (*Git) StashList

func (g *Git) StashList() ([]string, error)

StashList returns a list of stash entries.

func (*Git) StashPop

func (g *Git) StashPop() error

StashPop applies the most recent stash entry.

func (*Git) StashPush

func (g *Git) StashPush(message string) error

StashPush saves current changes to the stash with the given message.

func (*Git) Status

func (g *Git) Status() (string, error)

Status returns the git status output.

func (*Git) StatusPorcelain

func (g *Git) StatusPorcelain() ([]FileStatus, error)

StatusPorcelain returns structured git status for the working directory. Uses git status --porcelain (v1 format): "XY path" per line. Runs status and numstat concurrently for better performance (PERF-32).

func (*Git) Tag

func (g *Git) Tag(name, msg string) error

Tag creates a lightweight or annotated tag.

func (*Git) WorkDir

func (g *Git) WorkDir() string

WorkDir returns the working directory path.

Jump to

Keyboard shortcuts

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