Documentation
¶
Overview ¶
Package git is a generated GoMock package.
Index ¶
- Variables
- func CreateWorktree(repoDir, targetCommit string) (string, error)
- func CreateWorktreeWithFetchRecovery(repoDir, targetCommit, targetBranch string) (string, error)
- func DeepenFetch(repoDir, branch string, depth int) error
- func EnsureGitSafeDirectory() error
- func FetchRef(repoDir, branch string) error
- func GetLocalRepo() (*git.Repository, error)
- func GetRepoConfig(repo *git.Repository) (*config.Config, error)
- func GetWorktreeParentDir(worktreePath string) string
- func MergeBase(repoDir, targetBranch string) (string, error)
- func MergeBaseWithAutoFetch(repoDir, targetBranch string) (string, error)
- func OpenWorktreeAwareRepo(path string) (*git.Repository, error)
- func RemoveWorktree(repoDir, worktreePath string)
- type DefaultGitRepo
- type DefaultRepositoryOperations
- type GitRepoInterface
- type MockRepositoryOperations
- type MockRepositoryOperationsMockRecorder
- type RepoInfo
- type RepositoryOperations
Constants ¶
This section is empty.
Variables ¶
var ErrHeadOnTargetBranch = fmt.Errorf("HEAD is on target branch (merge-base equals HEAD)")
ErrHeadOnTargetBranch is returned when HEAD is already on the target branch (e.g., the merge commit was checked out), making merge-base equal to HEAD itself.
var ErrInvalidBranchName = errors.New("invalid branch name")
ErrInvalidBranchName indicates a branch name contains invalid characters.
var ErrNoCommonAncestor = fmt.Errorf("no common ancestor found")
ErrNoCommonAncestor is returned when no merge-base exists between two commits.
Functions ¶
func CreateWorktree ¶ added in v1.203.0
CreateWorktree creates a new git worktree at the specified path, checked out to the given target ref or SHA. This uses `git worktree add --detach` to create an isolated worktree that shares the repository's object database but has its own HEAD, allowing checkout operations without affecting the main worktree. The repoDir should be the path to any directory in the repository (main worktree or any linked worktree). Returns the path to the created worktree directory.
func CreateWorktreeWithFetchRecovery ¶
CreateWorktreeWithFetchRecovery creates a worktree at targetCommit, with a one-shot self-heal: if the initial CreateWorktree call fails because the target commit is missing from the local object DB AND a non-empty targetBranch is provided, the function performs a targeted `git fetch origin <targetBranch>` and retries.
This is the common shallow-clone CI scenario: actions/checkout@v4 with the default fetch-depth=1 only pulls the PR head, so a base SHA resolved from the GitHub event payload (event.pull_request.base.sha) often is not in the local object DB. A targeted fetch of the target branch is enough to make the SHA available without paying for a full unshallow.
Recovery is gated to ErrGitRefNotFound so that unrelated failures (temp directory creation, repo state corruption, permissions, etc.) propagate directly instead of being misdiagnosed as "target commit not available locally" and noisily attempting an unrelated fetch.
On final failure, the original CreateWorktree error is preserved (joined with the fetch error if the fetch also failed) so the caller can still surface its hints to the user.
func DeepenFetch ¶
DeepenFetch deepens a shallow clone by fetching additional history for a branch. Used as a second-stage fetch when the initial FetchRef succeeds but merge-base still cannot find a common ancestor (i.e., the fork point is older than the initial fetch depth).
Depth is the number of commits to deepen by, measured from the current shallow boundary. A value of 0 or negative means "fetch full history" (equivalent to --unshallow).
func EnsureGitSafeDirectory ¶ added in v1.215.0
func EnsureGitSafeDirectory() error
EnsureGitSafeDirectory adds GITHUB_WORKSPACE to git's safe.directory list when running in a GitHub Actions container. Container jobs run as a different user than the checkout owner, causing git to reject the repo as "dubious ownership".
func FetchRef ¶
FetchRef fetches a single branch from the "origin" remote using a narrow refspec. This minimizes data transfer compared to a full fetch, which is important for CI shallow clones where remote-tracking refs may not exist locally. The repoDir should be a path inside the repository.
func GetLocalRepo ¶
func GetLocalRepo() (*git.Repository, error)
func GetRepoConfig ¶
func GetRepoConfig(repo *git.Repository) (*config.Config, error)
func GetWorktreeParentDir ¶ added in v1.203.0
GetWorktreeParentDir returns the parent directory of a worktree path. This is useful for cleanup since CreateWorktree creates a parent temp dir containing the worktree.
func MergeBase ¶ added in v1.214.0
MergeBase computes the common ancestor between HEAD and origin/<targetBranch> inside the repository at repoDir. This is the gold standard for determining the fork point of a PR branch, regardless of what commit is checked out or which merge strategy was used.
The repoDir argument is a path inside the target repository (the .git directory is auto-detected upward from this path). Use "." for the current working directory.
Returns an error if:
- the local repo cannot be opened
- origin/<targetBranch> does not exist (e.g., shallow checkout)
- no common ancestor exists between the two commits
- the merge-base equals HEAD (HEAD is on the target branch)
MergeBase is a pure read operation. It will not modify the local object database. Callers running in CI shallow checkouts should prefer MergeBaseWithAutoFetch, which transparently fetches the target branch and deepens history when needed.
func MergeBaseWithAutoFetch ¶
MergeBaseWithAutoFetch is a CI-aware wrapper around MergeBase that transparently recovers from shallow-clone failures by fetching the target branch and, if needed, deepening history.
RepoDir is a path inside the repository (used to scope the fetch).
Behavior:
- Run MergeBase. If it succeeds, return the SHA.
- If it fails because origin/<targetBranch> is not present locally, call FetchRef(repoDir, targetBranch) and retry MergeBase.
- If MergeBase still returns ErrNoCommonAncestor (the shallow boundary does not reach the fork point), call DeepenFetch with deepenStep and retry once. This is bounded — we deepen at most once.
- If recovery is impossible (no network, target branch does not exist remotely, etc.), return the original MergeBase error so the caller can fall through to its own fallback chain.
ErrHeadOnTargetBranch is propagated unchanged (no fetch can fix it).
func OpenWorktreeAwareRepo ¶ added in v1.191.0
func OpenWorktreeAwareRepo(path string) (*git.Repository, error)
OpenWorktreeAwareRepo opens a Git repository at the given path, handling both regular repositories and worktrees correctly. It uses EnableDotGitCommonDir to properly support worktrees with access to the main repository's config, remotes, and references.
func RemoveWorktree ¶ added in v1.203.0
func RemoveWorktree(repoDir, worktreePath string)
RemoveWorktree removes a git worktree using `git worktree remove`. This properly unregisters the worktree from git's tracking in addition to removing the directory. The repoDir parameter should be the path to any directory in the repository (main worktree or any linked worktree).
Types ¶
type DefaultGitRepo ¶ added in v1.192.0
type DefaultGitRepo struct{}
DefaultGitRepo is the default implementation of GitRepoInterface.
func (*DefaultGitRepo) GetCurrentCommitSHA ¶ added in v1.192.0
func (d *DefaultGitRepo) GetCurrentCommitSHA() (string, error)
GetCurrentCommitSHA returns the SHA of the current HEAD commit.
func (*DefaultGitRepo) GetLocalRepoInfo ¶ added in v1.192.0
func (d *DefaultGitRepo) GetLocalRepoInfo() (*RepoInfo, error)
GetLocalRepoInfo returns information about the local git repository.
func (*DefaultGitRepo) GetRepoInfo ¶ added in v1.192.0
func (d *DefaultGitRepo) GetRepoInfo(repo *git.Repository) (RepoInfo, error)
GetRepoInfo returns the repository information for the given git.Repository.
type DefaultRepositoryOperations ¶ added in v1.195.0
type DefaultRepositoryOperations struct{}
DefaultRepositoryOperations implements RepositoryOperations using real git operations.
func (*DefaultRepositoryOperations) GetLocalRepo ¶ added in v1.195.0
func (d *DefaultRepositoryOperations) GetLocalRepo() (*git.Repository, error)
GetLocalRepo opens the local git repository.
func (*DefaultRepositoryOperations) GetRepoInfo ¶ added in v1.195.0
func (d *DefaultRepositoryOperations) GetRepoInfo(localRepo *git.Repository) (RepoInfo, error)
GetRepoInfo extracts repository information.
type GitRepoInterface ¶ added in v1.192.0
type GitRepoInterface interface {
GetLocalRepoInfo() (*RepoInfo, error)
GetRepoInfo(repo *git.Repository) (RepoInfo, error)
GetCurrentCommitSHA() (string, error)
}
GitRepoInterface defines the interface for git repository operations.
func NewDefaultGitRepo ¶ added in v1.192.0
func NewDefaultGitRepo() GitRepoInterface
NewDefaultGitRepo creates a new instance of DefaultGitRepo.
type MockRepositoryOperations ¶ added in v1.195.0
type MockRepositoryOperations struct {
// contains filtered or unexported fields
}
MockRepositoryOperations is a mock of RepositoryOperations interface.
func NewMockRepositoryOperations ¶ added in v1.195.0
func NewMockRepositoryOperations(ctrl *gomock.Controller) *MockRepositoryOperations
NewMockRepositoryOperations creates a new mock instance.
func (*MockRepositoryOperations) EXPECT ¶ added in v1.195.0
func (m *MockRepositoryOperations) EXPECT() *MockRepositoryOperationsMockRecorder
EXPECT returns an object that allows the caller to indicate expected use.
func (*MockRepositoryOperations) GetLocalRepo ¶ added in v1.195.0
func (m *MockRepositoryOperations) GetLocalRepo() (*v5.Repository, error)
GetLocalRepo mocks base method.
func (*MockRepositoryOperations) GetRepoInfo ¶ added in v1.195.0
func (m *MockRepositoryOperations) GetRepoInfo(localRepo *v5.Repository) (RepoInfo, error)
GetRepoInfo mocks base method.
type MockRepositoryOperationsMockRecorder ¶ added in v1.195.0
type MockRepositoryOperationsMockRecorder struct {
// contains filtered or unexported fields
}
MockRepositoryOperationsMockRecorder is the mock recorder for MockRepositoryOperations.
func (*MockRepositoryOperationsMockRecorder) GetLocalRepo ¶ added in v1.195.0
func (mr *MockRepositoryOperationsMockRecorder) GetLocalRepo() *gomock.Call
GetLocalRepo indicates an expected call of GetLocalRepo.
func (*MockRepositoryOperationsMockRecorder) GetRepoInfo ¶ added in v1.195.0
func (mr *MockRepositoryOperationsMockRecorder) GetRepoInfo(localRepo interface{}) *gomock.Call
GetRepoInfo indicates an expected call of GetRepoInfo.
type RepoInfo ¶
type RepoInfo struct {
LocalRepoPath string
LocalWorktree *git.Worktree
LocalWorktreePath string
RepoUrl string
RepoOwner string
RepoName string
RepoHost string
}
func GetRepoInfo ¶
func GetRepoInfo(localRepo *git.Repository) (RepoInfo, error)
type RepositoryOperations ¶ added in v1.195.0
type RepositoryOperations interface {
// GetLocalRepo opens the local git repository.
GetLocalRepo() (*git.Repository, error)
// GetRepoInfo extracts repository information (URL, name, owner, host).
GetRepoInfo(localRepo *git.Repository) (RepoInfo, error)
}
RepositoryOperations defines operations for working with git repositories. This interface allows mocking of git operations in tests.