git

package
v0.9.0 Latest Latest
Warning

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

Go to latest
Published: May 15, 2026 License: MIT Imports: 14 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ExecPath added in v0.7.0

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

Types

type Client

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

Client wraps a go-git repository and exposes commit operations.

func NewClient

func NewClient(ioStreams *pkg.IO) (*Client, error)

NewClient opens the git repository that contains the current directory. ioStreams configures the streams used for interactive operations; nil uses os.Stdin/Stdout/Stderr.

func NewClientAt added in v0.4.2

func NewClientAt(ioStreams *pkg.IO, dir string) (*Client, error)

NewClientAt opens the git repository rooted at dir. ioStreams configures the streams used for interactive operations; nil uses os.Stdin/Stdout/Stderr.

func (*Client) Authors

func (c *Client) Authors() ([]string, error)

Authors returns a deduplicated, alphabetically sorted list of commit author strings ("Name <email>") from the repository history. The current git config identity is prepended as the first (default) entry.

func (*Client) Checkout added in v0.8.0

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

Checkout switches the working tree to branchName. Wraps `git checkout <name>`. Idempotent: when the working tree is already on branchName, the call is a no-op — the underlying `git checkout` is skipped so heavyweight `post-checkout` hooks don't fire for a same-branch "switch". Returns a wrapped error from the git CLI on failure (e.g. unknown branch, untracked file collision).

func (*Client) Commit

func (c *Client) Commit(ctx context.Context, msg []byte, opts CommitOptions) error

Commit records a commit with msg and the given options using the system git binary so that all configured hooks (pre-commit, commit-msg, post-commit) run.

func (*Client) CreateBranch

func (c *Client) CreateBranch(name, baseBranch string) error

CreateBranch creates a new branch from baseBranch and checks it out.

func (*Client) CreateWorktree added in v0.9.0

func (c *Client) CreateWorktree(ctx context.Context, branchName, baseBranch, path string) error

CreateWorktree creates a new branch from baseBranch and checks it out in a linked worktree at path. Wraps `git worktree add -b <branch> <path> <base>`.

func (*Client) CurrentBranch added in v0.4.2

func (c *Client) CurrentBranch() (string, error)

CurrentBranch returns the short name of the branch HEAD points to. On a detached HEAD the returned name will not parse as an issue branch, so callers can simply ignore it.

func (*Client) DefaultBaseBranch

func (c *Client) DefaultBaseBranch() (string, error)

DefaultBaseBranch resolves the default base branch in priority order:

  1. refs/remotes/<remote>/HEAD (skipped when no remote)
  2. "main" if the local ref exists
  3. "master" if the local ref exists

func (*Client) DeleteLocalBranch added in v0.4.2

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

DeleteLocalBranch deletes the local branch by name. force=true uses -D (required after squash merges); force=false uses -d (safe).

func (*Client) FastForwardOnly added in v0.8.0

func (c *Client) FastForwardOnly(ctx context.Context, sourceBranch, targetBranch string) error

FastForwardOnly checks out targetBranch and runs `git merge --ff-only sourceBranch`. Returns a wrapped error when the FF is refused (diverged history) so the caller can render an actionable message.

func (*Client) Fetch added in v0.9.0

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

Fetch runs `git fetch <remote>`. Returns nil immediately when no remote is configured (local-only repo). Returns a wrapped error when the remote is unreachable or auth fails.

func (*Client) IO added in v0.8.0

func (c *Client) IO() *pkg.IO

IO returns the injected IO streams. Callers should write status/diagnostic messages through these instead of os.Stdout/os.Stderr so Cobra-aware redirection (tests, subcommand piping, future TUI capture) keeps working.

func (*Client) IsAncestor added in v0.8.0

func (c *Client) IsAncestor(ctx context.Context, child, ancestor string) (bool, error)

IsAncestor reports whether child is an ancestor of ancestor (or equal). Wraps `git merge-base --is-ancestor child ancestor`: exit code 0 → true, exit code 1 → false, any other exit code → wrapped error.

func (*Client) IsDirty added in v0.8.0

func (c *Client) IsDirty(ctx context.Context) (bool, error)

IsDirty reports whether the working tree has tracked-file modifications or staged-but-uncommitted changes. Wraps `git status --porcelain --untracked-files=no`. Untracked files are intentionally NOT counted as dirty: `git reset --hard` does not touch untracked content, so their presence does not put user work at risk during rollback.

