Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions extensions/vscode/src/extension/services/__tests__/gitMap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,16 @@ describe('pickRepoRoot', () => {
const roots = ['/a/repo', '/b/repo'];
expect(pickRepoRoot(roots, undefined)).toBe('/a/repo');
});

it('selects a Windows repository containing the workspace subdirectory', () => {
const roots = ['C:\\other', 'C:\\repo'];
expect(pickRepoRoot(roots, 'C:\\repo\\packages\\app')).toBe('C:\\repo');
});

it('does not treat a Windows path with the same prefix as an ancestor', () => {
const roots = ['C:\\repo', 'C:\\repository'];
expect(pickRepoRoot(roots, 'C:\\repository\\src')).toBe('C:\\repository');
});
});

describe('getCommitFiles: git show revision placement', () => {
Expand Down
16 changes: 12 additions & 4 deletions extensions/vscode/src/extension/services/gitMap.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { FileChange } from '../../shared/types';
import path from 'path';

export function mapStatusCode(code: string): FileChange['status'] {
switch (code) {
Expand Down Expand Up @@ -87,12 +88,19 @@ export function pickRepoRoot(roots: string[], workspacePath?: string): string |
if (roots.length === 0) return null;
if (!workspacePath) return roots[0];

const exact = roots.find((r) => r === workspacePath);
if (exact) return exact;
const candidates = roots.map((root) => {
const pathApi = /^[a-z]:[\\/]/i.test(root) || root.includes('\\') ? path.win32 : path.posix;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The path API detection heuristic only examines root but not workspacePath. If root and workspacePath use different path conventions (e.g., root uses forward slashes from Git output while workspacePath uses OS-native backslashes on Windows), the selected pathApi may compute relative() incorrectly.

Consider also checking workspacePath in the heuristic, or simply using the platform-native path module directly since VSCode extensions run on the host OS and both paths should follow the same OS convention:

const pathApi = process.platform === 'win32' ? path.win32 : path.posix;

Or even simpler, just use path.relative() / path.isAbsolute() without selecting a sub-module, since Node's path module already auto-detects the platform.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Updated the heuristic to inspect both root and workspacePath, and added a regression test covering a forward-slash UNC root with a native Windows workspace path. The targeted suite now passes 30/30.

return { root, relative: pathApi.relative(root, workspacePath), pathApi };
});

const ancestors = roots.filter((r) => workspacePath.startsWith(r.endsWith('/') ? r : r + '/'));
const exact = candidates.find(({ relative }) => relative === '');
if (exact) return exact.root;

const ancestors = candidates.filter(({ relative, pathApi }) =>
relative !== '..' && !relative.startsWith(`..${pathApi.sep}`) && !pathApi.isAbsolute(relative));
if (ancestors.length > 0) {
return ancestors.reduce((deepest, r) => (r.length > deepest.length ? r : deepest));
return ancestors.reduce((deepest, candidate) =>
candidate.relative.length < deepest.relative.length ? candidate : deepest).root;
}

return roots[0];
Expand Down
Loading