forked from anomalyco/opencode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgit-status.ts
More file actions
204 lines (178 loc) · 6.15 KB
/
git-status.ts
File metadata and controls
204 lines (178 loc) · 6.15 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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
import { z } from "zod"
import { Tool } from "openagent-ai"
export const gitStatus = Tool.define({
id: "git-status",
description: `Shows the current status of the git repository including staged, unstaged, and untracked files
- Lists modified, added, deleted, and untracked files
- Shows if the working directory is clean or has pending changes
Use this tool when you need to check what changes are ready to commit or understand the current state of your repository`,
parameters: z.object({
showUntracked: z
.boolean()
.optional()
.describe(
"Whether to include untracked files in the output. Defaults to true.",
),
}),
async execute(params): Promise<{
metadata: Tool.Metadata
output: string
}> {
const { showUntracked = true } = params
try {
// Check if we're in a git repository
const gitCheck = Bun.spawn(["git", "rev-parse", "--git-dir"], {
stdout: "pipe",
stderr: "pipe",
})
const gitCheckExit = await gitCheck.exited
if (gitCheckExit !== 0) {
throw new Error("Not in a git repository")
}
// Get current branch information
const branchProc = Bun.spawn(["git", "branch", "--show-current"], {
stdout: "pipe",
stderr: "pipe",
})
const branchOutput = await new Response(branchProc.stdout).text()
const branchExit = await branchProc.exited
const currentBranch = branchExit === 0 ? branchOutput.trim() : "unknown"
// Get remote tracking information
const remoteProc = Bun.spawn(
["git", "status", "--porcelain=v1", "--branch"],
{
stdout: "pipe",
stderr: "pipe",
},
)
const remoteOutput = await new Response(remoteProc.stdout).text()
const remoteExit = await remoteProc.exited
if (remoteExit !== 0) {
throw new Error("Failed to get git status")
}
// Parse the output
const lines = remoteOutput.trim().split("\n")
const branchLine = lines[0]
let ahead = 0
let behind = 0
let trackingBranch = ""
// Parse branch tracking info
if (branchLine.startsWith("##")) {
const branchInfo = branchLine.substring(3)
const trackingMatch = branchInfo.match(
/(.+?)\.\.\.(.+?)(?:\s+\[(.+?)\])?$/,
)
if (trackingMatch) {
trackingBranch = trackingMatch[2]
const statusInfo = trackingMatch[3]
if (statusInfo) {
const aheadMatch = statusInfo.match(/ahead (\d+)/)
const behindMatch = statusInfo.match(/behind (\d+)/)
if (aheadMatch) ahead = parseInt(aheadMatch[1])
if (behindMatch) behind = parseInt(behindMatch[1])
}
}
}
// Parse file changes
const fileLines = lines.slice(1).filter((line) => line.trim())
const staged: string[] = []
const unstaged: string[] = []
const untracked: string[] = []
const deleted: string[] = []
for (const line of fileLines) {
if (line.length < 3) continue
const indexStatus = line[0]
const workTreeStatus = line[1]
const fileName = line.substring(3)
// Staged changes
if (indexStatus !== " " && indexStatus !== "?") {
if (indexStatus === "D") {
deleted.push(fileName)
} else {
staged.push(fileName)
}
}
// Unstaged changes
if (workTreeStatus !== " " && workTreeStatus !== "?") {
if (workTreeStatus === "D") {
deleted.push(fileName)
} else {
unstaged.push(fileName)
}
}
// Untracked files
if (indexStatus === "?" && workTreeStatus === "?") {
untracked.push(fileName)
}
}
// Build output
let output = `Branch: ${currentBranch}`
if (trackingBranch) {
output += `\nTracking: ${trackingBranch}`
if (ahead > 0)
output += `\nAhead by ${ahead} commit${ahead > 1 ? "s" : ""}`
if (behind > 0)
output += `\nBehind by ${behind} commit${behind > 1 ? "s" : ""}`
}
const totalChanges =
staged.length +
unstaged.length +
(showUntracked ? untracked.length : 0) +
deleted.length
if (totalChanges === 0) {
output += "\n\nWorking directory clean"
} else {
output += "\n"
if (staged.length > 0) {
output += `\nStaged for commit (${staged.length} file${staged.length > 1 ? "s" : ""}):\n`
staged.forEach((file) => (output += ` + ${file}\n`))
}
if (unstaged.length > 0) {
output += `\nUnstaged changes (${unstaged.length} file${unstaged.length > 1 ? "s" : ""}):\n`
unstaged.forEach((file) => (output += ` M ${file}\n`))
}
if (deleted.length > 0) {
output += `\nDeleted files (${deleted.length} file${deleted.length > 1 ? "s" : ""}):\n`
deleted.forEach((file) => (output += ` D ${file}\n`))
}
if (showUntracked && untracked.length > 0) {
output += `\nUntracked files (${untracked.length} file${untracked.length > 1 ? "s" : ""}):\n`
untracked.forEach((file) => (output += ` ? ${file}\n`))
}
}
return {
metadata: {
branch: currentBranch,
trackingBranch,
ahead,
behind,
staged: staged.length,
unstaged: unstaged.length,
untracked: untracked.length,
deleted: deleted.length,
totalChanges,
isClean: totalChanges === 0,
title:
totalChanges === 0
? "Working directory clean"
: `${totalChanges} change${totalChanges > 1 ? "s" : ""} detected`,
},
output: output.trim(),
}
} catch (error) {
const errorMessage =
error instanceof Error ? error.message : String(error)
return {
metadata: {
branch: "unknown",
trackingBranch: "",
isClean: false,
error: true,
message: errorMessage,
title: "Git status failed",
},
output: `Failed to get git status: ${errorMessage}`,
}
}
},
})