flutter/dev/tools/bin/get_files_changed.dart
Matan Lurey cc9c13e009
Experiment with a files-changed.json per PR (#161788)
This is an experiment to determine what the wall time of a (minimal)
"upload a JSON file describing files changed".

We are looking for this to take (low) single digit minutes, because if
we use this file (and expand it to become a build plan), it will block
almost _all_ other actions, and will likely get longer as we add more
complex logic and checks. Here's hoping!
2025-01-22 20:22:30 +00:00

58 lines
1.6 KiB
Dart

// Copyright 2014 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'dart:convert';
import 'dart:io' as io;
import 'package:args/args.dart';
final ArgParser _argParser =
ArgParser()
..addOption(
'since',
help: 'What previous SHA to compare the current git state to.',
defaultsTo: 'HEAD^',
)
..addOption(
'output',
help: 'What format to output in.',
defaultsTo: io.stdout.hasTerminal ? 'text' : 'json',
allowed: <String>['text', 'json'],
);
void main(List<String> args) async {
final ArgResults argResults = _argParser.parse(args);
// Get a list of files changed between this commit and the base SHA.
final List<String> filesChanged;
{
final List<String> args = <String>[
'diff',
'--name-only',
'--full-index',
argResults.option('since')!,
];
final io.ProcessResult git = await io.Process.run('git', args);
if (git.exitCode != 0) {
io.stderr.writeln('$args failed (exit code: ${git.exitCode}):');
io.stderr.writeln(git.stdout);
io.stderr.writeln(git.stderr);
io.exitCode = 1;
return;
}
final String stdout = git.stdout as String;
filesChanged = const LineSplitter().convert(stdout);
}
// Output to stdout.
final _Output output = _Output.values.byName(argResults.option('output')!);
io.stdout.writeln(switch (output) {
_Output.text => filesChanged.join('\n'),
_Output.json => jsonEncode(filesChanged),
});
}
enum _Output { text, json }