fix: serialize git status/stage/commit/log results for IPC transfer

simple-git returns objects with Map/Set fields that cannot be cloned
over Electron IPC. Flatten all git operation results into plain
JSON-serializable objects to fix 'An object could not be cloned' error.
This commit is contained in:
2026-06-14 01:43:29 +05:30
parent 2e3f4dda0f
commit 809c266e54
+36 -7
View File
@@ -7,9 +7,20 @@ function getGitInstance(dir) {
async function getStatus(dir) {
try {
const git = getGitInstance(dir);
return await git.status();
const result = await git.status();
const files = [];
for (const [filePath, status] of Object.entries(result.files || {})) {
files.push({
filePath,
status: status.working_dir === 'M' ? 'modified'
: status.working_dir === 'A' || status.index === 'A' ? 'added'
: status.working_dir === 'D' || status.index === 'D' ? 'deleted'
: 'untracked',
});
}
return { files };
} catch (_err) {
return { error: 'Not a git repository' };
return { files: [], error: 'Not a git repository' };
}
}
@@ -17,16 +28,25 @@ async function stage(dir, files) {
try {
const git = getGitInstance(dir);
await git.add(files);
return await git.status();
const result = await git.status();
const staged = [];
for (const [filePath, status] of Object.entries(result.files || {})) {
staged.push({
filePath,
status: status.index === 'A' ? 'added' : status.index === 'M' ? 'modified' : status.index === 'D' ? 'deleted' : 'untracked',
});
}
return { files: staged };
} catch (err) {
return { error: err.message };
return { files: [], error: err.message };
}
}
async function commit(dir, message) {
try {
const git = getGitInstance(dir);
return await git.commit(message);
const result = await git.commit(message);
return { summary: result?.summary || 'Committed' };
} catch (err) {
return { error: err.message };
}
@@ -35,9 +55,18 @@ async function commit(dir, message) {
async function log(dir, maxCount = 20) {
try {
const git = getGitInstance(dir);
return await git.log({ maxCount });
const result = await git.log({ maxCount });
return {
latest: result?.latest || null,
all: (result?.all || []).map((entry) => ({
hash: entry.hash,
message: entry.message,
author: entry.author_name,
date: entry.date,
})),
};
} catch (err) {
return { error: err.message };
return { all: [], error: err.message };
}
}