Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion tools/githooks/lib/githooks.dart
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,21 @@ 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<int> run(List<String> args) async {
final CommandRunner<bool> runner = CommandRunner<bool> (
'githooks',
'Githooks implementation for the flutter/engine repo.',
)
..addCommand(PrePushCommand());
..addCommand(PostCheckoutCommand())
..addCommand(PostMergeCommand())
..addCommand(PrePushCommand())
..addCommand(PreRebaseCommand());

// Add top-level arguments.
runner.argParser
Expand Down
19 changes: 19 additions & 0 deletions tools/githooks/lib/src/messages.dart
Original file line number Diff line number Diff line change
@@ -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',
Copy link
Contributor

@dkwingsmt dkwingsmt Mar 5, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Typo?

    '\n${prefix}You may need to run "gclient sync -D"$postfix\n',

);
}
22 changes: 22 additions & 0 deletions tools/githooks/lib/src/post_checkout_command.dart
Original file line number Diff line number Diff line change
@@ -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<bool> {
@override
final String name = 'post-checkout';

@override
final String description = 'Checks that run after the worktree is updated';

@override
Future<bool> run() async {
printGclientSyncReminder(name);
return true;
}
}
22 changes: 22 additions & 0 deletions tools/githooks/lib/src/post_merge_command.dart
Original file line number Diff line number Diff line change
@@ -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<bool> {
@override
final String name = 'post-merge';

@override
final String description = 'Checks to run after a "git merge"';

@override
Future<bool> run() async {
printGclientSyncReminder(name);
return true;
}
}
23 changes: 23 additions & 0 deletions tools/githooks/lib/src/pre_rebase_command.dart
Original file line number Diff line number Diff line change
@@ -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<bool> {
@override
final String name = 'pre-rebase';

@override
final String description = 'Checks to run before a "git rebase"';

@override
Future<bool> run() async {
printGclientSyncReminder(name);
// Returning false here will block the rebase.
return true;
}
}
35 changes: 35 additions & 0 deletions tools/githooks/post-checkout
Original file line number Diff line number Diff line change
@@ -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))
35 changes: 35 additions & 0 deletions tools/githooks/post-merge
Original file line number Diff line number Diff line change
@@ -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))
4 changes: 2 additions & 2 deletions tools/githooks/pre-push
Original file line number Diff line number Diff line change
@@ -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.
Expand Down Expand Up @@ -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


Expand Down
35 changes: 35 additions & 0 deletions tools/githooks/pre-rebase
Original file line number Diff line number Diff line change
@@ -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))
48 changes: 48 additions & 0 deletions tools/githooks/test/githooks_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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(<String>[
'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(<String>[
'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(<String>[
'post-checkout',
'--flutter',
flutterPath.path,
]);
} catch (e, st) {
fail('Unexpected exception: $e\n$st');
}
expect(result, equals(0));
});
}