git

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: May 12, 2026 License: MIT Imports: 6 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

Index

Constants

This section is empty.

Variables

View Source
var ErrNotARepo = errors.New("not inside a git repository")

Functions

func Clone

func Clone(remote, dest string) error

Clone runs `git clone <remote> <dest>`. The destination is created by git itself; it must not already exist.

Types

type Error

type Error struct {
	Command string
	Stderr  string
	Err     error
}

Error wraps a failed git invocation with the command line and the captured stderr. Returned (wrapped) by every method on *Repo when the underlying `git` exits non-zero.

func (*Error) Error

func (e *Error) Error() string

func (*Error) Unwrap

func (e *Error) Unwrap() error

type Info

type Info struct {
	Root      string // absolute path
	Name      string // basename of root
	RemoteURL string // empty if no origin configured
}

func Detect

func Detect(dir string) (*Info, error)

Detect finds the git repo containing dir (or any of its parents). For a linked worktree it returns the *main* worktree's root, so that every worktree of a repo resolves to the same Info — without this, resolveRepo auto-registers a fresh repo per worktree.

type Repo

type Repo struct {
	Root string
}

Repo wraps a local git working tree at Root. Every method shells out to `git` via os/exec, matching detect.go's existing pattern. We don't pull in go-git for two reasons: (1) sync repos are small, the cost of the subprocess is negligible, and (2) `git` is already on every developer's PATH and behaves the same as the user's manual invocations — handy for debugging.

Errors from each method wrap stderr from git via *Error so callers can `errors.As` the wrapped form to surface the original command and stderr without re-shelling.

func Init

func Init(path string) (*Repo, error)

Init runs `git init -b main` at path and returns a *Repo bound to it. The directory is created if it doesn't yet exist. We pin the initial branch to `main` so sync repos look the same regardless of the user's `init.defaultBranch` (Linux/older git defaults to `master`, which mismatches the bare remote we push to and produces an empty checkout for the next cloner).

func Open

func Open(path string) (*Repo, error)

Open returns a *Repo at path if it contains a .git directory or file (a worktree pointer). Errors with ErrNotARepo otherwise so callers can distinguish "not a repo" from "filesystem error".

func (*Repo) Add

func (r *Repo) Add(paths ...string) error

Add runs `git add` on the supplied paths. With no paths, equivalent to `git add -A`.

func (*Repo) AddRemote

func (r *Repo) AddRemote(name, url string) error

AddRemote runs `git remote add <name> <url>`.

func (*Repo) Commit

func (r *Repo) Commit(message string) (string, error)

Commit runs `git commit -m <message>` and returns the resulting SHA. If the index has no staged changes, returns ("", nil) so the caller can distinguish "nothing to commit" from a hard error.

func (*Repo) CurrentBranch

func (r *Repo) CurrentBranch() (string, error)

CurrentBranch returns the name of the currently checked-out branch (e.g. "main", "master", or whatever the user's `init.defaultBranch` resolved to). Used so we don't hardcode "main" when push --set-upstream runs after `git init`.

func (*Repo) HasUncommittedChanges

func (r *Repo) HasUncommittedChanges() (bool, error)

HasUncommittedChanges reports whether the working tree or index has any uncommitted changes. `git status --porcelain` produces no output on a clean tree.

func (*Repo) HasUnpulledChanges

func (r *Repo) HasUnpulledChanges() (bool, error)

HasUnpulledChanges reports whether the upstream branch has commits not yet present locally. Best-effort — if the repo has no upstream configured (e.g., immediately after `git init` and before the first push), returns false with no error.

func (*Repo) IsEmpty

func (r *Repo) IsEmpty() (bool, error)

IsEmpty reports whether the repository has no commits at all. Detected by `git rev-parse HEAD` failing — the canonical "no commits yet" signal in git.

func (*Repo) Mv

func (r *Repo) Mv(from, to string) error

Mv runs `git mv <from> <to>`. The caller is responsible for any parent-directory creation on the new side; git mv will fail if the parent doesn't exist.

func (*Repo) Pull

func (r *Repo) Pull() error

Pull runs a fast-forward-only `git pull --ff-only`. We use ff-only rather than allowing merge so a sync race that produces a true conflict surfaces immediately rather than silently merging in the background.

func (*Repo) Push

func (r *Repo) Push() error

Push runs `git push`.

func (*Repo) PushSetUpstream

func (r *Repo) PushSetUpstream(remote, branch string) error

PushSetUpstream runs `git push --set-upstream <remote> <branch>`. Used on first push from `bacio sync init` so subsequent `git push` invocations have a tracking branch.

func (*Repo) RemoteHasContent

func (r *Repo) RemoteHasContent(remote string) (bool, error)

RemoteHasContent reports whether the named remote currently has any branch heads. Used by `bacio sync init` to refuse to bootstrap against a populated remote.

func (*Repo) WriteGitattributes

func (r *Repo) WriteGitattributes(content string) error

WriteGitattributes writes the supplied content to .gitattributes at the repo root. Used by `bacio sync init` to pin LF line endings (`* -text`) so git's autocrlf doesn't rewrite the canonical YAML on Windows checkouts.

Jump to

Keyboard shortcuts

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