From 8832caae8d58eb29ad0955b475c6a1bb57cdab4c Mon Sep 17 00:00:00 2001 From: Zach Anderson Date: Sun, 3 Mar 2024 15:33:30 -0800 Subject: [PATCH] Adds a post-merge githook that reminds to run gclient sync -D --- 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 +++++++++ tools/githooks/post-checkout | 35 ++++++++++++++ tools/githooks/post-merge | 35 ++++++++++++++ tools/githooks/pre-push | 4 +- tools/githooks/pre-rebase | 35 ++++++++++++++ tools/githooks/test/githooks_test.dart | 48 +++++++++++++++++++ 10 files changed, 248 insertions(+), 3 deletions(-) create mode 100644 tools/githooks/lib/src/messages.dart create mode 100644 tools/githooks/lib/src/post_checkout_command.dart create mode 100644 tools/githooks/lib/src/post_merge_command.dart create mode 100644 tools/githooks/lib/src/pre_rebase_command.dart create mode 100755 tools/githooks/post-checkout create mode 100755 tools/githooks/post-merge create mode 100755 tools/githooks/pre-rebase diff --git a/tools/githooks/lib/githooks.dart b/tools/githooks/lib/githooks.dart index 14c3d4349fff7..0a44e2ab33a0a 100644 --- a/tools/githooks/lib/githooks.dart +++ b/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/tools/githooks/lib/src/messages.dart b/tools/githooks/lib/src/messages.dart new file mode 100644 index 0000000000000..d524404cf5408 --- /dev/null +++ b/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/tools/githooks/lib/src/post_checkout_command.dart b/tools/githooks/lib/src/post_checkout_command.dart new file mode 100644 index 0000000000000..2be39d15e2c07 --- /dev/null +++ b/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/tools/githooks/lib/src/post_merge_command.dart b/tools/githooks/lib/src/post_merge_command.dart new file mode 100644 index 0000000000000..3ce259fda1b5d --- /dev/null +++ b/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/tools/githooks/lib/src/pre_rebase_command.dart b/tools/githooks/lib/src/pre_rebase_command.dart new file mode 100644 index 0000000000000..3e81670888261 --- /dev/null +++ b/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/tools/githooks/post-checkout b/tools/githooks/post-checkout new file mode 100755 index 0000000000000..123debdc28991 --- /dev/null +++ b/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/tools/githooks/post-merge b/tools/githooks/post-merge new file mode 100755 index 0000000000000..18beeef645059 --- /dev/null +++ b/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/tools/githooks/pre-push b/tools/githooks/pre-push index 0f629a08c8704..097730aae6b9f 100755 --- a/tools/githooks/pre-push +++ b/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/tools/githooks/pre-rebase b/tools/githooks/pre-rebase new file mode 100755 index 0000000000000..afaf9423e9949 --- /dev/null +++ b/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/tools/githooks/test/githooks_test.dart b/tools/githooks/test/githooks_test.dart index 2b347d31dd20f..74639c2d549c8 100644 --- a/tools/githooks/test/githooks_test.dart +++ b/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)); + }); }