The Git integration was failing with the following error when initializing a repository:
ENOENT: no such file or directory, lstat '.'
This error occurred because the Git service was not properly handling relative paths like . (current directory) when performing file system operations.
-
Working Directory Path: The Git service was initialized with
/workspaceas the working directory, but the actual file system was using a different root. -
Path Handling in
lstat: Thelstatmethod in theGitFileSystemAdapterhad special case handling for.and..paths, but it wasn't working correctly in all scenarios. -
Path Inconsistency: In the
createInitialCommitmethod, there was an inconsistency between the path used for writing the.gitkeepfile (/.gitkeep) and the path used for adding it to Git (.gitkeep). -
Add All Files Method: The
addAllFilesmethod was using the problematic.path directly, which was causing the error.
-
Updated Working Directory: Changed the Git service initialization to use
/as the working directory:export const gitService = new GitService('/');
-
Enhanced
lstatMethod: Improved the special case handling for.and..paths in thelstatmethod to ensure it always returns a valid directory stat object:if (filepath === '.' || filepath === '..') { debug(`lstat called for special directory ('${filepath}')`); // Always return a valid directory stat object for special directories // This ensures Git operations that use '.' work correctly return { isFile: () => false, isDirectory: () => true, isSymbolicLink: () => false, size: 0, mtime: new Date(), ctime: new Date(), mode: 16877, // 0o040755 for directories }; }
-
Fixed Path Inconsistency: Updated the
createInitialCommitmethod to use consistent paths:const gitkeepPath = `${this.dir === '/' ? '' : this.dir}/.gitkeep`; await this.fs.writeFile(gitkeepPath, '', { encoding: 'utf8' });
-
Improved
addAllFilesMethod: Rewrote theaddAllFilesmethod to avoid using the problematic.path:addAllFiles: async () => { try { // Use a more robust approach to add all files // First get the status to find all unstaged files const status = await gitService.status(); // If there are no files to add, just return if (status.length === 0) { info('No files to add to staging area'); return; } // Add each file individually instead of using '.' for (const item of status) { // Only add files that are not already staged if (item.stage !== 2) { await gitService.add(item.file); } } await get().getStatus(); info('All files added to staging area'); } catch (err) { const errorMessage = err instanceof Error ? err.message : 'Failed to add all files'; error('Failed to add all files:', errorMessage); set((state) => { state.error = errorMessage; }); } }
A new test script (test_git_add_all.js) has been created to verify that the addAllFiles method now works correctly. This script:
- Initializes a Git repository
- Creates a test file
- Calls the
addAllFilesmethod (which was previously failing) - Verifies that the file was added to the staging area
- Commits the changes
- Verifies that the commit was created successfully
To verify that the fix works:
- Run the application
- Initialize a Git repository
- Create some files
- Use the "Add All Files" functionality
- Verify that the files are added to the staging area without errors
Alternatively, you can run the test_git_add_all.js script to automatically verify the fix.
The changes made not only fix the specific error but also improve the robustness of the Git integration:
- The
addAllFilesmethod now handles each file individually, which is more reliable than using the.path. - The path handling in the
createInitialCommitmethod is now more consistent. - The
lstatmethod now handles special directories more robustly.
These improvements should make the Git integration more reliable and less prone to errors in the future.