getDefaultBranch function
Get the default branch (main/master/etc).
Implementation
Future<String> getDefaultBranch({String? cwd}) async {
// Try symbolic-ref first
final result = await _runGit([
'symbolic-ref',
'refs/remotes/origin/HEAD',
], cwd: cwd);
if (result.code == 0 && result.stdout.trim().isNotEmpty) {
return result.stdout.trim().replaceFirst('refs/remotes/origin/', '');
}
// Fallback: check common branch names
for (final branch in ['main', 'master']) {
final check = await _runGit([
'rev-parse',
'--verify',
'origin/$branch',
], cwd: cwd);
if (check.code == 0) return branch;
}
return 'main';
}