fromExisting static method

Future<GitDir> fromExisting(
  1. String gitDirRoot, {
  2. bool allowSubdirectory = false,
})

If allowSubdirectory is true, a GitDir may be returned if gitDirRoot is a subdirectory within a Git repository.

Implementation

static Future<GitDir> fromExisting(
  String gitDirRoot, {
  bool allowSubdirectory = false,
}) async {
  final path = p.absolute(gitDirRoot);

  final pr = await runGit(
    ['rev-parse', '--git-dir'],
    processWorkingDir: path.toString(),
  );

  var returnedPath = (pr.stdout as String).trim();

  if (returnedPath == '.git') {
    return GitDir._raw(path);
  }

  if (allowSubdirectory && p.basename(returnedPath) == '.git') {
    returnedPath = p.dirname(returnedPath);

    if (p.isWithin(returnedPath, path)) {
      return GitDir._raw(returnedPath);
    }
  }

  throw ArgumentError('The provided value "$gitDirRoot" is not '
      'the root of a git directory');
}