diffFiles method

Future<List<String>?> diffFiles({
  1. List<String> diffArgs = const [],
  2. String? diffFilters,
})

Implementation

Future<List<String>?> diffFiles({
  List<String> diffArgs = const [],
  String? diffFilters,
}) async {
  final filters = switch (diffFilters) {
    final String filters => filters,
    _ => 'ACMR',
  };

  final diff = switch (diffArgs) {
    _ when diffArgs.isNotEmpty => diffArgs,
    _ => ['--staged'],
  };
  final result = await Process.run('git', [
    'diff',
    'HEAD',
    '--name-only',
    ...diff,
    '--diff-filter=$filters',
  ]);

  final out = switch (result.stdout) {
    final String files => files.trim(),
    _ => null,
  };

  if (out == null) {
    logger
      ..err('Failed to get changed files')
      ..detail('Error: ${result.stderr}');
    return null;
  }

  final files =
      out.split('\n').where((element) => element.isNotEmpty).toList();

  return files;
}