Fix "git pull" listing changed files

This commit is contained in:
Alex Gleason committed 2025-09-30 12:47:21 -05:00
1 parent b7e94ff67c
commit df39f78d20
2 files changed
+48 -5

No files matched your search

+39
View File
@@ -81,4 +81,43 @@ describe('GitPullCommand', () => {
});
});
});
describe('execute', () => {
it('should list modified files when there are uncommitted changes', async () => {
// Mock statusMatrix to return some modified files
const mockGitWithChanges = {
...mockGit,
statusMatrix: async () => [
['src/index.ts', 1, 2, 1], // Modified file
['README.md', 1, 2, 2], // Modified and staged
['package.json', 0, 2, 0], // Untracked file
],
} as unknown as Git;
// Mock FS to ensure .git directory exists
const mockFSWithGit = {
stat: async (path: string) => {
if (path.endsWith('/.git')) {
return { isDirectory: () => true };
}
throw new Error('File not found');
},
} as unknown as JSRuntimeFS;
const commandWithChanges = new GitPullCommand({
git: mockGitWithChanges,
fs: mockFSWithGit,
pwd: '/test/repo',
});
const result = await commandWithChanges.execute(['origin', 'main']);
expect(result.exitCode).toBe(1);
expect(result.stderr).toContain('error: Your local changes to the following files would be overwritten by merge:');
expect(result.stderr).toContain('\tsrc/index.ts');
expect(result.stderr).toContain('\tREADME.md');
expect(result.stderr).toContain('\tpackage.json');
expect(result.stderr).toContain('Please commit your changes or stash them before you merge.');
});
});
});
+9 -5
View File
@@ -69,12 +69,16 @@ export class GitPullCommand implements GitSubcommand {
dir: this.pwd,
});
const hasChanges = statusMatrix.some(([, headStatus, workdirStatus, stageStatus]) => {
return headStatus !== workdirStatus || headStatus !== stageStatus;
});
const modifiedFiles = statusMatrix
.filter(([, headStatus, workdirStatus, stageStatus]) => {
// Check for any uncommitted changes (modified, staged, or untracked)
return headStatus !== workdirStatus || headStatus !== stageStatus;
})
.map(([filepath]) => filepath);
if (hasChanges) {
return createErrorResult('error: Your local changes to the following files would be overwritten by merge:\nPlease commit your changes or stash them before you merge.');
if (modifiedFiles.length > 0) {
const fileList = modifiedFiles.map(file => `\t${file}`).join('\n');
return createErrorResult(`error: Your local changes to the following files would be overwritten by merge:\n${fileList}\nPlease commit your changes or stash them before you merge.`);
}
try {