Skip to content

Commit e30f153

Browse files
committed
feat(git): show changed files and content in commit diff
Update GitOperations to list changed files, their statuses, and file contents for each commit diff, including deleted files and raw diff output.
1 parent c29071f commit e30f153

File tree

1 file changed

+61
-8
lines changed

1 file changed

+61
-8
lines changed

mpp-core/src/wasmJsMain/kotlin/cc/unitmesh/agent/platform/GitOperations.wasmJs.kt

Lines changed: 61 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -203,21 +203,74 @@ actual class GitOperations actual constructor(private val projectPath: String) {
203203
val module = lg2Module ?: return null
204204

205205
return try {
206+
// Step 1: Get list of changed files using diff --name-status
206207
commandOutputBuffer.clear()
207-
val exitCode = module.callMain(jsArrayOf("show", commitHash)).await<JsNumber>().toInt()
208+
var exitCode = module.callMain(jsArrayOf("diff", "--name-status", "$commitHash^", commitHash)).await<JsNumber>().toInt()
208209

209210
if (exitCode != 0) {
210-
WasmConsole.warn("git show failed with exit code: $exitCode")
211+
WasmConsole.warn("git diff --name-status failed with exit code: $exitCode")
211212
return null
212213
}
213214

214-
val diff = commandOutputBuffer.joinToString("\n")
215-
// TODO: Parse diff to extract file changes, additions, and deletions
215+
val changedFiles = commandOutputBuffer
216+
.filter { it.isNotBlank() }
217+
.mapNotNull { line ->
218+
// Format: "M\tfilename" or "A\tfilename" or "D\tfilename"
219+
val parts = line.split("\t", limit = 2)
220+
if (parts.size == 2) {
221+
Pair(parts[0], parts[1]) // (status, filename)
222+
} else null
223+
}
224+
225+
// Step 2: Get the actual diff for context
226+
commandOutputBuffer.clear()
227+
exitCode = module.callMain(jsArrayOf("diff", "$commitHash^", commitHash)).await<JsNumber>().toInt()
228+
val diffContent = if (exitCode == 0) {
229+
commandOutputBuffer.joinToString("\n")
230+
} else {
231+
""
232+
}
233+
234+
// Step 3: For each modified/added file, read its full content
235+
val filesWithContent = buildString {
236+
for ((status, filename) in changedFiles) {
237+
appendLine("=" .repeat(80))
238+
appendLine("File: $filename (Status: $status)")
239+
appendLine("=" .repeat(80))
240+
241+
when (status) {
242+
"D" -> {
243+
appendLine("[File Deleted]")
244+
}
245+
"A", "M" -> {
246+
// Try to read the file content from current working directory
247+
val content = try {
248+
val fileData = module.FS.readFile(filename).toIntArray()
249+
val bytes = fileData.map { it.toByte() }.toByteArray()
250+
bytes.decodeToString()
251+
} catch (e: Throwable) {
252+
"[Unable to read file: ${e.message}]"
253+
}
254+
appendLine(content)
255+
}
256+
}
257+
appendLine()
258+
}
259+
260+
// Also include the diff for reference
261+
if (diffContent.isNotBlank()) {
262+
appendLine("=" .repeat(80))
263+
appendLine("Git Diff Output:")
264+
appendLine("=" .repeat(80))
265+
appendLine(diffContent)
266+
}
267+
}
268+
216269
GitDiffInfo(
217-
files = emptyList(),
218-
totalAdditions = 0,
219-
totalDeletions = 0,
220-
originDiff = diff
270+
files = changedFiles.map { it.second },
271+
totalAdditions = 0, // Could be parsed from diff output if needed
272+
totalDeletions = 0, // Could be parsed from diff output if needed
273+
originDiff = filesWithContent
221274
)
222275
} catch (e: Throwable) {
223276
WasmConsole.error("Failed to get commit diff: ${e.message}")

0 commit comments

Comments
 (0)