mirror of
https://github.com/siteboon/claudecodeui.git
synced 2026-07-07 06:02:43 +08:00
fix: resolve source control and chat UX bugs
- make staging real in the git panel: new /stage and /unstage endpoints, /status now reports the actual index via porcelain -z (handles renames, conflicts, and paths with spaces), and Stage/Unstage All run real git commands instead of toggling client-side state - add branch search to the header dropdown and Branches tab - persist the chosen permission mode per provider so a brand-new chat keeps it once the session id arrives, instead of reverting to default - replace cmdk's fuzzy model search with strict token matching so "chatgpt" no longer surfaces unrelated models - unit tests for the git status parser
This commit is contained in:
@@ -293,6 +293,76 @@ async function resolveRepositoryFilePath(projectPath, filePath) {
|
||||
}
|
||||
|
||||
// Get git status for a project
|
||||
/**
|
||||
* Parses `git status --porcelain=v1 -z` output into the response shape the
|
||||
* git panel consumes. NUL-separated entries carry no path quoting, so names
|
||||
* with spaces/unicode survive intact (the plain porcelain output quotes and
|
||||
* escapes them, which broke the old line-based parser).
|
||||
*
|
||||
* `staged` lists paths with index-side changes. The UI renders its "Staged"
|
||||
* section from this list so it always mirrors the real git index (including
|
||||
* files staged outside the app, e.g. via VSCode or the terminal).
|
||||
*
|
||||
* Exported for tests.
|
||||
*/
|
||||
export function parseGitStatusOutput(statusOutput) {
|
||||
const modified = [];
|
||||
const added = [];
|
||||
const deleted = [];
|
||||
const untracked = [];
|
||||
const staged = [];
|
||||
|
||||
const statusEntries = statusOutput.split('\0');
|
||||
for (let entryIndex = 0; entryIndex < statusEntries.length; entryIndex++) {
|
||||
const entry = statusEntries[entryIndex];
|
||||
if (!entry || entry.length < 4) continue;
|
||||
|
||||
// Porcelain v1: X = index (staged) status, Y = worktree (unstaged) status.
|
||||
const indexStatus = entry[0];
|
||||
const worktreeStatus = entry[1];
|
||||
const file = entry.slice(3);
|
||||
|
||||
// Renames/copies carry the original path as the following NUL entry;
|
||||
// the UI tracks the post-rename path only.
|
||||
if (indexStatus === 'R' || indexStatus === 'C') {
|
||||
entryIndex += 1;
|
||||
}
|
||||
|
||||
if (indexStatus === '?') {
|
||||
untracked.push(file);
|
||||
continue;
|
||||
}
|
||||
if (indexStatus === '!') {
|
||||
continue; // ignored files are never reported
|
||||
}
|
||||
|
||||
const isConflict =
|
||||
indexStatus === 'U' || worktreeStatus === 'U' ||
|
||||
(indexStatus === 'A' && worktreeStatus === 'A') ||
|
||||
(indexStatus === 'D' && worktreeStatus === 'D');
|
||||
if (isConflict) {
|
||||
// Merge conflicts must be resolved in the worktree first; surface them
|
||||
// as modified and never as staged.
|
||||
modified.push(file);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (indexStatus !== ' ') {
|
||||
staged.push(file);
|
||||
}
|
||||
|
||||
if (indexStatus === 'D' || worktreeStatus === 'D') {
|
||||
deleted.push(file);
|
||||
} else if (indexStatus === 'A' || worktreeStatus === 'A') {
|
||||
added.push(file);
|
||||
} else {
|
||||
modified.push(file);
|
||||
}
|
||||
}
|
||||
|
||||
return { modified, added, deleted, untracked, staged };
|
||||
}
|
||||
|
||||
router.get('/status', async (req, res) => {
|
||||
const { project } = req.query;
|
||||
|
||||
@@ -309,30 +379,8 @@ router.get('/status', async (req, res) => {
|
||||
const branch = await getCurrentBranchName(projectPath);
|
||||
const hasCommits = await repositoryHasCommits(projectPath);
|
||||
|
||||
// Get git status
|
||||
const { stdout: statusOutput } = await spawnAsync('git', ['status', '--porcelain'], { cwd: projectPath });
|
||||
|
||||
const modified = [];
|
||||
const added = [];
|
||||
const deleted = [];
|
||||
const untracked = [];
|
||||
|
||||
statusOutput.split('\n').forEach(line => {
|
||||
if (!line.trim()) return;
|
||||
|
||||
const status = line.substring(0, 2);
|
||||
const file = line.substring(3);
|
||||
|
||||
if (status === 'M ' || status === ' M' || status === 'MM') {
|
||||
modified.push(file);
|
||||
} else if (status === 'A ' || status === 'AM') {
|
||||
added.push(file);
|
||||
} else if (status === 'D ' || status === ' D') {
|
||||
deleted.push(file);
|
||||
} else if (status === '??') {
|
||||
untracked.push(file);
|
||||
}
|
||||
});
|
||||
const { stdout: statusOutput } = await spawnAsync('git', ['status', '--porcelain=v1', '-z'], { cwd: projectPath });
|
||||
const { modified, added, deleted, untracked, staged } = parseGitStatusOutput(statusOutput);
|
||||
|
||||
res.json({
|
||||
branch,
|
||||
@@ -340,7 +388,8 @@ router.get('/status', async (req, res) => {
|
||||
modified,
|
||||
added,
|
||||
deleted,
|
||||
untracked
|
||||
untracked,
|
||||
staged
|
||||
});
|
||||
} catch (error) {
|
||||
console.error('Git status error:', error);
|
||||
@@ -593,6 +642,64 @@ router.post('/commit', async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// Stage files (git add). Mirrors what the UI shows as the "Staged" section,
|
||||
// so the app's staging state and the real git index never drift apart.
|
||||
router.post('/stage', async (req, res) => {
|
||||
const { project, files } = req.body;
|
||||
|
||||
if (!project || !Array.isArray(files) || files.length === 0) {
|
||||
return res.status(400).json({ error: 'Project id and files are required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const projectPath = await getActualProjectPath(project);
|
||||
await validateGitRepository(projectPath);
|
||||
const repositoryRootPath = await getRepositoryRootPath(projectPath);
|
||||
|
||||
for (const file of files) {
|
||||
const { repositoryRelativeFilePath } = await resolveRepositoryFilePath(projectPath, file);
|
||||
await spawnAsync('git', ['add', '--', repositoryRelativeFilePath], { cwd: repositoryRootPath });
|
||||
}
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Git stage error:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Unstage files (remove from the index, keep the worktree changes)
|
||||
router.post('/unstage', async (req, res) => {
|
||||
const { project, files } = req.body;
|
||||
|
||||
if (!project || !Array.isArray(files) || files.length === 0) {
|
||||
return res.status(400).json({ error: 'Project id and files are required' });
|
||||
}
|
||||
|
||||
try {
|
||||
const projectPath = await getActualProjectPath(project);
|
||||
await validateGitRepository(projectPath);
|
||||
const repositoryRootPath = await getRepositoryRootPath(projectPath);
|
||||
const hasCommits = await repositoryHasCommits(projectPath);
|
||||
|
||||
for (const file of files) {
|
||||
const { repositoryRelativeFilePath } = await resolveRepositoryFilePath(projectPath, file);
|
||||
if (hasCommits) {
|
||||
await spawnAsync('git', ['reset', 'HEAD', '--', repositoryRelativeFilePath], { cwd: repositoryRootPath });
|
||||
} else {
|
||||
// No HEAD to reset against before the first commit; dropping the
|
||||
// index entry is the only way to unstage while keeping the file.
|
||||
await spawnAsync('git', ['rm', '--cached', '-r', '--force', '--', repositoryRelativeFilePath], { cwd: repositoryRootPath });
|
||||
}
|
||||
}
|
||||
|
||||
res.json({ success: true });
|
||||
} catch (error) {
|
||||
console.error('Git unstage error:', error);
|
||||
res.status(500).json({ error: error.message });
|
||||
}
|
||||
});
|
||||
|
||||
// Revert latest local commit (keeps changes staged)
|
||||
router.post('/revert-local-commit', async (req, res) => {
|
||||
const { project } = req.body;
|
||||
|
||||
68
server/routes/git.test.js
Normal file
68
server/routes/git.test.js
Normal file
@@ -0,0 +1,68 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import test from 'node:test';
|
||||
|
||||
import { parseGitStatusOutput } from './git.js';
|
||||
|
||||
// Builds `git status --porcelain=v1 -z` output: NUL-separated entries with a
|
||||
// trailing NUL, exactly as git emits it.
|
||||
const porcelain = (...entries) => entries.join('\0') + '\0';
|
||||
|
||||
test('parseGitStatusOutput buckets files and reports index-side staging', () => {
|
||||
const output = porcelain(
|
||||
'M staged-modified.ts',
|
||||
' M unstaged-modified.ts',
|
||||
'MM staged-and-unstaged.ts',
|
||||
'A staged-new.ts',
|
||||
'D staged-deleted.ts',
|
||||
' D unstaged-deleted.ts',
|
||||
'?? untracked.ts',
|
||||
);
|
||||
|
||||
const result = parseGitStatusOutput(output);
|
||||
|
||||
assert.deepEqual(result.modified, ['staged-modified.ts', 'unstaged-modified.ts', 'staged-and-unstaged.ts']);
|
||||
assert.deepEqual(result.added, ['staged-new.ts']);
|
||||
assert.deepEqual(result.deleted, ['staged-deleted.ts', 'unstaged-deleted.ts']);
|
||||
assert.deepEqual(result.untracked, ['untracked.ts']);
|
||||
// Only index-side (X) changes count as staged.
|
||||
assert.deepEqual(result.staged, [
|
||||
'staged-modified.ts',
|
||||
'staged-and-unstaged.ts',
|
||||
'staged-new.ts',
|
||||
'staged-deleted.ts',
|
||||
]);
|
||||
});
|
||||
|
||||
test('parseGitStatusOutput keeps paths with spaces intact (-z output has no quoting)', () => {
|
||||
const result = parseGitStatusOutput(porcelain('M src/my folder/some file.ts'));
|
||||
assert.deepEqual(result.modified, ['src/my folder/some file.ts']);
|
||||
assert.deepEqual(result.staged, ['src/my folder/some file.ts']);
|
||||
});
|
||||
|
||||
test('parseGitStatusOutput tracks the post-rename path and skips the original', () => {
|
||||
const output = porcelain('R renamed-to.ts', 'renamed-from.ts', ' M other.ts');
|
||||
const result = parseGitStatusOutput(output);
|
||||
|
||||
assert.deepEqual(result.modified, ['renamed-to.ts', 'other.ts']);
|
||||
assert.deepEqual(result.staged, ['renamed-to.ts']);
|
||||
// The pre-rename path is metadata, not a change entry.
|
||||
assert.equal(JSON.stringify(result).includes('renamed-from.ts'), false);
|
||||
});
|
||||
|
||||
test('parseGitStatusOutput never reports merge conflicts as staged', () => {
|
||||
const output = porcelain('UU conflicted.ts', 'AA both-added.ts', 'DD both-deleted.ts');
|
||||
const result = parseGitStatusOutput(output);
|
||||
|
||||
assert.deepEqual(result.modified, ['conflicted.ts', 'both-added.ts', 'both-deleted.ts']);
|
||||
assert.deepEqual(result.staged, []);
|
||||
});
|
||||
|
||||
test('parseGitStatusOutput handles empty output', () => {
|
||||
assert.deepEqual(parseGitStatusOutput(''), {
|
||||
modified: [],
|
||||
added: [],
|
||||
deleted: [],
|
||||
untracked: [],
|
||||
staged: [],
|
||||
});
|
||||
});
|
||||
Reference in New Issue
Block a user