Documentation
¶
Overview ¶
Package git wraps os/exec calls to the git binary and is the sole interface through which safegit interacts with git plumbing commands. All functions shell out to git and return structured results; no other package may invoke git directly.
Index ¶
- Variables
- func AddFile(ctx context.Context, indexPath, filePath string) error
- func CatFileBlob(ctx context.Context, sha string) ([]byte, error)
- func CommitMessage(ctx context.Context, rev string) (string, error)
- func CommitTree(ctx context.Context, treeSHA, parentSHA, message string) (string, error)
- func CommitTreeWithAuthor(ctx context.Context, treeSHA string, parentSHAs []string, message string, ...) (string, error)
- func CommonGitDir(ctx context.Context) (string, error)
- func GitDir(ctx context.Context) (string, error)
- func HashObject(ctx context.Context, path string) (string, error)
- func HashObjectWrite(ctx context.Context, path string) (string, error)
- func HashObjectWriteBytes(ctx context.Context, data []byte) (string, error)
- func HeadRef(ctx context.Context) (string, error)
- func IsAncestorOf(ctx context.Context, commitSHA, descendantSHA string) (bool, error)
- func IsIgnored(ctx context.Context, filePath string) (bool, error)
- func IsTracked(ctx context.Context, filePath string) (bool, error)
- func ListSkipWorktreeFiles(ctx context.Context) ([]string, error)
- func ListTrackedIgnoredFiles(ctx context.Context) ([]string, error)
- func MkTree(ctx context.Context, entries []TreeEntry) (string, error)
- func ReadTree(ctx context.Context, indexPath, treeish string) error
- func RepoRoot(ctx context.Context) (string, error)
- func RevParse(ctx context.Context, rev string) (string, error)
- func RmCached(ctx context.Context, indexPath, filePath string) error
- func Run(ctx context.Context, args ...string) (stdout, stderr string, err error)
- func RunPassthrough(ctx context.Context, args ...string) error
- func RunWithEnv(ctx context.Context, env []string, args ...string) (stdout, stderr string, err error)
- func RunWithEnvStdin(ctx context.Context, env []string, stdin []byte, args ...string) (stdout, stderr string, err error)
- func RunWithGitDir(ctx context.Context, gitDir string, workTree string, args ...string) (stdout, stderr string, err error)
- func SyncMainIndex(ctx context.Context, treeish string) error
- func SyncMainIndexWithWorktree(ctx context.Context, treeish string) ([]string, error)
- func UpdateRef(ctx context.Context, ref, newSHA, oldSHA string) error
- func WriteTree(ctx context.Context, indexPath string) (string, error)
- type AuthorInfo
- type CommitInfo
- type ObjectEntry
- type ObjectIterator
- type TreeEntry
Constants ¶
This section is empty.
Variables ¶
var ErrDetachedHead = fmt.Errorf("HEAD is detached (not on a branch); check out a branch first or use --branch")
ErrDetachedHead is returned when HEAD is not on a branch.
Functions ¶
func CatFileBlob ¶ added in v0.14.0
CatFileBlob reads blob content by SHA via git cat-file -p.
func CommitMessage ¶
CommitMessage returns the full commit message of the given revision.
func CommitTree ¶
CommitTree creates a commit object from a tree SHA and parent, returns commit SHA. If parentSHA is empty, creates a root commit.
func CommitTreeWithAuthor ¶ added in v0.7.0
func CommitTreeWithAuthor(ctx context.Context, treeSHA string, parentSHAs []string, message string, author, committer AuthorInfo) (string, error)
CommitTreeWithAuthor creates a commit object with explicit author and committer identity, returning the new commit SHA.
func CommonGitDir ¶ added in v0.1.1
CommonGitDir returns the path to the shared .git directory. For normal repos this equals GitDir(); for worktrees it returns the main .git dir that is shared across all worktrees. Lock files should live here so that worktrees committing to the same branch serialize correctly.
func HashObject ¶ added in v0.11.0
HashObject returns the blob SHA for a file without writing to the object store.
func HashObjectWrite ¶ added in v0.12.0
HashObjectWrite hashes a file and writes the blob to the object store, returning the blob SHA.
func HashObjectWriteBytes ¶ added in v0.14.0
HashObjectWriteBytes writes in-memory bytes as a blob to the object store via git hash-object -w --stdin, returning the blob SHA.
func HeadRef ¶
HeadRef returns the current branch ref (e.g. "refs/heads/main"). Returns ErrDetachedHead if HEAD is not on a branch.
func IsAncestorOf ¶ added in v0.13.0
IsAncestorOf checks whether commitSHA is an ancestor of (or equal to) descendantSHA. Uses git merge-base --is-ancestor which exits 0 if true, 1 if false, and other codes on error.
func IsTracked ¶
IsTracked checks whether a file is tracked by git (present in HEAD tree). Uses cat-file instead of ls-files because safegit never writes to the main index -- files committed via safegit exist in HEAD but not in .git/index.
func ListSkipWorktreeFiles ¶ added in v0.9.0
ListSkipWorktreeFiles returns the paths of all files with the skip-worktree flag set in the main index. It parses `git ls-files -v` output, selecting lines that start with "S " (the skip-worktree indicator).
func ListTrackedIgnoredFiles ¶ added in v0.17.1
ListTrackedIgnoredFiles returns the paths of all files that are tracked in the index but ignored by .gitignore rules. These are files that were once committed and later gitignored -- read-tree --reset -u would overwrite them, destroying local modifications (e.g., config files with secrets).
func MkTree ¶ added in v0.12.0
MkTree creates a tree object from a slice of TreeEntry values and returns the tree SHA. Each entry must have Mode, ObjectType, SHA, and Path populated. Input is piped to `git mktree` as "<mode> <type> <sha>\t<name>\n".
func RmCached ¶
RmCached removes a file or directory from a custom index without touching the working tree.
func RunPassthrough ¶ added in v0.1.1
RunPassthrough executes a git command with stdin/stdout/stderr wired to the terminal (os.Stdin, os.Stdout, os.Stderr). It prepends --no-optional-locks like Run, but does not capture output -- suitable for interactive/pager commands.
func RunWithEnv ¶
func RunWithEnv(ctx context.Context, env []string, args ...string) (stdout, stderr string, err error)
RunWithEnv executes a git command with additional environment variables.
func RunWithEnvStdin ¶
func RunWithEnvStdin(ctx context.Context, env []string, stdin []byte, args ...string) (stdout, stderr string, err error)
RunWithEnvStdin executes a git command with environment variables and stdin data.
func RunWithGitDir ¶ added in v0.15.0
func RunWithGitDir(ctx context.Context, gitDir string, workTree string, args ...string) (stdout, stderr string, err error)
RunWithGitDir executes a git command against a specific git directory and work tree, rather than relying on cwd-based discovery. Sets GIT_DIR, GIT_WORK_TREE, and cmd.Dir so both git and cwd-relative paths resolve against the target repo.
func SyncMainIndex ¶
SyncMainIndex updates the main .git/index to match the given treeish. This makes git status/diff reflect the committed state after safegit commits. Skip-worktree flags are preserved across the read-tree rebuild.
func SyncMainIndexWithWorktree ¶ added in v0.16.0
SyncMainIndexWithWorktree updates the main .git/index AND the working tree to match the given treeish. Uses --reset -u, so the working tree must be clean before calling. Needed after history rewrites (scrub) where committed blobs have changed and the working tree must reflect the new content.
Tracked+gitignored files (committed then later gitignored, e.g., config files with secrets) are protected: skip-worktree is set before read-tree so --reset -u does not overwrite them. Pre-existing skip-worktree flags are also preserved.
Returns the list of protected tracked+gitignored paths (empty if none).
Types ¶
type AuthorInfo ¶ added in v0.7.0
type AuthorInfo struct {
Name string
Email string
Date string // raw git date format: "1234567890 +0200"
}
AuthorInfo holds the name, email, and raw git date for an author or committer.
type CommitInfo ¶ added in v0.7.0
type CommitInfo struct {
Tree string
Parents []string
Author AuthorInfo
Committer AuthorInfo
Message string
}
CommitInfo holds the parsed contents of a git commit object.
func ParseCommit ¶ added in v0.7.0
func ParseCommit(ctx context.Context, sha string) (CommitInfo, error)
ParseCommit reads and parses a commit object by SHA using git cat-file.
type ObjectEntry ¶ added in v0.14.0
type ObjectEntry struct {
SHA string
Type string // "blob", "commit", or "tag" (trees are skipped)
Size int
Content []byte
}
ObjectEntry holds one object read from a git cat-file --batch stream.
type ObjectIterator ¶ added in v0.14.0
type ObjectIterator struct {
// contains filtered or unexported fields
}
ObjectIterator streams objects from a long-running git cat-file process.
func CatFileBatchAll ¶ added in v0.14.0
func CatFileBatchAll(ctx context.Context) (*ObjectIterator, error)
CatFileBatchAll starts a git cat-file --batch-all-objects --batch subprocess and returns an ObjectIterator for streaming the results. The caller must call Close() when done.
func CatFileBatchAllWithDir ¶ added in v0.15.0
func CatFileBatchAllWithDir(ctx context.Context, gitDir string) (*ObjectIterator, error)
CatFileBatchAllWithDir starts a git cat-file --batch-all-objects --batch subprocess targeting a specific git directory. Returns an ObjectIterator for streaming the results. The caller must call Close() when done.
func (*ObjectIterator) Close ¶ added in v0.14.0
func (it *ObjectIterator) Close() error
Close kills the subprocess if it is still running and waits for it to exit.
func (*ObjectIterator) Next ¶ added in v0.14.0
func (it *ObjectIterator) Next() (*ObjectEntry, error)
Next reads the next non-tree object from the stream. Trees are silently skipped. Returns io.EOF when the stream ends.
type TreeEntry ¶ added in v0.11.0
type TreeEntry struct {
SHA string // SHA of the object (blob or tree)
Path string // repo-relative path (full path for recursive, basename for non-recursive)
Mode string // file mode (e.g. "100644", "040000")
ObjectType string // object type (e.g. "blob", "tree")
}
TreeEntry represents an entry from git ls-tree (blob, tree, or other object).