isCurrentDirectoryBareGitRepo function

bool isCurrentDirectoryBareGitRepo({
  1. String? cwd,
})

Check if cwd looks like a bare git repo (security check).

Implementation

bool isCurrentDirectoryBareGitRepo({String? cwd}) {
  final currentCwd = cwd ?? Directory.current.path;
  final gitPath = p.join(currentCwd, '.git');

  try {
    final stat = FileStat.statSync(gitPath);
    if (stat.type == FileSystemEntityType.file) return false;
    if (stat.type == FileSystemEntityType.directory) {
      try {
        final headStat = FileStat.statSync(p.join(gitPath, 'HEAD'));
        if (headStat.type == FileSystemEntityType.file) return false;
      } catch (_) {}
    }
  } catch (_) {}

  // Check bare repo indicators
  for (final item in [
    [p.join(currentCwd, 'HEAD'), FileSystemEntityType.file],
    [p.join(currentCwd, 'objects'), FileSystemEntityType.directory],
    [p.join(currentCwd, 'refs'), FileSystemEntityType.directory],
  ]) {
    try {
      final stat = FileStat.statSync(item[0] as String);
      if (stat.type == item[1]) return true;
    } catch (_) {}
  }

  return false;
}