getGitHash static method

String? getGitHash(
  1. [String? dirPath]
)

Gets the git hash from dirPath, if it's a repo, otherwise null.

Implementation

static String? getGitHash([String? dirPath]) {
  final gitPath =
      path.normalize(path.join(dirPath ?? Directory.current.path, '.git'));
  final isGitPath = Directory(gitPath).existsSync();
  final hasGitTool = Utils.isCommand('git');
  if (!isGitPath || !hasGitTool) return null;

  final result = Process.runSync('git', ['rev-parse', '--short', 'HEAD'],
      workingDirectory: dirPath);
  if (result.exitCode != 0) {
    throw FileSystemException(result.stderr.toString(), dirPath);
  }

  return result.stdout.toString().trim();
}