import { execSync } from 'node:child_process'

export function execGit(command: string, cwd: string): string {
  try {
    return execSync(command, {
      cwd,
      encoding: 'utf-8',
      stdio: 'pipe',
    }).trim()
  }
  catch {
    return ''
  }
}

export function isGitRepository(cwd: string): boolean {
  return execGit('git rev-parse --git-dir', cwd).length > 0
}

export function getCurrentBranch(cwd: string): string | null {
  const branch = execGit('git rev-parse --abbrev-ref HEAD', cwd)
  if (!branch || branch === 'HEAD')
    return null
  return branch
}
