Open History to see the last commit
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;
29}
30
31export interface GitPreparedCommitContext {
32 stagedSummary: string;
33 stagedPatch: string;
34}
35
36export interface GitPushResult {
37 status: "pushed" | "skipped_up_to_date";
38 branch: string;
39 upstreamBranch?: string | undefined;
40 setUpstream?: boolean | undefined;
41}
42
43export interface GitRangeContext {
44 commitSummary: string;
45 diffSummary: string;
46 diffPatch: string;
47}
48
49export interface GitRenameBranchInput {
50 cwd: string;
51 oldBranch: string;
52 newBranch: string;
53}
54
55export interface GitRenameBranchResult {
56 branch: string;
57}
58
59/**
60 * GitCoreShape - Service API for low-level Git repository interactions.
61 */
62export interface GitCoreShape {
63 /**
64 * Read Git status for a repository.
65 */
66 readonly status: (input: GitStatusInput) => Effect.Effect<GitStatusResult, GitCommandError>;
67
68 /**
69 * Read detailed working tree / branch status for a repository.
70 */
71 readonly statusDetails: (cwd: string) => Effect.Effect<GitStatusDetails, GitCommandError>;
72
73 /**
74 * Build staged change context for commit generation.
75 */
76 readonly prepareCommitContext: (
77 cwd: string,
78 ) => Effect.Effect<GitPreparedCommitContext | null, GitCommandError>;
79
80 /**
81 * Create a commit with provided subject/body.
82 */
83 readonly commit: (
84 cwd: string,
85 subject: string,
86 body: string,
87 ) => Effect.Effect<{ commitSha: string }, GitCommandError>;
88
89 /**
90 * Push current branch, setting upstream if needed.
91 */
92 readonly pushCurrentBranch: (
93 cwd: string,
94 fallbackBranch: string | null,
95 ) => Effect.Effect<GitPushResult, GitCommandError>;
96
97 /**
98 * Collect commit/diff context between base branch and current HEAD.
99 */
100 readonly readRangeContext: (
101 cwd: string,
102 baseBranch: string,
103 ) => Effect.Effect<GitRangeContext, GitCommandError>;
104
105 /**
106 * Read a Git config value from the local repository.
107 */
108 readonly readConfigValue: (
109 cwd: string,
110 key: string,
111 ) => Effect.Effect<string | null, GitCommandError>;
112
113 /**
114 * List local branches and branch metadata.
115 */
116 readonly listBranches: (
117 input: GitListBranchesInput,
118 ) => Effect.Effect<GitListBranchesResult, GitCommandError>;
119
120 /**
121 * Pull current branch from upstream using fast-forward only.
122 */
123 readonly pullCurrentBranch: (cwd: string) => Effect.Effect<GitPullResult, GitCommandError>;
124
125 /**
126 * Create a worktree and branch from a base branch.
127 */
128 readonly createWorktree: (
129 input: GitCreateWorktreeInput,
130 ) => Effect.Effect<GitCreateWorktreeResult, GitCommandError>;
131
132 /**
133 * Remove an existing worktree.
134 */
135 readonly removeWorktree: (input: GitRemoveWorktreeInput) => Effect.Effect<void, GitCommandError>;
136
137 /**
138 * Rename an existing local branch.
139 */
140 readonly renameBranch: (
141 input: GitRenameBranchInput,
142 ) => Effect.Effect<GitRenameBranchResult, GitCommandError>;
143
144 /**
145 * Create a local branch.
146 */
147 readonly createBranch: (input: GitCreateBranchInput) => Effect.Effect<void, GitCommandError>;
148
149 /**
150 * Checkout an existing branch and refresh its upstream metadata in background.
151 */
152 readonly checkoutBranch: (input: GitCheckoutInput) => Effect.Effect<void, GitCommandError>;
153
154 /**
155 * Initialize a repository in the provided directory.
156 */
157 readonly initRepo: (input: GitInitInput) => Effect.Effect<void, GitCommandError>;
158}
159
160/**
161 * GitCore - Service tag for low-level Git repository operations.
162 */
163export class GitCore extends ServiceMap.Service<GitCore, GitCoreShape>()(
164 "t3/git/Services/GitCore",
165) {}