git

package
v0.7.0 Latest Latest
Warning

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

Go to latest
Published: Oct 20, 2026 License: Apache-2.0 Imports: 10 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package git creates opt-in commits of an applied change set. It is deliberately minimal and hardened: a commit stages ONLY the caller-supplied paths (never `git add -A`), runs with repository hooks disabled, and executes in a sanitized environment so a git hook, credential helper, or editor can never observe the daemon's privileged environment or provider API keys (§M7).

Index

Constants

This section is empty.

Variables

View Source
var BinPath = "git"

BinPath is the path to the git binary used by Commit. It is a package-level variable so tests can substitute a fake git (mirroring the executor's dockerPath override). Defaults to "git", resolved via PATH.

View Source
var ErrGHNotFound = errors.New("git: the GitHub CLI (`gh`) is not installed or not on PATH")

ErrGHNotFound is returned by CreatePR and PRChecks when the GitHub CLI cannot be located, so a caller can give a clear, specific message rather than an opaque "exec: not found".

View Source
var ErrNoRemote = errors.New("git: this repository has no remote configured; add one (e.g. `git remote add origin <url>`) and push the branch, then run `idea pr` again")

ErrNoRemote is returned by CreatePR when the repository has no git remote configured at all (ICP re-run gap: `idea pr` on such a repository dumped a raw git error -- "'origin' does not appear to be a git repository" -- with no indication of what was actually wrong or what to do about it). pushRemote's own "origin" fallback names a remote that must actually exist for that raw error to be avoidable; this is checked before ever shelling out to `git push`, rather than pattern-matching git's own (unversioned, locale-dependent) stderr text after the fact.

View Source
var GHBinPath = "gh"

GHBinPath is the path to the GitHub CLI binary CreatePR and PRChecks invoke. A package-level variable so tests can substitute a fake, mirroring BinPath for git itself. Defaults to "gh", resolved via PATH.

Functions

func CheckoutBranch added in v0.6.0

func CheckoutBranch(ctx context.Context, workTree, branch string) error

CheckoutBranch switches workTree onto branch. A no-op (nil error) when branch is "" (CurrentBranch could not determine what to restore to, most often a detached HEAD -- nothing to switch back to) or workTree is not a repository. Used to restore the branch CurrentBranch captured before a later failure; callers restoring on an error path should treat this call's own failure as best-effort and never let it mask the original error.

func Commit

func Commit(ctx context.Context, workTree string, paths []string, message string) (string, error)

Commit stages the given workspace-relative paths (adds, modifications, and deletions) and creates one commit whose subject is message. It returns the new commit's SHA.

Behavior:

  • If workTree is not a git repository (workTree/.git missing), it returns ("", nil) — a non-repo is a silent no-op, not an error.
  • If there is nothing to commit (the staged diff is empty), it returns ("", nil).
  • Only the given paths are staged; the commit never uses `git add -A`.
  • Repository hooks are disabled (core.hooksPath points at an empty temp dir), so `git commit` cannot execute arbitrary hook code under the daemon's environment.
  • The git process runs with a sanitized environment (PATH/HOME/LANG plus GIT_TERMINAL_PROMPT=0), so inherited GIT_* variables and provider API keys are dropped.

Any real git failure (missing binary, identity not configured, lock held, etc.) is returned as an error; the caller treats it as non-fatal.

func CreatePR added in v0.6.0

func CreatePR(ctx context.Context, workTree string, req PRRequest) (string, error)

CreatePR pushes req.Branch to its configured upstream remote (or `origin` when none is configured -- review M7) with `-u`, so it is safe to call again against an already-pushed branch, and runs `gh pr create`, returning the new pull request's URL. `gh` prints exactly the URL on success; a caller with an existing PR for this branch should not call CreatePR again -- Engine.CreatePR is the idempotent entry point that checks domain.Initiative.PRURL first.

func CurrentBranch added in v0.6.0

func CurrentBranch(ctx context.Context, workTree string) string

CurrentBranch returns the short name of the branch workTree currently has checked out, or "" when workTree is not a repository, HEAD is detached, or the branch otherwise cannot be determined. Best-effort and never an error a caller must handle: it exists purely so a caller that is about to switch branches (EnsureInitiativeBranch) can restore this one later if something it does after the switch fails (review MEDIUM #4: a catch-up-commit or change-set-commit failure after `checkout -b` used to leave the workspace stranded on the initiative's branch instead of back where it started).

func DefaultBranchGuardReason added in v0.6.0

func DefaultBranchGuardReason(ctx context.Context, workTree string, allowDefaultBranch bool) (string, error)

DefaultBranchGuardReason reports whether workTree's CURRENT branch (after any branch-per-initiative switch has already happened) is the repository's default branch, unless allowDefaultBranch overrides it (the `git.allow_default_branch` config toggle). This is deliberately the only check still run AFTER a checkout (review H2): an agent commit landing directly on the default branch, with no human commit of their own in between, is a stronger claim than opting into git_commit alone should make, and that judgment has to be made against whatever branch is checked out by the time a commit is about to happen -- the initiative's own branch, not whatever HEAD was before branch-per-initiative switched it.

func EnsureInitiativeBranch added in v0.6.0

func EnsureInitiativeBranch(ctx context.Context, workTree, existingBranch, titleSeed string, keepPaths []string, catchUpFiles []CatchUpFile) (branch, reason, warning string, err error)

EnsureInitiativeBranch switches workTree onto an initiative's dedicated branch for git.branch_per_initiative (review C4, adopted #2), creating it from the current HEAD the first time.

  • existingBranch, when non-empty, is the branch a prior apply of this same initiative already created (domain.Initiative.GitBranch); it is checked out directly, so every change set for one initiative lands on the same branch and the name is computed only once.
  • Otherwise a fresh branch named "idea/<slug of titleSeed>" is created (suffixed -2, -3, … on a name collision) -- but only if the tree has no uncommitted changes outside keepPaths (the change set about to be committed) and catchUpPaths (see below): those would otherwise ride along onto the new branch uninvited, which is exactly the failure this guards against.
  • catchUpFiles names files this same initiative's OWN earlier applied change sets touched but never committed (review defect D: a real run had git_commit off for its first several applies, then turned it and branch_per_initiative on together -- the workspace was left with files from those earlier applies still uncommitted, and every later attempt refused forever with "uncommitted changes outside this change set" naming the harness's OWN prior writes). A dirty path that matches one of these, AND whose on-disk content still matches the digest that change set actually wrote there, is committed onto the initiative's branch (right after the branch switch below, so the catch-up commit lands there and not on whatever branch was checked out before) as its own commit, separate from the caller's own change-set commit. A dirty path that matches neither keepPaths nor catchUpFiles, OR that matches one but no longer has the content that change set wrote (review MEDIUM #4: catching it up unconditionally would sweep a user's own hand-edit into a commit titled as the harness's own catch-up), still refuses, naming only the paths that are genuinely unexplained or stale.

Returns (branch, "", "", nil) once workTree is checked out on branch. A soft refusal -- the tree has a dirty path outside keepPaths and catchUpPaths -- is ("", reason, "", nil): workTree is left exactly as found, and the caller should skip the commit rather than proceed on whatever branch was already checked out. A non-empty warning (with branch still set, and no error) means the branch WAS created and checked out, but not from the resolved default branch's commit -- see the HEAD-vs-default comment below (review H6); the caller should still commit, but surface the warning (e.g. as the change set's git_commit_warning). A non-nil error is a genuine git failure.

func PreCommitGuardReason added in v0.6.0

func PreCommitGuardReason(ctx context.Context, workTree string) (string, error)

PreCommitGuardReason reports why the opt-in commit-on-apply path must not touch workTree at all right now -- including the branch switch/creation that may happen before the commit itself -- or "" when it may proceed (review C4, adopted change #2; review H2 split this out of the combined CommitGuardReason so a caller can run it BEFORE any checkout).

A refusal here must never half-apply a change set: the caller still writes the files and marks the change set applied, and only skips the commit (and any branch switch), reporting this reason as a warning on the change set instead of failing the apply.

The guard checks, in order:

  • a merge or cherry-pick in progress (a checkout/commit here would either finish someone else's merge with the agent's message, abandon it, or fail outright because git refuses to switch branches mid-conflict);
  • a rebase in progress (same hazard, different mechanism);
  • a detached HEAD (committing here is an orphan the next checkout loses, and -- since this now runs before any checkout -- a branch-per-initiative switch is refused too rather than silently leaving the detached state for the checkout to (maybe) fix).

Not a repository at all is not refused here -- Commit's own isRepo check already makes that a silent no-op, and this function has nothing to add.

func Slugify added in v0.6.0

func Slugify(s string, maxLen int) string

Slugify lowercases s, collapses every run of characters outside [a-z0-9] into a single '-', trims a leading or trailing '-', and truncates to maxLen without leaving a trailing '-'. It is deterministic and pure ASCII, so the result is safe as a git ref component and portable across every git host. An input with nothing left after collapsing (all punctuation, all non-ASCII, empty) falls back to "initiative" rather than the empty string.

Types

type CatchUpFile added in v0.6.0

type CatchUpFile struct {
	Path   string
	Digest string
}

CatchUpFile names one path from an initiative's own earlier applied, uncommitted change sets (review defect D), and the content that change set is recorded as having actually produced there -- so EnsureInitiativeBranch can tell the harness's own uncommitted write apart from a user's hand-edit made to the same path afterward (review MEDIUM #4).

Digest is the post-apply content digest (blob.Digest of the file's bytes) the change set wrote, or "" when that change set DELETED the path -- the expected on-disk state is then "absent", not merely a digest mismatch.

type CheckStatus added in v0.6.0

type CheckStatus struct {
	Name  string `json:"name"`
	State string `json:"state"` // e.g. SUCCESS, FAILURE, PENDING, SKIPPED
	Link  string `json:"link,omitempty"`
}

CheckStatus is one CI check's state, from `gh pr checks --json`.

type PRChecksResult added in v0.6.0

type PRChecksResult struct {
	// Overall is "success" (every check succeeded), "failure" (at least one
	// failed), "pending" (none failed, at least one still running), or "none"
	// (no checks reported at all -- a brand-new PR, or a repository with no
	// CI configured).
	Overall string        `json:"overall"`
	Checks  []CheckStatus `json:"checks"`
}

PRChecksResult summarizes `gh pr checks` for one pull request.

func PRChecks added in v0.6.0

func PRChecks(ctx context.Context, workTree, ref string) (PRChecksResult, error)

PRChecks reports the latest CI status for ref (a branch name or PR number) via `gh pr checks --json`. `gh` exits non-zero when any check failed or is still pending -- that is itself the answer, not a call failure, so only an unparsable result (or gh/PR genuinely missing) is reported as an error.

type PRRequest added in v0.6.0

type PRRequest struct {
	// Branch is the head branch, already checked out (git.branch_per_initiative
	// creates it) and pushed by CreatePR itself.
	Branch string
	// Base is the target branch; empty lets `gh` fall back to the
	// repository's default branch.
	Base  string
	Title string
	Body  string
}

PRRequest is what CreatePR needs to open a pull request.

Jump to

Keyboard shortcuts

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