Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

GitCore.ts

148 lines3.8 KB

1/**
2 * GitCore - Effect service contract for low-level Git operations.
3 *
4 * Wraps core repository primitives used by higher-level orchestration
5 * services and WebSocket routes.
6 *
7 * @module GitCore
8 */
9import { ServiceMap } from "effect";
10import type { Effect } from "effect";
11import type {
12 GitCheckoutInput,
13 GitCreateBranchInput,
14 GitCreateWorktreeInput,
15 GitCreateWorktreeResult,
16 GitInitInput,
17 GitListBranchesInput,
18 GitListBranchesResult,
19 GitPullResult,
20 GitRemoveWorktreeInput,
21 GitStatusInput,
22 GitStatusResult,
23} from "@t3tools/contracts";
24​
25import type { GitCommandError } from "../Errors.ts";
26​
27export interface GitStatusDetails extends Omit<GitStatusResult, "pr"> {
28 upstreamRef: string | null;
30​
31export interface GitPreparedCommitContext {
32 stagedSummary: string;
33 stagedPatch: string;
35​
36export interface GitPushResult {
37 status: "pushed" | "skipped_up_to_date";
38 branch: string;
39 upstreamBranch?: string | undefined;
40 setUpstream?: boolean | undefined;
42​
43export interface GitRangeContext {
44 commitSummary: string;
45 diffSummary: string;
46 diffPatch: string;
48​
49/**
50 * GitCoreShape - Service API for low-level Git repository interactions.
51 */
52export interface GitCoreShape {
53 /**
54 * Read Git status for a repository.
55 */
56 readonly status: (input: GitStatusInput) => Effect.Effect<GitStatusResult, GitCommandError>;
57​
58 /**
59 * Read detailed working tree / branch status for a repository.
60 */
61 readonly statusDetails: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
62​
63 /**
64 * Build staged change context for commit generation.
65 */
66 readonly prepareCommitContext: (
67 cwd: string,
68 ) => Effect.Effect<GitPreparedCommitContext | null, GitCommandError>;
69​
70 /**
71 * Create a commit with provided subject/body.
72 */
73 readonly commit: (
74 cwd: string,
75 subject: string,
76 body: string,
77 ) => Effect.Effect<{ commitSha: string }, GitCommandError>;
78​
79 /**
80 * Push current branch, setting upstream if needed.
81 */
82 readonly pushCurrentBranch: (
83 cwd: string,
84 fallbackBranch: string | null,
85 ) => Effect.Effect<GitPushResult, GitCommandError>;
86​
87 /**
88 * Collect commit/diff context between base branch and current HEAD.
89 */
90 readonly readRangeContext: (
91 cwd: string,
92 baseBranch: string,
93 ) => Effect.Effect<GitRangeContext, GitCommandError>;
94​
95 /**
96 * Read a Git config value from the local repository.
97 */
98 readonly readConfigValue: (
99 cwd: string,
100 key: string,
101 ) => Effect.Effect<string | null, GitCommandError>;
102​
103 /**
104 * List local + remote branches and branch metadata.
105 */
106 readonly listBranches: (
107 input: GitListBranchesInput,
108 ) => Effect.Effect<GitListBranchesResult, GitCommandError>;
109​
110 /**
111 * Pull current branch from upstream using fast-forward only.
112 */
113 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<GitPullResult, GitCommandError>;
114​
115 /**
116 * Create a worktree and branch from a base branch.
117 */
118 readonly createWorktree: (
119 input: GitCreateWorktreeInput,
120 ) => Effect.Effect<GitCreateWorktreeResult, GitCommandError>;
121​
122 /**
123 * Remove an existing worktree.
124 */
125 readonly removeWorktree: (input: GitRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
126​
127 /**
128 * Create a local branch.
129 */
130 readonly createBranch: (input: GitCreateBranchInput) => Effect.Effect<void, GitCommandError>;
131​
132 /**
133 * Checkout an existing branch and refresh its upstream metadata in background.
134 */
135 readonly checkoutBranch: (input: GitCheckoutInput) => Effect.Effect<void, GitCommandError>;
136​
137 /**
138 * Initialize a repository in the provided directory.
139 */
140 readonly initRepo: (input: GitInitInput) => Effect.Effect<void, GitCommandError>;
142​
143/**
144 * GitCore - Service tag for low-level Git repository operations.
145 */
146export class GitCore extends ServiceMap.Service<GitCore, GitCoreShape>()(
147 "t3/git/Services/GitCore",
148) {}