worktreeExists static method

Future<bool> worktreeExists(
  1. String gitRoot,
  2. String worktreePath
)

Checks if a worktree exists at the specified path

Implementation

static Future<bool> worktreeExists(String gitRoot, String worktreePath) async {
  try {
    final result = await Process.run(
      'git',
      ['worktree', 'list'],
      workingDirectory: gitRoot,
    );
    if (result.exitCode != 0) {
      return false;
    }
    final worktrees = result.stdout.toString().trim().split('\n');
    // Check if any worktree path matches (handle both absolute and relative paths)
    final normalizedPath = Directory(worktreePath).absolute.path;
    return worktrees.any((line) {
      final parts = line.trim().split(RegExp(r'\s+'));
      if (parts.isEmpty) return false;
      final existingPath = Directory(parts[0]).absolute.path;
      return existingPath == normalizedPath;
    });
  } catch (e) {
    logger.detail('Error checking worktree existence: $e');
    return false;
  }
}