Documentation
¶
Overview ¶
Package git provides Git operations for repository management
Index ¶
- Constants
- Variables
- type Commander
- type Git
- func (g *Git) Clone(ctx context.Context, url, targetDir string) error
- func (g *Git) FetchAll(ctx context.Context, repoDir string) error
- func (g *Git) FetchPullRefs(ctx context.Context, repoDir string) error
- func (g *Git) Fsck(ctx context.Context, repoDir string) error
- func (g *Git) GetDefaultBranch(ctx context.Context, repoDir string) (string, error)
- func (g *Git) GetHEAD(ctx context.Context, repoDir string) (string, error)
- func (g *Git) GetLastFetchTime(repoDir string) (time.Time, error)
- func (g *Git) GetRemoteBranchSHA(ctx context.Context, repoDir, remote, branch string) (string, error)
- func (g *Git) GetRemoteURL(ctx context.Context, repoDir string) (string, error)
- func (g *Git) IsGitRepo(dir string) bool
- func (g *Git) LsRemoteDefaultBranch(ctx context.Context, url string) (string, error)
- func (g *Git) LsRemoteHeads(ctx context.Context, url string) (map[string]string, error)
- func (g *Git) LsRemotePulls(ctx context.Context, url string) (map[string]string, error)
- func (g *Git) PullRefs(ctx context.Context, repoDir string) (map[string]string, error)
- func (g *Git) RemoteBranches(ctx context.Context, repoDir string) (map[string]string, error)
- func (g *Git) SubmoduleStatus(ctx context.Context, repoDir string) ([]Submodule, error)
- func (g *Git) SubmoduleUpdate(ctx context.Context, repoDir string) error
- func (g *Git) UpdateWorkTree(ctx context.Context, repoDir, branch string) (moved bool, err error)
- type LFS
- func (l *LFS) EnsureLFS(ctx context.Context) error
- func (l *LFS) Fetch(ctx context.Context, repoDir string) error
- func (l *LFS) FetchAll(ctx context.Context, repoDir string) error
- func (l *LFS) Initialize(ctx context.Context) error
- func (l *LFS) Install(ctx context.Context) error
- func (l *LFS) IsInstalled() bool
- func (l *LFS) MissingLFSObjects(ctx context.Context, repoDir string) ([]string, error)
- func (l *LFS) Pull(ctx context.Context, repoDir string) error
- func (l *LFS) RepoUsesLFS(repoDir string) bool
- type MockCommander
- type MockCommanderCall
- type Submodule
Constants ¶
const PullRefSpec = "+refs/pull/*/head:refs/pull/*/head"
PullRefSpec maps GitHub's pull request heads to the same names locally
Variables ¶
var ( ErrGitNotInstalled = errors.New("git is not installed or not in PATH") ErrNotAGitRepo = errors.New("not a git repository") ErrCloneFailed = errors.New("git clone failed") ErrPullFailed = errors.New("git pull failed") ErrFetchFailed = errors.New("git fetch failed") ErrSubmoduleFailed = errors.New("git submodule update failed") )
Common errors
var ( ErrLFSNotInstalled = errors.New("git-lfs is not installed") ErrLFSInstallFail = errors.New("failed to install git-lfs") ErrLFSFetchFailed = errors.New("git lfs fetch failed") )
Common LFS errors
var ( ErrLsRemoteFailed = errors.New("git ls-remote failed") ErrFsckFailed = errors.New("git fsck failed") ErrLFSListFailed = errors.New("git lfs ls-files failed") )
Errors from the backup checks
var ( ErrDirtyWorkTree = errors.New("local changes, working tree not updated") ErrCheckoutFailed = errors.New("working tree not updated") )
Errors from updating the working tree
var ErrSubmoduleStatusFailed = errors.New("git submodule status failed")
ErrSubmoduleStatusFailed is returned when git can't report on the submodules
Functions ¶
This section is empty.
Types ¶
type Commander ¶
type Commander interface {
// Run executes a git command and returns an error if it fails
Run(ctx context.Context, dir string, args ...string) error
// Output executes a git command and returns its output
Output(ctx context.Context, dir string, args ...string) (string, error)
}
Commander provides an interface for executing git commands This interface is used for mocking in tests
type Git ¶
type Git struct {
// GitPath is the path to the git executable
GitPath string
// Quiet suppresses stdout/stderr output (for TUI mode)
Quiet bool
// Token is the authentication token for HTTPS operations
Token string
// Host is the GitHub host the token is sent to (default github.com)
Host string
}
Git provides Git operations
func NewQuietWithToken ¶
NewQuietWithToken creates a new Git instance that suppresses output and uses auth token
func NewWithToken ¶
NewWithToken creates a new Git instance with authentication token
func (*Git) FetchAll ¶
FetchAll fetches all branches from all remotes with pruning. This updates all remote-tracking branches without modifying the working directory. Use --prune to remove local references to branches deleted on remote.
func (*Git) FetchPullRefs ¶ added in v1.1.1
FetchPullRefs fetches the head commit of every pull request into refs/pull/<n>/head. These keep commits of fork and deleted branches. They are never pruned. FETCH_HEAD is left alone, since fast sync reads its time as the last full fetch.
func (*Git) GetDefaultBranch ¶
GetDefaultBranch returns the default branch of a repository
func (*Git) GetLastFetchTime ¶
GetLastFetchTime returns the modification time of .git/FETCH_HEAD, which indicates when the repository was last fetched. Returns zero time if never fetched.
func (*Git) GetRemoteBranchSHA ¶
func (g *Git) GetRemoteBranchSHA(ctx context.Context, repoDir, remote, branch string) (string, error)
GetRemoteBranchSHA returns the SHA of a remote-tracking branch (e.g., origin/main)
func (*Git) GetRemoteURL ¶
GetRemoteURL returns the remote origin URL for a repository
func (*Git) LsRemoteDefaultBranch ¶ added in v1.1.1
LsRemoteDefaultBranch returns the branch the remote's HEAD points at, or "" for an empty repo
func (*Git) LsRemoteHeads ¶ added in v1.1.1
LsRemoteHeads returns branch name -> head SHA as the remote has them
func (*Git) LsRemotePulls ¶ added in v1.1.1
LsRemotePulls returns pull request number -> head SHA as the remote has them
func (*Git) PullRefs ¶ added in v1.1.1
PullRefs returns pull request number -> SHA of the local refs/pull/*/head refs
func (*Git) RemoteBranches ¶ added in v1.1.1
RemoteBranches returns branch name -> SHA of the local origin/* remote-tracking refs
func (*Git) SubmoduleStatus ¶ added in v1.1.1
SubmoduleStatus lists every submodule, nested ones too, with its state. Repos without .gitmodules have none.
func (*Git) SubmoduleUpdate ¶ added in v1.1.1
SubmoduleUpdate syncs submodule URLs and checks out all submodules (recursively) of a repository. The token goes along per command, and SSH submodule URLs are fetched over HTTPS, since a backup host usually has no SSH keys. Repos without .gitmodules are a no-op.
func (*Git) UpdateWorkTree ¶ added in v1.1.1
UpdateWorkTree moves the checkout to origin/<branch>: it switches to branch if another one is checked out, then fast-forwards it. It never resets anything: with local changes it returns ErrDirtyWorkTree, and a branch that can't fast-forward returns ErrCheckoutFailed. moved is true when HEAD changed. A branch that was never fetched (an empty repo) is left alone.
type LFS ¶
type LFS struct {
// LFSPath is the path to the git-lfs executable
LFSPath string
// Git is the underlying git instance
Git *Git
// Quiet suppresses stdout/stderr output (inherited from Git)
Quiet bool
}
LFS provides Git LFS operations
func (*LFS) FetchAll ¶ added in v1.1.1
FetchAll runs 'git lfs fetch --all' to download the LFS objects of every ref, not just the checked-out one, so a backup has all of them
func (*LFS) Initialize ¶
Initialize runs 'git lfs install' to set up Git LFS hooks
func (*LFS) IsInstalled ¶
IsInstalled checks if git-lfs is installed
func (*LFS) MissingLFSObjects ¶ added in v1.1.1
MissingLFSObjects returns the LFS objects referenced anywhere in history that are not stored locally with the right content. It needs git-lfs.
func (*LFS) RepoUsesLFS ¶
RepoUsesLFS checks if a repository uses Git LFS
type MockCommander ¶
type MockCommander struct {
// RunFunc is called when Run is invoked
RunFunc func(ctx context.Context, dir string, args ...string) error
// OutputFunc is called when Output is invoked
OutputFunc func(ctx context.Context, dir string, args ...string) (string, error)
// Calls records all method calls for verification
Calls []MockCommanderCall
}
MockCommander is a mock implementation of Commander for testing
func NewMockCommander ¶
func NewMockCommander() *MockCommander
NewMockCommander creates a new mock commander
func (*MockCommander) CallCount ¶
func (m *MockCommander) CallCount(method string) int
CallCount returns the number of times a method was called
type MockCommanderCall ¶
MockCommanderCall records a method call
type Submodule ¶ added in v1.1.1
type Submodule struct {
// Path is relative to the top repo, nested submodules included
Path string
// SHA is the commit the parent records for it
SHA string
// State is ' ' when checked out at SHA, '-' when not checked out,
// '+' when at another commit and 'U' when it has merge conflicts
State byte
}
Submodule is one line of git submodule status