git

package
v0.36.1 Latest Latest
Warning

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

Go to latest
Published: Sep 14, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func CommitAllChanges

func CommitAllChanges(worktreeDir, commitMsg string) error

CommitAllChanges stages all changes in worktreeDir and commits them. If there is nothing to commit it is a no-op. The commit uses a local identity override so it succeeds even when no global git config exists.

func CommitStaged added in v0.26.0

func CommitStaged(dir, msg string) error

CommitStaged commits the currently staged changes with the given message. It uses a local identity override so it works in repos without user.name/ user.email configured. Returns an error when nothing is staged.

func CreateChainBranch added in v0.2.2

func CreateChainBranch(repoDir, chainBranch string) error

CreateChainBranch creates the shared branch for a dependency chain, branching from the current HEAD. It is a no-op when the branch already exists.

func DeleteBranch

func DeleteBranch(repoDir, branchName string) error

DeleteBranch deletes a local branch. Returns nil if the branch does not exist.

func DiffFile added in v0.26.0

func DiffFile(dir, path string, staged bool) (string, error)

DiffFile returns the raw unified diff for path. When staged is true it diffs the index (cached) instead of the working tree. Returns "" when the file has no changes at the requested level. Uses --no-color.

Untracked files (git status "??") are diffed with `git diff --no-index /dev/null -- path` so that a brand-new file renders as an all-addition hunk instead of the empty string plain `git diff` returns for untracked paths. Staged untracked files are handled by the normal --cached path.

func EnsureBranchOnRemote added in v0.12.0

func EnsureBranchOnRemote(ctx context.Context, repoDir, branchName string) error

EnsureBranchOnRemote pushes branchName to origin if it is not already present there, so it can be used as a PR base. It is a no-op when branchName is empty or already on the remote. Returns an error only when the push itself fails.

func EnsureLocalBranch

func EnsureLocalBranch(repoDir, branchName string) error

EnsureLocalBranch makes sure branchName exists as a local git ref. If the branch already exists locally it is a no-op; otherwise it attempts to fetch it from origin. Returns an error only when neither works.

func GetCurrentBranch

func GetCurrentBranch(dir string) string

GetCurrentBranch returns the current git branch name for the given directory.

func HasStaged added in v0.26.0

func HasStaged(dir string) bool

HasStaged reports whether the index has any staged changes.

func IsRepo added in v0.26.0

func IsRepo(dir string) bool

IsRepo reports whether dir is inside a git work tree.

func MergeTaskBranch added in v0.2.2

func MergeTaskBranch(repoDir, chainBranch, taskBranch, taskTitle string) error

MergeTaskBranch merges a completed task branch into the shared chain branch. A temporary worktree is created on the chain branch to perform the merge, then removed. The chain branch itself is kept so subsequent tasks and the final chain PR can use it.

func PushBranch

func PushBranch(ctx context.Context, repoDir, branchName string) (bool, error)

PushBranch pushes branchName to origin using ctx for timeout/cancellation. Returns false (with nil error) when no remote is configured.

func RemoveTaskWorktree

func RemoveTaskWorktree(repoDir string, branchName string) error

RemoveTaskWorktree removes a git worktree and its local branch.

func RemoveTaskWorktreeKeepBranch

func RemoveTaskWorktreeKeepBranch(repoDir string, branchName string) error

RemoveTaskWorktreeKeepBranch removes the worktree directory but keeps the branch intact. Use this when there is no remote to push to, so the work remains accessible via the branch ref.

func ShowCommit added in v0.26.0

func ShowCommit(dir, sha string) (string, error)

ShowCommit returns the full unified diff for the given commit (raw text from `git show --no-color`).

func Slugify

func Slugify(title string) string

Slugify converts a task title into a URL-safe slug.

func Stage added in v0.26.0

func Stage(dir string, paths []string) error

