remotes method

Future<List<GitRemote>> remotes({
  1. String? workDir,
})

Lists configured remotes.

Implementation

Future<List<GitRemote>> remotes({String? workDir}) async {
  final result = await _runGit([
    'remote',
    '-v',
  ], workDir: workDir ?? defaultWorkDir);
  final output = (result.stdout as String).trim();
  if (output.isEmpty) return [];

  final map = <String, _RemoteUrls>{};

  for (final line in LineSplitter.split(output)) {
    final parts = line.split(RegExp(r'\s+'));
    if (parts.length < 3) continue;
    final name = parts[0];
    final url = parts[1];
    final type = parts[2]; // (fetch) or (push)

    map.putIfAbsent(name, () => _RemoteUrls());
    if (type.contains('fetch')) {
      map[name]!.fetchUrl = url;
    } else {
      map[name]!.pushUrl = url;
    }
  }

  return map.entries
      .map(
        (e) => GitRemote(
          name: e.key,
          fetchUrl: e.value.fetchUrl ?? '',
          pushUrl: e.value.pushUrl ?? e.value.fetchUrl ?? '',
        ),
      )
      .toList();
}