getGitDiffSize method

Future<int> getGitDiffSize(
  1. String filePath
)

Get the size of changes for a file from git diff.

Implementation

Future<int> getGitDiffSize(String filePath) async {
  final cwd = getAttributionRepoRoot();

  try {
    final result = await Process.run(gitExe, [
      'diff',
      '--cached',
      '--stat',
      '--',
      filePath,
    ], workingDirectory: cwd);

    if (result.exitCode != 0 || (result.stdout as String).isEmpty) {
      return 0;
    }

    final lines = (result.stdout as String)
        .split('\n')
        .where((l) => l.isNotEmpty);
    var totalChanges = 0;

    for (final line in lines) {
      if (line.contains('file changed') || line.contains('files changed')) {
        final insertMatch = RegExp(r'(\d+) insertions?').firstMatch(line);
        final deleteMatch = RegExp(r'(\d+) deletions?').firstMatch(line);

        final insertions = int.tryParse(insertMatch?.group(1) ?? '') ?? 0;
        final deletions = int.tryParse(deleteMatch?.group(1) ?? '') ?? 0;
        totalChanges += (insertions + deletions) * 40;
      }
    }

    return totalChanges;
  } catch (_) {
    return 0;
  }
}