{"modulePath":"github.com/chainloop-dev/chainloop","version":"v1.138.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package git\n\n## Constants\n\n```go\nconst (\n\tStatusCreated  = \"created\"\n\tStatusModified = \"modified\"\n\tStatusDeleted  = \"deleted\"\n\tStatusRenamed  = \"renamed\"\n)\n```\nFile statuses reported in aicodingsession.FileChange.Status. The control plane buckets on these exact strings when it ingests the evidence, so they are a wire contract, not free-form labels.\n\n## Variables\n\n```go\nvar ErrNotARepository = errors.New(\"not a git repository (or any parent up to root)\")\n```\nErrNotARepository reports that no .git was found from cwd up to root. It is the only error callers should read as \"fall back to non-git mode\": every other failure (unreadable cwd, malformed .git file) means a repository is there but broken, and must be surfaced.\n\n```go\nvar ErrReflogRewritten = errors.New(\"HEAD reflog was rewritten\")\n```\nErrReflogRewritten reports that the HEAD reflog no longer holds a position read from it earlier: something expired or rewrote it in between, so the position no longer marks where the new entries start.\n\n```go\nvar ErrUnbatchableName = errors.New(\"name holds a newline\")\n```\nErrUnbatchableName reports a name that a BlobReader cannot ask git for, because it holds a newline: read it in another way.\n\n## Functions\n\n```go\nfunc CommitPaths(repoRoot, from, to string) (map[string]bool, error)\n```\nCommitPaths returns the repo-relative paths, with forward slashes, that differ between the commits from and to. A zero from is the first commit of a branch, and every path of to then counts.\n\n```go\nfunc FindGitDir() (string, error)\n```\nFindGitDir returns the path to the .git directory for the current repository.\n\n```go\nfunc FindGitDirAndRoot() (gitDir, repoRoot string, err error)\n```\nFindGitDirAndRoot returns both the .git directory and repo root.\n\n```go\nfunc FindGitDirAndRootFrom(dir string) (gitDir, repoRoot string, err error)\n```\nFindGitDirAndRootFrom is FindGitDirAndRoot starting from dir instead of cwd. dir need not exist yet: the walk only probes for .git on the way up.\n\n```go\nfunc IsMergeInProgress(gitDir string) bool\n```\nIsMergeInProgress reports whether a merge is in progress in the given .git directory. Detected via the presence of the MERGE\\_HEAD file, which git creates when a \\`git merge\\` cannot fast-forward and stops to let the user produce the merge commit.\n\n```go\nfunc IsZeroOID(oid string) bool\n```\nIsZeroOID reports whether oid is the all-zero object ID that a reflog entry holds for \"no commit\", as the old side of the first commit.\n\n```go\nfunc RepoRoot() (string, error)\n```\nRepoRoot returns the top-level directory of the current git repository.\n\n## Types\n\n```go\ntype BlameLine struct {\n\tSHA  string\n\tText string\n}\n```\nBlameLine is one line of a file, with the commit that last wrote it.\n\n```go\ntype BlobReader struct {\n\t// contains filtered or unexported fields\n}\n```\nBlobReader reads files at commits through one long-running \"git cat-file --batch\" process, so that many reads cost one process, not several processes each. It is not safe for concurrent use.\n\n```go\nfunc NewBlobReader(repoRoot string) (*BlobReader, error)\n```\nNewBlobReader starts a BlobReader for the repository at repoRoot. Close it when done.\n\n```go\nfunc (r *BlobReader) Close() error\n```\nClose stops the git process.\n\n```go\nfunc (r *BlobReader) FileAt(rev, path string) ([]byte, error)\n```\nFileAt returns the content of path, repo-relative with forward slashes, at rev. It returns nil when rev does not resolve or does not hold path as a file, and ErrUnbatchableName when rev or path holds a newline.\n\n```go\ntype BranchCommit struct {\n\tSHA         string\n\tMessage     string\n\tCommittedAt time.Time\n\t// Parents is the number of parents. A merge commit has more than one.\n\tParents int\n}\n```\nClient abstracts git operations so the implementation can be swapped (e.g. from exec-based to go-git). BranchCommit is a commit of the current branch.\n\n```go\ntype Client interface {\n\t// Context collects repository metadata: remote URL, branch, commits since base.\n\tContext(repoRoot string) (*aicodingsession.GitContext, error)\n\t// CodeChanges collects file-level diff stats between the merge base and HEAD.\n\tCodeChanges(repoRoot string) (*aicodingsession.CodeChanges, error)\n\t// CodeChangesForRange collects file-level diff stats for a specific commit range.\n\t// The range covers startSHA^..endSHA (i.e. changes introduced by the commits).\n\tCodeChangesForRange(repoRoot, startSHA, endSHA string) (*aicodingsession.CodeChanges, error)\n\t// CodeChangesForCommits collects file-level diff stats for the union of the\n\t// provided commit SHAs. Each commit's parent..commit diff is computed\n\t// individually and summed by file path, so a non-contiguous set (e.g. one\n\t// session's commits interleaved with another's on the same branch) is\n\t// handled correctly. The order in which commits are visited is unspecified;\n\t// per-file Status reflects the most recently visited commit's view.\n\tCodeChangesForCommits(repoRoot string, shas []string) (*aicodingsession.CodeChanges, error)\n\t// CommitLines returns, for each file that the commit changed against its\n\t// first parent, the content of the lines it added and removed. Binary\n\t// files are left out. A renamed file shows the lines that changed, under\n\t// its new path, with its old path.\n\tCommitLines(repoRoot, sha string) ([]FileLines, error)\n\t// Renames returns, for each file that was renamed between two revisions,\n\t// its new path by its old path.\n\tRenames(repoRoot, from, to string) (map[string]string, error)\n\t// FileAt returns the content of a repo-relative path at a commit, or nil\n\t// when the commit does not hold the path.\n\tFileAt(repoRoot, sha, path string) ([]byte, error)\n\t// FilesAt returns base content for each path, or nil for paths absent at sha.\n\tFilesAt(repoRoot, sha string, paths []string) (map[string][]byte, error)\n\t// Blame returns each line of a repo-relative path at a revision, with the\n\t// commit that last wrote it, or nil when the revision does not hold the\n\t// path.\n\tBlame(repoRoot, rev, path string) ([]BlameLine, error)\n\t// CommitHeadInfo returns the SHA and message of HEAD.\n\tCommitHeadInfo(repoRoot string) (sha, message string, err error)\n\t// HeadAuthorTime returns the author date of HEAD.\n\tHeadAuthorTime(repoRoot string) (time.Time, error)\n\t// GeneratedMatcher returns a predicate that reports whether a repo-relative\n\t// path is marked linguist-generated=true in the repository's .gitattributes.\n\t// Implementations must return a no-op matcher (always false) when attributes\n\t// can't be read — filtering is best-effort and must not block attribution.\n\tGeneratedMatcher(repoRoot string) func(path string) bool\n\t// StagedFiles returns repo-relative paths of files staged for the current commit.\n\tStagedFiles(repoRoot string) ([]string, error)\n\t// SnapshotWorktree returns a signature of the working tree as a map of\n\t// repo-relative path (forward slashes) → content hash, skipping the .git\n\t// directory and any .gitignore'd paths. Used to detect files changed by an\n\t// agent-run shell command by diffing a before/after signature.\n\tSnapshotWorktree(repoRoot string) (map[string]string, error)\n\t// SnapshotIndex returns staged regular-file paths and their blob hashes.\n\tSnapshotIndex(repoRoot string) (map[string]string, error)\n\t// IndexFile returns the staged content for a repo-relative path.\n\tIndexFile(repoRoot, path string) ([]byte, error)\n\t// IndexBlobs returns the content of the supplied snapshot hashes by path.\n\tIndexBlobs(repoRoot string, hashes map[string]string) (map[string][]byte, error)\n\t// IsMergeCommit reports whether the commit identified by sha has more than\n\t// one parent. Returns an error when the commit cannot be resolved.\n\tIsMergeCommit(repoRoot, sha string) (bool, error)\n\t// BranchCommitSHAs returns the SHAs reachable from HEAD that are not\n\t// reachable from the default remote branch (HEAD ^merge-base). The merge\n\t// base itself is excluded. HEAD comes first, and on a linear branch each\n\t// commit comes before its parents.\n\tBranchCommitSHAs(repoRoot string) ([]string, error)\n\t// BranchCommits returns the commits of BranchCommitSHAs, in the same\n\t// order, whose message contains grep. An empty grep returns every commit.\n\t// It reads all of them in one pass.\n\tBranchCommits(repoRoot, grep string) ([]BranchCommit, error)\n\t// LocalReachableSHAs returns the union of SHAs reachable from every\n\t// local branch tip (refs/heads/*). Used by the post-push GC to identify\n\t// CommitRecords that are still alive on at least one branch — anything\n\t// missing is an orphan from a deleted branch or a pre-rebase rewrite and\n\t// is safe to drop.\n\tLocalReachableSHAs(repoRoot string) (map[string]bool, error)\n}\n```\n\n```go\nfunc NewGitClient() Client\n```\nNewGitClient returns the client that runs the git binary when one is on the PATH, and the go-git client otherwise. The git binary is many times faster on a long history, for example for blame.\n\n```go\ntype ExecClient struct{}\n```\nExecClient implements Client by shelling out to the git binary.\n\n```go\nfunc NewExecClient() *ExecClient\n```\n\n```go\nfunc (c *ExecClient) Blame(repoRoot, rev, path string) ([]BlameLine, error)\n```\nBlame returns the last writer of each line of path at rev.\n\n```go\nfunc (c *ExecClient) BranchCommitSHAs(repoRoot string) ([]string, error)\n```\nBranchCommitSHAs returns the SHAs reachable from HEAD that are not reachable from the default remote branch. The merge base is excluded. Order matches \\`git rev-list HEAD ^\\\u003cbase\u003e\\` (newest first). When no default remote branch is detected, returns every commit reachable from HEAD.\n\n```go\nfunc (c *ExecClient) BranchCommits(repoRoot, grep string) ([]BranchCommit, error)\n```\nBranchCommits returns the branch commits whose message contains grep, with one git log call.\n\n```go\nfunc (c *ExecClient) CodeChanges(repoRoot string) (*aicodingsession.CodeChanges, error)\n```\nCodeChanges collects file-level diff stats between the merge base and HEAD.\n\n```go\nfunc (c *ExecClient) CodeChangesForCommits(repoRoot string, shas []string) (*aicodingsession.CodeChanges, error)\n```\nCodeChangesForCommits aggregates per-commit (parent..commit) diffs across a possibly non-contiguous set of SHAs. See the GoGitClient counterpart for the rationale; this implementation walks each SHA via parseDiffStats and sums.\n\n```go\nfunc (c *ExecClient) CodeChangesForRange(repoRoot, startSHA, endSHA string) (*aicodingsession.CodeChanges, error)\n```\nCodeChangesForRange collects file-level diff stats for a specific commit range. The range covers startSHA^..endSHA (changes introduced by the commits).\n\n```go\nfunc (c *ExecClient) CommitHeadInfo(repoRoot string) (sha, message string, err error)\n```\nCommitHeadInfo returns the SHA and message of HEAD.\n\n```go\nfunc (c *ExecClient) CommitLines(repoRoot, sha string) ([]FileLines, error)\n```\nCommitLines parses \\`git diff -U0\\` of the commit against its first parent.\n\n```go\nfunc (c *ExecClient) Context(repoRoot string) (*aicodingsession.GitContext, error)\n```\nContext collects repository metadata: remote URL, branch, commits since base.\n\n```go\nfunc (c *ExecClient) FileAt(repoRoot, sha, path string) ([]byte, error)\n```\nFileAt returns the content of path at the commit, or nil when the commit does not hold it.\n\n```go\nfunc (c *ExecClient) FilesAt(repoRoot, sha string, paths []string) (map[string][]byte, error)\n```\nFilesAt reads base blobs in one Git process. Newline paths use FileAt because cat-file --batch separates queries by newline.\n\n```go\nfunc (c *ExecClient) GeneratedMatcher(repoRoot string) func(path string) bool\n```\nGeneratedMatcher returns a predicate that reports whether a repo-relative path is marked linguist-generated=true, by shelling out to \\`git check-attr\\` per call. \\`git check-attr\\` returns \"\" on error (e.g. non-git directory), which degrades to a no-op matcher.\n\n```go\nfunc (c *ExecClient) HeadAuthorTime(repoRoot string) (time.Time, error)\n```\nHeadAuthorTime returns the author date of HEAD.\n\n```go\nfunc (c *ExecClient) IndexBlobs(repoRoot string, hashes map[string]string) (map[string][]byte, error)\n```\nIndexBlobs reads the supplied staged blob hashes in one git process.\n\n```go\nfunc (c *ExecClient) IndexFile(repoRoot, path string) ([]byte, error)\n```\nIndexFile reads the staged blob selected by Git's effective index.\n\n```go\nfunc (c *ExecClient) IsMergeCommit(repoRoot, sha string) (bool, error)\n```\nIsMergeCommit reports whether the commit at sha has more than one parent.\n\n```go\nfunc (c *ExecClient) LocalReachableSHAs(repoRoot string) (map[string]bool, error)\n```\nLocalReachableSHAs returns the union of SHAs reachable from every local branch tip. Implemented as a single \\`git rev-list\\` invocation across all refs under refs/heads/.\n\n```go\nfunc (c *ExecClient) Renames(repoRoot, from, to string) (map[string]string, error)\n```\nRenames reads \\`git diff --name-status\\` between two revisions, with rename detection on, whatever the configuration of the user says.\n\n```go\nfunc (c *ExecClient) SnapshotIndex(repoRoot string) (map[string]string, error)\n```\nSnapshotIndex reads the effective index with Git, including split and sparse indexes and temporary indexes selected by GIT\\_INDEX\\_FILE.\n\n```go\nfunc (c *ExecClient) SnapshotWorktree(repoRoot string) (map[string]string, error)\n```\nSnapshotWorktree returns a signature of the working tree (repo-relative path → content hash). The walk is git-binary-independent, so it shares the implementation used by GoGitClient.\n\n```go\nfunc (c *ExecClient) StagedFiles(repoRoot string) ([]string, error)\n```\nStagedFiles returns repo-relative paths of files staged for the current commit.\n\n```go\ntype FileLines struct {\n\tPath string\n\t// OldPath is the path of the file in the parent, set when the commit\n\t// renamed the file.\n\tOldPath string\n\tAdded   []string\n\tRemoved []string\n\t// Hunks holds the same lines, split into the hunks of the diff.\n\tHunks []Hunk\n}\n```\nFileLines holds the lines that one commit added to and removed from a file, without their line ends.\n\n```go\ntype GoGitClient struct{}\n```\nGoGitClient implements Client using go-git (no external git binary required).\n\n```go\nfunc NewGoGitClient() *GoGitClient\n```\n\n```go\nfunc (c *GoGitClient) Blame(repoRoot, rev, path string) ([]BlameLine, error)\n```\nBlame returns the last writer of each line of path at rev. On a long history it is many times slower than the git binary, which is why NewGitClient prefers the exec client.\n\n```go\nfunc (c *GoGitClient) BranchCommitSHAs(repoRoot string) ([]string, error)\n```\nBranchCommitSHAs returns the SHAs reachable from HEAD that are not reachable from the default remote branch. The merge base is excluded. HEAD comes first, and on a linear branch each commit comes before its parents.\n\n```go\nfunc (c *GoGitClient) BranchCommits(repoRoot, grep string) ([]BranchCommit, error)\n```\nBranchCommits returns the branch commits whose message contains grep.\n\n```go\nfunc (c *GoGitClient) CodeChanges(repoRoot string) (*aicodingsession.CodeChanges, error)\n```\n\n```go\nfunc (c *GoGitClient) CodeChangesForCommits(repoRoot string, shas []string) (*aicodingsession.CodeChanges, error)\n```\nCodeChangesForCommits computes the union of per-commit (parent..commit) diffs for the given SHAs, summing line counts per file path. This is what runTracePush needs when a session's commits are non-contiguous on the branch — a SHA range diff would over-count by including unrelated commits in the gap. Unresolvable SHAs are skipped (best-effort) rather than failing the whole computation; that lets attestation continue when a stale CommitRecord references a SHA that's been pruned by git.\n\n```go\nfunc (c *GoGitClient) CodeChangesForRange(repoRoot, startSHA, endSHA string) (*aicodingsession.CodeChanges, error)\n```\n\n```go\nfunc (c *GoGitClient) CommitHeadInfo(repoRoot string) (sha, message string, err error)\n```\n\n```go\nfunc (c *GoGitClient) CommitLines(repoRoot, sha string) ([]FileLines, error)\n```\nCommitLines returns the lines that the commit added and removed, per file, against its first parent.\n\n```go\nfunc (c *GoGitClient) Context(repoRoot string) (*aicodingsession.GitContext, error)\n```\n\n```go\nfunc (c *GoGitClient) FileAt(repoRoot, sha, path string) ([]byte, error)\n```\nFileAt returns the content of path at the commit, or nil when the commit does not hold it. sha can be any revision, such as HEAD.\n\n```go\nfunc (c *GoGitClient) FilesAt(repoRoot, sha string, paths []string) (map[string][]byte, error)\n```\nFilesAt reads base content for multiple paths, keeping absent paths nil.\n\n```go\nfunc (c *GoGitClient) GeneratedMatcher(repoRoot string) func(path string) bool\n```\nGeneratedMatcher returns a predicate that reports whether a repo-relative path is marked linguist-generated=true in the repository's .gitattributes, parsed in-process via go-git's gitattributes parser.\n\n```go\nfunc (c *GoGitClient) HeadAuthorTime(repoRoot string) (time.Time, error)\n```\n\n```go\nfunc (c *GoGitClient) IndexBlobs(repoRoot string, hashes map[string]string) (map[string][]byte, error)\n```\nIndexBlobs reads the supplied snapshot hashes without scanning the index again.\n\n```go\nfunc (c *GoGitClient) IndexFile(repoRoot, path string) ([]byte, error)\n```\nIndexFile returns the exact staged content for path.\n\n```go\nfunc (c *GoGitClient) IsMergeCommit(repoRoot, sha string) (bool, error)\n```\nIsMergeCommit reports whether the commit at sha has more than one parent.\n\n```go\nfunc (c *GoGitClient) LocalReachableSHAs(repoRoot string) (map[string]bool, error)\n```\nLocalReachableSHAs walks every local branch tip (refs/heads/\\*) and returns the union of reachable SHAs. The shared \\`seen\\` map both deduplicates work across branch walks (preorder iter skips already-seen commits and stops descending into their parents) and records every visited commit so the caller can build the live-SHA set.\n\n```go\nfunc (c *GoGitClient) Renames(repoRoot, from, to string) (map[string]string, error)\n```\nRenames returns the new path of each file renamed between two revisions, by its old path.\n\n```go\nfunc (c *GoGitClient) SnapshotIndex(repoRoot string) (map[string]string, error)\n```\nSnapshotIndex returns the content signature currently staged in git's index. It excludes symlinks and submodules to match SnapshotWorktree's file scope.\n\n```go\nfunc (c *GoGitClient) SnapshotWorktree(repoRoot string) (map[string]string, error)\n```\nSnapshotWorktree returns a signature of the working tree (repo-relative path → SHA-256 content hash), honoring .gitignore and skipping the .git directory.\n\n```go\nfunc (c *GoGitClient) StagedFiles(repoRoot string) ([]string, error)\n```\nStagedFiles returns repo-relative paths of files staged for the current commit.\n\n```go\ntype Hunk struct {\n\t// OldStart is the line of the parent's file where the hunk starts,\n\t// counting from one: the first removed line, or the line that the added\n\t// lines go before. Removed line i is line OldStart+i of the parent.\n\tOldStart int\n\tRemoved  []string\n\tAdded    []string\n}\n```\nHunk is one run of removed and added lines of a diff.\n\n```go\ntype ReflogEntry struct {\n\t// Old and New are the commits HEAD pointed to before and after the move.\n\tOld, New string\n\t// Message says what moved HEAD, for example \"commit: \u003csubject\u003e\" or\n\t// \"rebase (finish): returning to refs/heads/main\".\n\tMessage string\n}\n```\nReflogEntry is one move of HEAD, as the reflog records it.\n\n```go\nfunc HeadReflogSince(repoRoot string, pos ReflogPos) ([]ReflogEntry, error)\n```\nHeadReflogSince returns the HEAD reflog entries written after pos, a position that HeadReflogPos returned earlier, oldest first. It returns ErrReflogRewritten when the reflog no longer holds pos.\n\n```go\ntype ReflogPos struct {\n\t// Offset is the size in bytes of the reflog file, with the files ref\n\t// storage.\n\tOffset int64 `json:\"offset,omitempty\"`\n\t// Top is the newest entry, as git prints it, with the reftable ref\n\t// storage. It is empty when the reflog has no entry.\n\tTop string `json:\"top,omitempty\"`\n}\n```\nReflogPos marks the end of the HEAD reflog at one moment. Taken before a command, it marks where the entries that the command writes start.\n\n```go\nfunc HeadReflogPos(repoRoot string) (ReflogPos, error)\n```\nHeadReflogPos returns the position of the end of the HEAD reflog of the checkout at repoRoot. It is cheap enough for a hook that runs before every shell command, whatever the length of the reflog: a stat with the files ref storage, and one entry read through git with reftable.\n\n","path":"github.com/chainloop-dev/chainloop/app/cli/internal/trace/git","name":"git","synopsis":"","isRedistributable":true}
