Skip to content

Commit

Permalink
feat(shorebird_cli): add listBranches to Git
Browse files Browse the repository at this point in the history
  • Loading branch information
felangel committed Aug 8, 2023
1 parent fb5bcac commit b296d86
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
23 changes: 23 additions & 0 deletions packages/shorebird_cli/lib/src/git.dart
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@ class Git {
}
}

/// List all branches in the git repository located at [directory] that match
/// the [pattern].
Future<String> listBranches({
required String directory,
required String pattern,
}) async {
final arguments = ['branch', '--all', '--list', pattern];
final result = await process.run(
executable,
arguments,
workingDirectory: directory,
);
if (result.exitCode != 0) {
throw ProcessException(
executable,
arguments,
'${result.stderr}',
result.exitCode,
);
}
return '${result.stdout}'.trim();
}

/// Prunes stale remote branches from the repository at [directory]
/// associated with [name].
Future<void> remotePrune({
Expand Down
39 changes: 39 additions & 0 deletions packages/shorebird_cli/test/src/git_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,45 @@ void main() {
});
});

group('listBranches', () {
const directory = 'repository';
const pattern = 'pattern';
const output = '''
* main
stable
remotes/origin/HEAD -> origin/main''';
test('executes correct command', () async {
when(() => processResult.stdout).thenReturn(output);
await expectLater(
runWithOverrides(
() => git.listBranches(pattern: pattern, directory: directory),
),
completion(equals(output)),
);
verify(
() => process.run(
'git',
['branch', '--all', '--list', pattern],
workingDirectory: directory,
),
).called(1);
});

test('throws ProcessException if process exits with error', () async {
const error = 'oops';
when(() => processResult.exitCode).thenReturn(ExitCode.software.code);
when(() => processResult.stderr).thenReturn(error);
expect(
() => runWithOverrides(
() => git.listBranches(pattern: pattern, directory: directory),
),
throwsA(
isA<ProcessException>().having((e) => e.message, 'message', error),
),
);
});
});

group('remotePrune', () {
const directory = './output';
const name = 'origin';
Expand Down

0 comments on commit b296d86

Please sign in to comment.