gunk-dev/the-valley

client.go

sourced9c8073fd613main's published tip history
4.4 KiB
1package git
2
3import "context"
4
5// Client defines the interface for all git operations Klaus uses.
6// Implementations must be safe for concurrent use.
7type Client interface {
8 // CommonDir returns the absolute path to the git common directory.
9 // This works from worktrees too (returns the main repo's .git dir).
10 CommonDir(ctx context.Context) (string, error)
11
12 // FetchAll fetches all branches and tags from origin, pruning deleted remote branches.
13 FetchAll(ctx context.Context, repoDir string) error
14
15 // FetchBranch fetches a single branch from origin.
16 FetchBranch(ctx context.Context, repoDir, branch string) error
17
18 // WorktreeAdd creates a new worktree at path on a new branch based on startPoint.
19 // If the branch is already checked out in a stale worktree, it prunes and retries once.
20 WorktreeAdd(ctx context.Context, repoDir, path, branch, startPoint string) error
21
22 // WorktreeRemove removes a worktree. repoDir is the main repo directory.
23 WorktreeRemove(ctx context.Context, repoDir, path string) error
24
25 // WorktreeAddTrack creates a worktree tracking an existing remote branch.
26 // It never removes a worktree: it refuses when path exists, and with a
27 // *WorktreeInUseError when the branch is checked out in a worktree whose
28 // directory exists. Unless discard is set, it refuses with a
29 // *LocalWorkError when the local branch has commits origin lacks.
30 WorktreeAddTrack(ctx context.Context, repoDir, path, branch string, discard bool) error
31
32 // WorktreePrune removes stale worktree tracking information.
33 WorktreePrune(ctx context.Context, repoDir string) error
34
35 // BranchDelete deletes a local branch.
36 BranchDelete(ctx context.Context, repoDir, branch string) error
37
38 // IsClean reports whether the working tree at repoDir has no uncommitted changes.
39 IsClean(ctx context.Context, repoDir string) (bool, error)
40
41 // CurrentBranch returns the short name of the currently checked-out branch,
42 // or "" when HEAD is detached.
43 CurrentBranch(ctx context.Context, repoDir string) (string, error)
44
45 // HasUpstream reports whether the current branch has a tracking upstream.
46 HasUpstream(ctx context.Context, repoDir string) (bool, error)
47
48 // MergeFastForward runs `git merge --ff-only @{u}` in repoDir.
49 MergeFastForward(ctx context.Context, repoDir string) error
50
51 // CommitsBehindUpstream returns how many commits the current branch is
52 // behind its upstream. Returns 0 if already up-to-date.
53 CommitsBehindUpstream(ctx context.Context, repoDir string) (int, error)
54
55 // CommitsAhead returns the number of commits on ref not reachable from base.
56 CommitsAhead(ctx context.Context, repoDir, base, ref string) (int, error)
57
58 // BranchPushed reports whether origin's branch tip equals the local tip,
59 // checked against the remote itself; an error means unverified.
60 BranchPushed(ctx context.Context, repoDir, branch string) (bool, error)
61
62 // RemoteBranchSHA returns origin's tip of branch, or "" when origin has
63 // no such branch, checked against the remote itself.
64 RemoteBranchSHA(ctx context.Context, repoDir, branch string) (string, error)
65
66 // EnsureDataRef ensures the custom data ref exists. Creates it with an empty
67 // initial commit if it doesn't.
68 EnsureDataRef(ctx context.Context, repoDir, dataRef string) error
69
70 // SyncToDataRef commits files to the data ref using a temporary index.
71 // files is a map of path-in-tree -> local-file-path.
72 SyncToDataRef(ctx context.Context, repoDir, dataRef, commitMsg string, files map[string]string) error
73
74 // EnsureClone clones a repo to destDir if it doesn't already exist.
75 // If it already exists, fetches the latest from origin.
76 EnsureClone(ctx context.Context, cloneURL, destDir string) error
77
78 // PushDataRef pushes the data ref to origin.
79 PushDataRef(ctx context.Context, repoDir, dataRef string) error
80
81 // FetchDataRef updates the local data ref from origin. Best-effort: the
82 // returned error can be ignored when there is no remote or no such ref.
83 FetchDataRef(ctx context.Context, repoDir, dataRef string) error
84
85 // ReadDataRefFile returns the raw bytes of a file stored in the data ref tree.
86 ReadDataRefFile(ctx context.Context, repoDir, dataRef, treePath string) ([]byte, error)
87
88 // InstallHooks installs klaus's hooks in the given linked worktree only:
89 // commit-msg strips Claude/Anthropic attribution and adds prov's trailers,
90 // pre-commit runs format and re-stages the staged files, and each then
91 // runs the repository's own hook of its name.
92 InstallHooks(ctx context.Context, worktreeDir string, prov Provenance, format []string) error
93}