symlinkDirectories method

Future<void> symlinkDirectories(
  1. String repoRootPath,
  2. String worktreePath,
  3. List<String> dirsToSymlink
)

Symlinks directories from the main repository to avoid duplication.

Implementation

Future<void> symlinkDirectories(
  String repoRootPath,
  String worktreePath,
  List<String> dirsToSymlink,
) async {
  for (final dir in dirsToSymlink) {
    if (dir.contains('..')) {
      _logForDebugging(
        'Skipping symlink for "$dir": path traversal detected',
        level: 'warn',
      );
      continue;
    }

    final sourcePath = '$repoRootPath/$dir';
    final destPath = '$worktreePath/$dir';

    try {
      await Link(destPath).create(sourcePath);
      _logForDebugging(
        'Symlinked $dir from main repository to worktree to avoid disk bloat',
      );
    } on FileSystemException catch (e) {
      // ENOENT: source doesn't exist yet; EEXIST: destination already exists
      if (!e.message.contains('No such file') &&
          !e.message.contains('File exists')) {
        _logForDebugging(
          'Failed to symlink $dir: ${e.message}',
          level: 'warn',
        );
      }
    }
  }
}