Documentation
¶
Overview ¶
Package git provides centralized utilities and command builders for invoking git.
Index ¶
- Constants
- func FindMainRepo(reposRoot, worktreeRoot, dir string) (string, error)
- func FindMainRepoAndBranchFromGit(wtPath string) (string, string, error)
- func FindRepoRoot(dir string) (string, error)
- func IsRepo(dir string) bool
- func ResolveCloneDir(reposRoot, uri string) (string, error)
- func ResolveMainRepoFromWorktree(reposRoot, worktreeRoot, wtPath string) (string, string, error)
- func ResolveRepoRelPath(reposRoot, repoDir string) (string, error)
- func ResolveUniqueMainRepos(reposRoot, worktreeRoot string, wtPaths []string) ([]string, error)
- func ResolveWorktreePath(reposRoot, worktreeRoot, repoDir, branch string) (string, error)
- func URI2RelPath(uri string) (string, error)
- type Command
- func (c *Command) BranchDelete(ctx context.Context, repoDir, branch string, force bool, stderr io.Writer) error
- func (c *Command) BranchDeleteCmd(branch string, force bool) []string
- func (c *Command) Checkout(ctx context.Context, dir, target string, stderr io.Writer) error
- func (c *Command) Clone(ctx context.Context, uri, targetDir string, stderr io.Writer) error
- func (c *Command) CloneCmd(uri, dir string) []string
- func (c *Command) CurrentBranch(ctx context.Context, dir string) (string, error)
- func (c *Command) DiffCmd(interactive bool, flags ...string) []string
- func (c *Command) EnsureCloned(ctx context.Context, reposRoot, uri string, stderr io.Writer) (string, error)
- func (c *Command) FetchCmd(flags ...string) []string
- func (c *Command) GitBin() string
- func (c *Command) GrepCmd(args ...string) []string
- func (c *Command) IsDirty(ctx context.Context, dir string) (bool, error)
- func (c *Command) IsMerged(ctx context.Context, repoDir, branch, target string) (bool, error)
- func (c *Command) LsFilesCmd(flags ...string) []string
- func (c *Command) Output(ctx context.Context, dir string, args ...string) (string, error)
- func (c *Command) PullCmd(flags ...string) []string
- func (c *Command) RemoteURL(ctx context.Context, dir string) (string, error)
- func (c *Command) ResolveDefaultBranch(ctx context.Context, dir string) (string, error)
- func (c *Command) Run(ctx context.Context, dir string, args []string, stdout, stderr io.Writer) error
- func (c *Command) StatusCmd(flags ...string) []string
- func (c *Command) WorktreeAdd(ctx context.Context, repoDir, wtPath, branch, newBranch string, ...) error
- func (c *Command) WorktreeAddCmd(wtPath, branch, newBranch string) []string
- func (c *Command) WorktreePrune(ctx context.Context, repoDir string, dryRun bool, stdout, stderr io.Writer) error
- func (c *Command) WorktreePruneCmd(dryRun bool) []string
- func (c *Command) WorktreeRemove(ctx context.Context, repoDir, wtPath string, force, noForce bool, ...) error
- func (c *Command) WorktreeRemoveCmd(wtPath string, force, noForce bool) []string
- type Runner
Constants ¶
const DefaultCommand = "git"
DefaultCommand is the default binary name for git.
Variables ¶
This section is empty.
Functions ¶
func FindMainRepo ¶ added in v0.4.0
FindMainRepo resolves the root directory of the main git repository for dir. If dir is within a linked git worktree, it resolves the main repository. If dir is within a normal git repository, it returns that repository's root.
func FindMainRepoAndBranchFromGit ¶ added in v0.4.0
FindMainRepoAndBranchFromGit discovers the main repository path and branch name by reading the git worktree metadata (.git file and commondir) directly.
func FindRepoRoot ¶ added in v0.4.0
FindRepoRoot walks up from dir until it finds a directory containing a ".git" entry. Returns the absolute path to the repository root.
func IsRepo ¶ added in v0.3.0
IsRepo reports whether dir contains a ".git" entry (file or directory).
func ResolveCloneDir ¶ added in v0.3.0
ResolveCloneDir resolves the absolute local directory path under reposRoot for uri.
func ResolveMainRepoFromWorktree ¶ added in v0.4.0
ResolveMainRepoFromWorktree resolves the main repository path (repos_root/DIR) and branch name from a worktree path (worktree_root/DIR/branch).
func ResolveRepoRelPath ¶ added in v0.4.0
ResolveRepoRelPath resolves the relative path of the git repository containing repoDir with respect to reposRoot (i.e. DIR in repos_root/DIR).
func ResolveUniqueMainRepos ¶ added in v0.4.0
ResolveUniqueMainRepos resolves the unique main repository paths under reposRoot corresponding to the given worktree paths under worktreeRoot, preserving order.
func ResolveWorktreePath ¶ added in v0.4.0
ResolveWorktreePath computes worktree_root/DIR/branch for a repository at repoDir under reposRoot.
func URI2RelPath ¶ added in v0.3.0
URI2RelPath converts a repository URI (HTTPS, HTTP, SSH, git://, SCP-like git@host:path, or host/path) into a relative directory path (<host>/<path> without trailing .git).
Types ¶
type Command ¶
type Command struct {
// Bin is the path or name of the git binary. Defaults to DefaultCommand ("git") if empty.
Bin string
// Runner executes the command.
Runner Runner
}
Command represents a git command configuration.
func (*Command) BranchDelete ¶ added in v0.4.0
func (c *Command) BranchDelete(ctx context.Context, repoDir, branch string, force bool, stderr io.Writer) error
BranchDelete deletes branch in repoDir. If force is true, it uses -D instead of -d.
func (*Command) BranchDeleteCmd ¶ added in v0.4.0
BranchDeleteCmd constructs argv for "git branch -d <branch>" (or "-D" when force is true).
func (*Command) Clone ¶ added in v0.3.0
Clone clones uri into targetDir, routing command output to stderr.
func (*Command) CloneCmd ¶ added in v0.3.0
CloneCmd constructs argv for "git clone -- <uri> <dir>" with the given URI and destination directory.
func (*Command) CurrentBranch ¶
CurrentBranch returns the currently checked-out branch in dir.
func (*Command) DiffCmd ¶
DiffCmd constructs argv for "git diff" with the given flags. If interactive is false, it injects "--no-pager" before "diff".
func (*Command) EnsureCloned ¶ added in v0.3.0
func (c *Command) EnsureCloned(ctx context.Context, reposRoot, uri string, stderr io.Writer) (string, error)
EnsureCloned resolves the local directory for uri under reposRoot and clones the repository if it does not already exist (as determined by IsRepo).
func (*Command) IsDirty ¶
IsDirty checks if the git repository in dir has uncommitted changes or untracked files.
func (*Command) IsMerged ¶ added in v0.4.0
IsMerged reports whether branch has been merged into target in repoDir. It checks standard ancestor merges first (git merge-base --is-ancestor), and falls back to tree comparison via git merge-tree --write-tree to detect squash merges.
func (*Command) LsFilesCmd ¶
LsFilesCmd constructs argv for "git ls-files" with the given flags.
func (*Command) Output ¶
Output executes a git command in dir and returns its trimmed stdout string.
func (*Command) RemoteURL ¶
RemoteURL returns the remote URL for origin in dir, or an empty string if not found.
func (*Command) ResolveDefaultBranch ¶
ResolveDefaultBranch identifies the default branch for the repository in dir: 1. refs/remotes/origin/HEAD symbolic ref (e.g. "origin/main" -> "main") 2. init.defaultBranch config 3. refs/heads/main exists 4. refs/heads/master exists
func (*Command) Run ¶
func (c *Command) Run(ctx context.Context, dir string, args []string, stdout, stderr io.Writer) error
Run executes a git command in dir with sub-arguments args.
func (*Command) StatusCmd ¶
StatusCmd constructs argv for "git status" with the given flags. If flags is empty, it defaults to []string{"--short"}.
func (*Command) WorktreeAdd ¶ added in v0.4.0
func (c *Command) WorktreeAdd(ctx context.Context, repoDir, wtPath, branch, newBranch string, stderr io.Writer) error
WorktreeAdd adds a worktree at wtPath for the repository in repoDir.
func (*Command) WorktreeAddCmd ¶ added in v0.4.0
WorktreeAddCmd constructs argv for "git worktree add". If newBranch is non-empty, it constructs "git worktree add -b <newBranch> -- <wtPath> <branch>". Otherwise, it constructs "git worktree add -- <wtPath> <branch>".
func (*Command) WorktreePrune ¶ added in v0.4.0
func (c *Command) WorktreePrune(ctx context.Context, repoDir string, dryRun bool, stdout, stderr io.Writer) error
WorktreePrune runs "git worktree prune [--dry-run]" in repoDir.
func (*Command) WorktreePruneCmd ¶ added in v0.4.0
WorktreePruneCmd constructs argv for "git worktree prune [--dry-run]".