Stage adds the given paths to the index (git add -- <paths...>). It uses explicit paths only — never -A — so the caller controls scope. Empty paths is a no-op.

func Unstage added in v0.26.0

func Unstage(dir string, paths []string) error

Unstage removes the given paths from the index (git reset HEAD -- <paths...>). Empty paths is a no-op.

Types

type Commit added in v0.26.0

type Commit struct {
	SHA     string `json:"sha"`
	Short   string `json:"short"`
	Message string `json:"message"` // subject line only
	Author  string `json:"author"`
	Time    string `json:"time"` // ISO 8601
}

Commit is one entry from `git log --format=...`.

func RecentCommits added in v0.26.0

func RecentCommits(dir string, n int) ([]Commit, error)

RecentCommits returns the last n commits (default 20 when n <= 0) as Commit entries. Returns nil, nil when dir is not a git repository.

type FileStatus added in v0.26.0

type FileStatus struct {
	Path   string `json:"path"`   // workspace-relative
	X      string `json:"x"`      // index/staged state code, e.g. "M", "A", " ", "?"
	Y      string `json:"y"`      // worktree state code
	Staged bool   `json:"staged"` // true when X is non-blank and not "?"
}

FileStatus is one entry from `git status --porcelain`.

func Status added in v0.26.0

func Status(dir string) ([]FileStatus, error)

Status returns the working-tree status of dir as a list of FileStatus entries (one per changed file). Returns nil, nil when the tree is clean. Returns nil, nil (not an error) when dir is not a git repository.

type PullRequest

type PullRequest struct {
	URL    string
	Number int
}

PullRequest holds the result of creating a pull request.

func CreatePR

func CreatePR(ctx context.Context, repoDir, branchName, title, body, baseBranch string) (*PullRequest, error)

CreatePR creates a pull request via the gh CLI using ctx for timeout/cancellation. It is idempotent: if an open PR already exists for branchName it is returned as-is. If baseBranch no longer exists on the remote (e.g. stacked PR was merged and branch deleted), it falls back to the repo's default branch automatically.

type SyncStatus added in v0.13.0

type SyncStatus struct {
	IsRepo      bool   `json:"isRepo"`      // false when dir is not a git repo
	Branch      string `json:"branch"`      // current branch ("" / "HEAD" when detached)
	HasUpstream bool   `json:"hasUpstream"` // whether the branch tracks a remote branch
	Upstream    string `json:"upstream"`    // e.g. "origin/main"
	Ahead       int    `json:"ahead"`       // local commits not on upstream
	Behind      int    `json:"behind"`      // upstream commits not in local
	Fetched     bool   `json:"fetched"`     // whether the best-effort fetch succeeded
	FetchError  string `json:"fetchError,omitempty"`
}

SyncStatus describes how the current branch relates to its upstream.

func BranchSyncStatus added in v0.13.0

func BranchSyncStatus(ctx context.Context, repoDir string) (*SyncStatus, error)

BranchSyncStatus reports whether the current branch is in sync with its upstream. It first does a best-effort, time-bounded `git fetch` of the branch's remote so the ahead/behind counts reflect the real remote state; if the fetch fails or times out (offline, auth, no remote) it falls back to the last-known remote-tracking ref and records the reason in FetchError.

type TaskWorktree

type TaskWorktree struct {
	BranchName string
	Path       string // absolute path to the worktree directory
}

TaskWorktree holds the result of creating a worktree for a task.

func CreateTaskWorktree

func CreateTaskWorktree(repoDir string, taskID string, slug string, baseBranch string) (*TaskWorktree, error)

CreateTaskWorktree creates a git worktree for a task. Each worktree is an independent checkout on its own branch, so multiple agents can work in parallel without interfering with each other. The worktree lives under <repo>/.ogcode/worktrees/<branchName>. If baseBranch is non-empty the new branch is created from that branch instead of HEAD, enabling stacked PRs for dependent tasks.

Jump to

Keyboard shortcuts

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