func (*Client) IsMergedInto

func (c *Client) IsMergedInto(branchName, baseBranch string) (bool, error)

IsMergedInto reports whether branchName's tip commit is reachable from baseBranch, i.e. whether the branch has been merged into base (mirrors git merge-base --is-ancestor).

func (*Client) LocalBranchNames

func (c *Client) LocalBranchNames() ([]string, error)

LocalBranchNames returns the short names of all local branches.

func (*Client) MergeDryRun added in v0.4.2

func (c *Client) MergeDryRun(ctx context.Context, branchName, baseBranch string) ([]string, error)

MergeDryRun checks whether branchName merges cleanly into baseBranch. It uses `git merge-tree --write-tree` (git 2.38+) to perform a 3-way merge in-memory: the working tree is never touched, no hooks run, and submodules are not traversed. Returns the list of conflicting file paths, or nil if clean.

func (*Client) MergeNoFF added in v0.4.2

func (c *Client) MergeNoFF(ctx context.Context, branchName, baseBranch string) error

MergeNoFF runs a classic --no-ff merge of branchName into baseBranch. After the merge the working directory is on baseBranch.

func (*Client) MergeRebase added in v0.8.0

func (c *Client) MergeRebase(ctx context.Context, featureBranch, baseBranch string) error

MergeRebase prepares featureBranch for a single-commit close. When a remote is configured, it merges against remote/<baseBranch> and soft-resets to the same ref. When no remote is available (local-only repo), it uses the local baseBranch directly.

The mechanic is a real `git merge --no-edit <remoteBase>` (submodule-safe — handles gitlinks correctly, unlike `merge --squash`) followed by `git reset --soft <remoteBase>`, leaving HEAD at <remoteBase>, the working tree at the merged state, and the index staged with the consolidated diff. The transient merge commit produced by the merge step is unreachable after the reset and is eventually garbage-collected — `--no-edit` is what prevents git from opening $EDITOR for that throwaway commit message.

Caller is responsible for the final commit (typically via the commitizen TUI form) and for rollback on failure.

func (*Client) MergeSquash added in v0.4.2

func (c *Client) MergeSquash(ctx context.Context, branchName, baseBranch string) error

MergeSquash checks out baseBranch and squash-merges branchName into it, leaving the squashed changes staged. The caller is responsible for the follow-up commit (so it can drive an interactive commit form).

func (*Client) Remote added in v0.9.0

func (c *Client) Remote() (string, error)

Remote returns the resolved remote name, auto-detecting on first call.

Resolution order:

  1. Already pinned via SetRemote → return as-is.
  2. Exactly one remote → cache and return its name.
  3. Zero remotes → return ("", nil); caller treats this as local-only.
  4. Multiple remotes, one named "origin" → use "origin" (git convention).
  5. Multiple remotes, none named "origin" → error with actionable message.

func (*Client) RepoName added in v0.9.0

func (c *Client) RepoName() (string, error)

RepoName returns a short identifier for this repository. Resolution order:

  1. Last path segment of the configured remote URL, with ".git" stripped.
  2. Base name of the working tree root directory (local-only fallback).

func (*Client) ResetHard added in v0.8.0

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

ResetHard runs `git reset --hard <target>`. Used by the close orchestrator to atomically roll the current branch back to its original tip on TUI abort or commit failure. Does not touch untracked files.

func (*Client) ResolveRef added in v0.8.0

func (c *Client) ResolveRef(name string) (plumbing.Hash, error)

ResolveRef returns the commit hash that `name` resolves to (with reference indirection followed). Use it for read-only ref lookups from packages that need a plumbing.Hash without taking a dependency on go-git's plumbing API.

func (*Client) SetRemote added in v0.9.0

func (c *Client) SetRemote(name string)

SetRemote pins the remote name used for all remote operations. Call this when the user has configured branch.remote explicitly.

func (*Client) WorkingTreeRoot

func (c *Client) WorkingTreeRoot() (string, error)

WorkingTreeRoot returns the absolute path of the repository's working tree root.

type CommitOptions

type CommitOptions struct {
	All        bool
	Amend      bool
	NoVerify   bool
	Signoff    bool
	AllowEmpty bool
	Author     string // "Name <email>"; empty = git config identity
}

CommitOptions configures Client.Commit.

Jump to

Keyboard shortcuts

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