getCurrentBranch static method
Gets the current Git branch name Returns null if in detached HEAD state or throws GitException if there's an error.
Implementation
static Future<String?> getCurrentBranch(String? root) async {
try {
final result = await Process.run('git', ['branch', '--show-current'], workingDirectory: root);
if (result.exitCode != 0) {
// In detached HEAD state, this command returns empty output
final output = result.stdout.toString().trim();
if (output.isEmpty) {
return null;
}
throw const GitException('Failed to get current branch');
}
final output = result.stdout.toString().trim();
return output.isEmpty ? null : output;
} on ProcessException catch (e) {
throw GitException('Git command not found: ${e.message}');
} catch (e) {
throw GitException('Unexpected error: $e');
}
}