getCommitShasForFile method

Future<List<String>> getCommitShasForFile({
  1. required String gitDirpath,
  2. required String relativeFilepath,
  3. bool followName = true,
  4. Map<String, String>? environment,
  5. bool includeParentEnvironment = true,
})

Get a chronologically sorted list of all commits from the file at relativeFilepath in the git repository at gitDirpath.

The first element in the list is the most recent commit.

Implementation

Future<List<String>> getCommitShasForFile({
  required String gitDirpath,
  required String relativeFilepath,
  bool followName = true,
  Map<String, String>? environment,
  bool includeParentEnvironment = true,
}) async {
  final dir = checkDirectoryExists(gitDirpath, "gitDirpath");
  final file = checkFileExists(lib_path.join(dir.path, relativeFilepath));

  final res = await runAsync(
    [
      "log",
      "--follow",
      "--format=%H",
      "--",
      lib_path.relative(file.path, from: dir.path),
    ],
    workingDirectory: dir.path,
    environment: environment,
    includeParentEnvironment: includeParentEnvironment,
  );

  if (0 != res.exitCode) {
    throw CliResultException(
      exitCode: res.exitCode,
      stderr: res.stderr,
      message: "Failed to get the commit shas from "
          "the git directory at '$gitDirpath'",
    );
  }

  return res.stdout
      .toString()
      .split("\n")
      .where((e) => e.isNotEmpty)
      .toList();
}