gitOpenDirectory function

GitRepository gitOpenDirectory(
  1. String path, {
  2. bool create = false,
  3. String defaultBranch = 'main',
})

Opens the bare repository at path, creating it (with defaultBranch as HEAD) when create is set and nothing is there.

Implementation

GitRepository gitOpenDirectory(
  String path, {
  bool create = false,
  String defaultBranch = 'main',
}) {
  final dir = Directory(path);
  final head = File(p.join(path, 'HEAD'));
  if (!head.existsSync()) {
    if (!create) {
      throw FileSystemException('Not a git repository', path);
    }
    for (final sub in [
      'objects/pack',
      'objects/info',
      'refs/heads',
      'refs/tags',
    ]) {
      Directory(p.join(path, sub)).createSync(recursive: true);
    }
    File(p.join(path, 'config')).writeAsStringSync(
      '[core]\n\trepositoryformatversion = 0\n\tfilemode = true\n'
      '\tbare = true\n',
    );
    head.writeAsStringSync('ref: refs/heads/$defaultBranch\n');
  }
  return GitRepository(
    objects: GitDirectoryObjectStore(dir.path),
    refs: GitDirectoryRefStore(dir.path),
  );
}