Iterable? getDiff()

in packages/code_excerpt_updater/lib/src/differ.dart [24:91]


  Iterable<String>? getDiff(String relativeSrcPath1, String region,
      Map<String, String?> args, String pathPrefix) {
    final relativeSrcPath2 = args['diff-with'] ?? '';
    final useCompleteFiles = region.isEmpty && args['remove'] == null;
    final path1 = useCompleteFiles
        ? filteredFile(p.join(pathPrefix, relativeSrcPath1))
        : _writeExcerpt(relativeSrcPath1, region, args);
    final path2 = useCompleteFiles
        ? filteredFile(p.join(pathPrefix, relativeSrcPath2))
        : _writeExcerpt(relativeSrcPath2, region, args);

    final diffUArg = args['diff-u'];
    final diffArgs = diffUArg == null ? ['-u'] : ['-U', diffUArg];
    diffArgs.add(path1.path);
    diffArgs.add(path2.path);
    final r = Process.runSync('diff', diffArgs);

    try {
      path1.deleteSync();
      path2.deleteSync();
    } on FileSystemException catch (e) {
      _log.info(
          'Ignored exception while attempting to delete temporary files: $e');
    }

    if (r.exitCode > 1) {
      _reportError(r.stderr as String);
      return null;
    }
    if ((r.stdout as String).isEmpty) return []; // no differences between files

    /* Sample diff output:
    --- examples/acx/lottery/1-base/lib/lottery_simulator.html	2017-08-25 07:45:24.000000000 -0400
    +++ examples/acx/lottery/2-starteasy/lib/lottery_simulator.html	2017-08-25 07:45:24.000000000 -0400
    @@ -23,35 +23,39 @@
         <div class="clear-floats"></div>
       </div>

    -  Progress: <strong>{{progress}}%</strong> <br>
    -  <progress max="100" [value]="progress"></progress>
    +  <material-progress  [activeProgress]="progress" class="life-progress">
    +  </material-progress>

       <div class="controls">
    ...
    */

    var diffText = (r.stdout as String).trim();
    final fromArg = args['from'];
    final toArg = args['to'];
    if (fromArg != null || toArg != null) {
      final from = fromArg == null ? null : patternArgToMatcher(fromArg);
      final to = toArg == null ? null : patternArgToMatcher(toArg);
      final diff = Diff(diffText);
      if (diff.keepLines(from: from, to: to)) diffText = diff.toString();
    }
    final result = diffText.split(eol);

    // Fix file id lines by removing:
    // - [pathPrefix] from the start of the file path so that paths are relative
    // - timestamp (because file timestamps are not relevant in the git world)
    result[0] = _adjustDiffFileIdLine(
        relativeSrcPath1 + (region.isEmpty ? '' : ' ($region)'), result[0]);
    result[1] = _adjustDiffFileIdLine(
        relativeSrcPath2 + (region.isEmpty ? '' : ' ($region)'), result[1]);
    _log.fine('>> diff result:\n${result.join("\n")}');
    return result;
  }