From 6220641bde01f1ca13df690916f0742fbd1570fe Mon Sep 17 00:00:00 2001 From: Zachary Anderson Date: Tue, 5 Mar 2024 18:23:34 +0000 Subject: [PATCH] Adds githooks that reminds to run `gclient sync -D` (flutter/engine#51156) This PR adds githooks for `post-checkout`, `post-merge`, `pre-rebase` that remind to run `gclient sync -D`. This is probably going to print the reminder too much. The `pre-rebase` hook runs before a `git pull --rebase` that is actually going to update something, but the other hooks may be needed to cover other workflows. The printed message will also include the hook that it comes from, so we can remove the message from hooks where it doesn't make sense. Screenshot 2024-03-04 at 18 36 15 --- .../flutter/tools/githooks/lib/githooks.dart | 8 +++- .../tools/githooks/lib/src/messages.dart | 19 ++++++++ .../lib/src/post_checkout_command.dart | 22 +++++++++ .../githooks/lib/src/post_merge_command.dart | 22 +++++++++ .../githooks/lib/src/pre_rebase_command.dart | 23 +++++++++ .../src/flutter/tools/githooks/post-checkout | 35 ++++++++++++++ engine/src/flutter/tools/githooks/post-merge | 35 ++++++++++++++ engine/src/flutter/tools/githooks/pre-push | 4 +- engine/src/flutter/tools/githooks/pre-rebase | 35 ++++++++++++++ .../tools/githooks/test/githooks_test.dart | 48 +++++++++++++++++++ 10 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 engine/src/flutter/tools/githooks/lib/src/messages.dart create mode 100644 engine/src/flutter/tools/githooks/lib/src/post_checkout_command.dart create mode 100644 engine/src/flutter/tools/githooks/lib/src/post_merge_command.dart create mode 100644 engine/src/flutter/tools/githooks/lib/src/pre_rebase_command.dart create mode 100755 engine/src/flutter/tools/githooks/post-checkout create mode 100755 engine/src/flutter/tools/githooks/post-merge create mode 100755 engine/src/flutter/tools/githooks/pre-rebase diff --git a/engine/src/flutter/tools/githooks/lib/githooks.dart b/engine/src/flutter/tools/githooks/lib/githooks.dart index 14c3d4349f..0a44e2ab33 100644 --- a/engine/src/flutter/tools/githooks/lib/githooks.dart +++ b/engine/src/flutter/tools/githooks/lib/githooks.dart @@ -7,7 +7,10 @@ import 'dart:io' as io; import 'package:args/args.dart'; import 'package:args/command_runner.dart'; +import 'src/post_checkout_command.dart'; +import 'src/post_merge_command.dart'; import 'src/pre_push_command.dart'; +import 'src/pre_rebase_command.dart'; /// Runs the githooks Future run(List args) async { @@ -15,7 +18,10 @@ Future run(List args) async { 'githooks', 'Githooks implementation for the flutter/engine repo.', ) - ..addCommand(PrePushCommand()); + ..addCommand(PostCheckoutCommand()) + ..addCommand(PostMergeCommand()) + ..addCommand(PrePushCommand()) + ..addCommand(PreRebaseCommand()); // Add top-level arguments. runner.argParser diff --git a/engine/src/flutter/tools/githooks/lib/src/messages.dart b/engine/src/flutter/tools/githooks/lib/src/messages.dart new file mode 100644 index 0000000000..d524404cf5 --- /dev/null +++ b/engine/src/flutter/tools/githooks/lib/src/messages.dart @@ -0,0 +1,19 @@ +// Copyright 2013 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:io' as io; + +const String _redBoldUnderline = '\x1B[31;1;4m'; +const String _reset = '\x1B[0m'; + +/// Prints a reminder to stdout to run `gclient sync -D`. Uses colors when +/// stdout supports ANSI escape codes. +void printGclientSyncReminder(String command) { + final String prefix = io.stdout.supportsAnsiEscapes ? _redBoldUnderline : ''; + final String postfix = io.stdout.supportsAnsiEscapes ? _reset : ''; + io.stderr.writeln('$command: The engine source tree has been updated.'); + io.stderr.writeln( + '\n${prefix}You man need to run "gclient sync -D"$postfix\n', + ); +} diff --git a/engine/src/flutter/tools/githooks/lib/src/post_checkout_command.dart b/engine/src/flutter/tools/githooks/lib/src/post_checkout_command.dart new file mode 100644 index 0000000000..2be39d15e2 --- /dev/null +++ b/engine/src/flutter/tools/githooks/lib/src/post_checkout_command.dart @@ -0,0 +1,22 @@ +// Copyright 2013 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 'package:args/command_runner.dart'; + +import 'messages.dart'; + +/// The command that implements the post-checkout githook +class PostCheckoutCommand extends Command { + @override + final String name = 'post-checkout'; + + @override + final String description = 'Checks that run after the worktree is updated'; + + @override + Future run() async { + printGclientSyncReminder(name); + return true; + } +} diff --git a/engine/src/flutter/tools/githooks/lib/src/post_merge_command.dart b/engine/src/flutter/tools/githooks/lib/src/post_merge_command.dart new file mode 100644 index 0000000000..3ce259fda1 --- /dev/null +++ b/engine/src/flutter/tools/githooks/lib/src/post_merge_command.dart @@ -0,0 +1,22 @@ +// Copyright 2013 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 'package:args/command_runner.dart'; + +import 'messages.dart'; + +/// The command that implements the post-merge githook +class PostMergeCommand extends Command { + @override + final String name = 'post-merge'; + + @override + final String description = 'Checks to run after a "git merge"'; + + @override + Future run() async { + printGclientSyncReminder(name); + return true; + } +} diff --git a/engine/src/flutter/tools/githooks/lib/src/pre_rebase_command.dart b/engine/src/flutter/tools/githooks/lib/src/pre_rebase_command.dart new file mode 100644 index 0000000000..3e81670888 --- /dev/null +++ b/engine/src/flutter/tools/githooks/lib/src/pre_rebase_command.dart @@ -0,0 +1,23 @@ +// Copyright 2013 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 'package:args/command_runner.dart'; + +import 'messages.dart'; + +/// The command that implements the pre-rebase githook +class PreRebaseCommand extends Command { + @override + final String name = 'pre-rebase'; + + @override + final String description = 'Checks to run before a "git rebase"'; + + @override + Future run() async { + printGclientSyncReminder(name); + // Returning false here will block the rebase. + return true; + } +} diff --git a/engine/src/flutter/tools/githooks/post-checkout b/engine/src/flutter/tools/githooks/post-checkout new file mode 100755 index 0000000000..123debdc28 --- /dev/null +++ b/engine/src/flutter/tools/githooks/post-checkout @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# Copyright 2013 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. + +''' +Runs the post-checkout githooks. +''' + +import os +import subprocess +import sys + +SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +FLUTTER_DIR = os.path.join(SRC_ROOT, 'flutter') +DART_BIN = os.path.join(SRC_ROOT, 'third_party', 'dart', 'tools', 'sdks', 'dart-sdk', 'bin') + +def Main(argv): + githook_args = [ + '--flutter', + FLUTTER_DIR, + ] + + result = subprocess.run([ + os.path.join(DART_BIN, 'dart'), + '--disable-dart-dev', + os.path.join(FLUTTER_DIR, 'tools', 'githooks', 'bin', 'main.dart'), + ] + githook_args + [ + 'post-checkout', + ] + argv[1:], cwd=SRC_ROOT) + return result.returncode + + +if __name__ == '__main__': + sys.exit(Main(sys.argv)) diff --git a/engine/src/flutter/tools/githooks/post-merge b/engine/src/flutter/tools/githooks/post-merge new file mode 100755 index 0000000000..18beeef645 --- /dev/null +++ b/engine/src/flutter/tools/githooks/post-merge @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# Copyright 2013 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. + +''' +Runs the post-merge githooks. +''' + +import os +import subprocess +import sys + +SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +FLUTTER_DIR = os.path.join(SRC_ROOT, 'flutter') +DART_BIN = os.path.join(SRC_ROOT, 'third_party', 'dart', 'tools', 'sdks', 'dart-sdk', 'bin') + +def Main(argv): + githook_args = [ + '--flutter', + FLUTTER_DIR, + ] + + result = subprocess.run([ + os.path.join(DART_BIN, 'dart'), + '--disable-dart-dev', + os.path.join(FLUTTER_DIR, 'tools', 'githooks', 'bin', 'main.dart'), + ] + githook_args + [ + 'post-merge', + ] + argv[1:], cwd=SRC_ROOT) + return result.returncode + + +if __name__ == '__main__': + sys.exit(Main(sys.argv)) diff --git a/engine/src/flutter/tools/githooks/pre-push b/engine/src/flutter/tools/githooks/pre-push index 0f629a08c8..097730aae6 100755 --- a/engine/src/flutter/tools/githooks/pre-push +++ b/engine/src/flutter/tools/githooks/pre-push @@ -1,4 +1,4 @@ -#!/usr/bin/env vpython3 +#!/usr/bin/env python3 # Copyright 2013 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. @@ -34,7 +34,7 @@ def Main(argv): os.path.join(FLUTTER_DIR, 'tools', 'githooks', 'bin', 'main.dart'), ] + githook_args + [ 'pre-push', - ], cwd=SRC_ROOT) + ] + argv[1:], cwd=SRC_ROOT) return result.returncode diff --git a/engine/src/flutter/tools/githooks/pre-rebase b/engine/src/flutter/tools/githooks/pre-rebase new file mode 100755 index 0000000000..afaf9423e9 --- /dev/null +++ b/engine/src/flutter/tools/githooks/pre-rebase @@ -0,0 +1,35 @@ +#!/usr/bin/env python3 +# Copyright 2013 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. + +''' +Runs the pre-rebase githooks. +''' + +import os +import subprocess +import sys + +SRC_ROOT = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))) +FLUTTER_DIR = os.path.join(SRC_ROOT, 'flutter') +DART_BIN = os.path.join(SRC_ROOT, 'third_party', 'dart', 'tools', 'sdks', 'dart-sdk', 'bin') + +def Main(argv): + githook_args = [ + '--flutter', + FLUTTER_DIR, + ] + + result = subprocess.run([ + os.path.join(DART_BIN, 'dart'), + '--disable-dart-dev', + os.path.join(FLUTTER_DIR, 'tools', 'githooks', 'bin', 'main.dart'), + ] + githook_args + [ + 'pre-rebase', + ] + argv[1:], cwd=SRC_ROOT) + return result.returncode + + +if __name__ == '__main__': + sys.exit(Main(sys.argv)) diff --git a/engine/src/flutter/tools/githooks/test/githooks_test.dart b/engine/src/flutter/tools/githooks/test/githooks_test.dart index 2b347d31dd..74639c2d54 100644 --- a/engine/src/flutter/tools/githooks/test/githooks_test.dart +++ b/engine/src/flutter/tools/githooks/test/githooks_test.dart @@ -66,4 +66,52 @@ void main() { } expect(result, equals(1)); }); + + test('post-merge runs successfully', () async { + int? result; + try { + final io.Directory flutterPath = io.File(io.Platform.script.path) + .parent.parent.parent; + result = await run([ + 'post-merge', + '--flutter', + flutterPath.path, + ]); + } catch (e, st) { + fail('Unexpected exception: $e\n$st'); + } + expect(result, equals(0)); + }); + + test('pre-rebase runs successfully', () async { + int? result; + try { + final io.Directory flutterPath = io.File(io.Platform.script.path) + .parent.parent.parent; + result = await run([ + 'pre-rebase', + '--flutter', + flutterPath.path, + ]); + } catch (e, st) { + fail('Unexpected exception: $e\n$st'); + } + expect(result, equals(0)); + }); + + test('post-checkout runs successfully', () async { + int? result; + try { + final io.Directory flutterPath = io.File(io.Platform.script.path) + .parent.parent.parent; + result = await run([ + 'post-checkout', + '--flutter', + flutterPath.path, + ]); + } catch (e, st) { + fail('Unexpected exception: $e\n$st'); + } + expect(result, equals(0)); + }); }