|
| 1 | +// Copyright 2013 The Flutter Authors |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'dart:io'; |
| 6 | + |
| 7 | +import 'package:args/args.dart'; |
| 8 | + |
| 9 | +import 'test_utils.dart'; |
| 10 | + |
| 11 | +const String _remoteFlag = 'remote'; |
| 12 | +const String _baseBranchFlag = 'base-branch'; |
| 13 | +const String _headFlag = 'head'; |
| 14 | +const String _helpFlag = 'help'; |
| 15 | + |
| 16 | +void main(List<String> args) { |
| 17 | + final String defaultRemote = detectDefaultRemote(); |
| 18 | + final parser = ArgParser() |
| 19 | + ..addOption( |
| 20 | + _remoteFlag, |
| 21 | + defaultsTo: defaultRemote, |
| 22 | + help: |
| 23 | + 'The git remote to compare against (defaults to detected flutter/packages remote or origin).', |
| 24 | + ) |
| 25 | + ..addOption(_baseBranchFlag, defaultsTo: 'main', help: 'The base branch to compare against.') |
| 26 | + ..addOption(_headFlag, defaultsTo: 'HEAD', help: 'The head commit or reference to compare.') |
| 27 | + ..addFlag(_helpFlag, abbr: 'h', negatable: false, help: 'Prints usage information.'); |
| 28 | + |
| 29 | + final ArgResults argResults; |
| 30 | + try { |
| 31 | + argResults = parser.parse(args); |
| 32 | + } on FormatException catch (e) { |
| 33 | + stderr.writeln(e.message); |
| 34 | + stderr.writeln(parser.usage); |
| 35 | + exitCode = 1; |
| 36 | + return; |
| 37 | + } |
| 38 | + |
| 39 | + if (argResults.wasParsed(_helpFlag)) { |
| 40 | + stdout.writeln('Checks git commit history for evaluation test credentials.\n'); |
| 41 | + stdout.writeln(parser.usage); |
| 42 | + return; |
| 43 | + } |
| 44 | + |
| 45 | + final remote = argResults[_remoteFlag] as String; |
| 46 | + final baseBranch = argResults[_baseBranchFlag] as String; |
| 47 | + final head = argResults[_headFlag] as String; |
| 48 | + |
| 49 | + // Fetch upstream to ensure base branch is available. |
| 50 | + final ProcessResult fetchResult = Process.runSync('git', <String>['fetch', remote, baseBranch]); |
| 51 | + if (fetchResult.exitCode != 0) { |
| 52 | + stderr.writeln('Warning: Failed to fetch $remote/$baseBranch: ${fetchResult.stderr}'); |
| 53 | + } |
| 54 | + |
| 55 | + final ProcessResult result = Process.runSync('git', <String>[ |
| 56 | + 'log', |
| 57 | + '$remote/$baseBranch..$head', |
| 58 | + '--format=%h | Author: %an <%ae> | Committer: %cn <%ce> | %s', |
| 59 | + ]); |
| 60 | + |
| 61 | + if (result.exitCode != 0) { |
| 62 | + stderr.writeln( |
| 63 | + 'SCRIPT_ERROR: Failed to run git log for $remote/$baseBranch..$head:\n${result.stderr}', |
| 64 | + ); |
| 65 | + exitCode = 2; |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + final logOutput = result.stdout.toString(); |
| 70 | + |
| 71 | + final List<String> identities = logOutput |
| 72 | + .split('\n') |
| 73 | + .map((String line) => line.trim()) |
| 74 | + .where((String line) => line.isNotEmpty) |
| 75 | + .toList(); |
| 76 | + |
| 77 | + final forbiddenIdentities = <String>[evalAuthorEmail, evalAuthorName]; |
| 78 | + |
| 79 | + for (final identity in identities) { |
| 80 | + for (final forbiddenIdentity in forbiddenIdentities) { |
| 81 | + if (identity.contains(forbiddenIdentity)) { |
| 82 | + stderr.writeln( |
| 83 | + 'ERROR: Found commit(s) authored or committed by evaluation test credentials:\n' |
| 84 | + ' $identity\n' |
| 85 | + 'Evaluation test commits must not be pushed. Clean or rebase your branch before pushing.', |
| 86 | + ); |
| 87 | + exitCode = 1; |
| 88 | + return; |
| 89 | + } |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + stdout.writeln('SUCCESS: No evaluation test commits detected.'); |
| 94 | +} |
0 commit comments