-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathgit.ts
More file actions
137 lines (119 loc) · 3.2 KB
/
git.ts
File metadata and controls
137 lines (119 loc) · 3.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/**
* Git utilities for code review
*
* Centralized git operations for diff collection and branch detection.
* Used by both Claude Code hook and OpenCode plugin.
*/
import {
type DiffOption,
type DiffResult,
type DiffType,
type GitCommandResult,
type GitContext,
type ReviewGitRuntime,
type WorktreeInfo,
getCurrentBranch as getCurrentBranchCore,
getDefaultBranch as getDefaultBranchCore,
getWorktrees as getWorktreesCore,
getGitContext as getGitContextCore,
getFileContentsForDiff as getFileContentsForDiffCore,
gitAddFile as gitAddFileCore,
gitResetFile as gitResetFileCore,
parseWorktreeDiffType,
runGitDiff as runGitDiffCore,
runGitDiffWithContext as runGitDiffWithContextCore,
validateFilePath,
} from "@plannotator/shared/review-core";
export type {
DiffOption,
DiffType,
DiffResult,
GitContext,
WorktreeInfo,
} from "@plannotator/shared/review-core";
async function runGit(
args: string[],
options?: { cwd?: string; timeoutMs?: number },
): Promise<GitCommandResult> {
const proc = Bun.spawn(["git", ...args], {
cwd: options?.cwd,
stdout: "pipe",
stderr: "pipe",
});
let timer: ReturnType<typeof setTimeout> | undefined;
if (options?.timeoutMs) {
timer = setTimeout(() => proc.kill(), options.timeoutMs);
}
const [stdout, stderr, exitCode] = await Promise.all([
new Response(proc.stdout).text(),
new Response(proc.stderr).text(),
proc.exited,
]);
if (timer) clearTimeout(timer);
return { stdout, stderr, exitCode };
}
/** Bun-based git runtime. Exported for use with shared utilities (worktree, etc.) */
export const runtime: ReviewGitRuntime = {
runGit,
async readTextFile(path: string): Promise<string | null> {
try {
return await Bun.file(path).text();
} catch {
return null;
}
},
};
export function getCurrentBranch(): Promise<string> {
return getCurrentBranchCore(runtime);
}
export function getDefaultBranch(): Promise<string> {
return getDefaultBranchCore(runtime);
}
export function getWorktrees(): Promise<WorktreeInfo[]> {
return getWorktreesCore(runtime);
}
export function getGitContext(cwd?: string): Promise<GitContext> {
return getGitContextCore(runtime, cwd);
}
export function runGitDiff(
diffType: DiffType,
defaultBranch: string = "main",
cwd?: string,
): Promise<DiffResult> {
return runGitDiffCore(runtime, diffType, defaultBranch, cwd);
}
export function runGitDiffWithContext(
diffType: DiffType,
gitContext: GitContext,
): Promise<DiffResult> {
return runGitDiffWithContextCore(runtime, diffType, gitContext);
}
export function getFileContentsForDiff(
diffType: DiffType,
defaultBranch: string,
filePath: string,
oldPath?: string,
cwd?: string,
): Promise<{ oldContent: string | null; newContent: string | null }> {
return getFileContentsForDiffCore(
runtime,
diffType,
defaultBranch,
filePath,
oldPath,
cwd,
);
}
export function gitAddFile(
filePath: string,
cwd?: string,
): Promise<void> {
return gitAddFileCore(runtime, filePath, cwd);
}
export function gitResetFile(
filePath: string,
cwd?: string,
): Promise<void> {
return gitResetFileCore(runtime, filePath, cwd);
}
export { parseWorktreeDiffType, validateFilePath };