From ea7f739224c998372aabc502115e4e79d103f1cf Mon Sep 17 00:00:00 2001 From: Mikhail Date: Wed, 22 Jun 2022 12:08:45 +0700 Subject: [PATCH] Fix chromium tools/git/move_source_file.py (#13579) * Fix chromium tools/git/move_source_file.py * Add npm run mass-rename * Add a comment * Review fixes * mass-rename => mass_rename * More review fixes --- build/commands/lib/util.js | 5 +++ build/commands/scripts/commands.js | 4 +++ package.json | 1 + patches/tools-git-move_source_file.py.patch | 36 +++++++++++++++++++++ script/move_source_fille_utils.py | 25 ++++++++++++++ 5 files changed, 71 insertions(+) create mode 100644 patches/tools-git-move_source_file.py.patch create mode 100644 script/move_source_fille_utils.py diff --git a/build/commands/lib/util.js b/build/commands/lib/util.js index a72da3eebd6a..84a560564224 100755 --- a/build/commands/lib/util.js +++ b/build/commands/lib/util.js @@ -669,6 +669,11 @@ const util = { util.run(cmd, args, cmd_options) }, + massRename: (options = {}) => { + let cmd_options = config.defaultOptions + cmd_options.cwd = config.braveCoreDir + util.run('python3', [path.join(config.srcDir, 'tools', 'git', 'mass-rename.py')], cmd_options) + }, shouldUpdateChromium: (chromiumRef = config.getProjectRef('chrome')) => { const headSHA = util.runGit(config.srcDir, ['rev-parse', 'HEAD'], true) diff --git a/build/commands/scripts/commands.js b/build/commands/scripts/commands.js index 47ad38b79241..e5689d16ec33 100644 --- a/build/commands/scripts/commands.js +++ b/build/commands/scripts/commands.js @@ -275,5 +275,9 @@ program 'enables formatting of Swift file types using swift-format') .action(util.format) +program + .command('mass_rename') + .action(util.massRename) + program .parse(process.argv) diff --git a/package.json b/package.json index 20fcda173a22..260c645865bc 100644 --- a/package.json +++ b/package.json @@ -26,6 +26,7 @@ "lint": "node ./build/commands/scripts/commands.js lint", "presubmit": "node ./build/commands/scripts/commands.js presubmit", "format": "node ./build/commands/scripts/commands.js format", + "mass_rename": "node ./build/commands/scripts/commands.js mass_rename", "test": "node ./build/commands/scripts/commands.js test", "test:scripts": "jest build/commands/lib build/commands/scripts", "test-security": "npm run check_security && npm run audit_deps && npm run network-audit", diff --git a/patches/tools-git-move_source_file.py.patch b/patches/tools-git-move_source_file.py.patch new file mode 100644 index 000000000000..236db0849993 --- /dev/null +++ b/patches/tools-git-move_source_file.py.patch @@ -0,0 +1,36 @@ +diff --git a/tools/git/move_source_file.py b/tools/git/move_source_file.py +index ab57ae721179f77bb92b808e65dfe1ced8cf605f..ef3fb8f06b312609cf86e0c088e7c4804c9eef8e 100755 +--- a/tools/git/move_source_file.py ++++ b/tools/git/move_source_file.py +@@ -91,6 +91,7 @@ def UpdatePostMove(from_path, to_path): + to update references in .gyp(i) files using a heuristic. + """ + # Include paths always use forward slashes. ++ from move_source_fille_utils import to_src_relative_path; from_path, to_path = to_src_relative_path(from_path, to_path) + from_path = from_path.replace('\\', '/') + to_path = to_path.replace('\\', '/') + extension = os.path.splitext(from_path)[1] +@@ -115,6 +116,7 @@ def UpdatePostMove(from_path, to_path): + mffr.MultiFileFindReplace( + r'(//.*)%s' % re.escape(from_path), + r'\1%s' % to_path, ++ ['*.gni', '*.gn'] + # Brave uses full paths (start with //) to add files + ['*.cc', '*.h', '*.m', '*.mm', '*.cpp']) + + # Update references in GYP and BUILD.gn files. +@@ -150,6 +152,7 @@ def UpdatePostMove(from_path, to_path): + return (parts[0], '') + + visiting_directory = '' ++ from move_source_fille_utils import to_cwd_relative_path; from_path, to_path = to_cwd_relative_path(from_path, to_path) + from_rest = from_path + to_rest = to_path + while True: +@@ -186,6 +189,7 @@ def UpdateIncludeGuard(old_path, new_path): + old_guard = MakeIncludeGuardName(old_path) + new_guard = MakeIncludeGuardName(new_path) + ++ from move_source_fille_utils import to_cwd_relative_path; [new_path] = to_cwd_relative_path(new_path) + with open(new_path) as f: + contents = f.read() + diff --git a/script/move_source_fille_utils.py b/script/move_source_fille_utils.py new file mode 100644 index 000000000000..74e748ca4c0e --- /dev/null +++ b/script/move_source_fille_utils.py @@ -0,0 +1,25 @@ +# Copyright (c) 2022 The Brave Authors. All rights reserved. +# This Source Code Form is subject to the terms of the Mozilla Public +# License, v. 2.0. If a copy of the MPL was not distributed with this file, +# You can obtain one at https://mozilla.org/MPL/2.0/. */ + +"""This utils is used in chromium tools/git/move_source_file.py.""" + +import os + +BRAVE_DIR_NAME = 'brave' + +def _is_in_brave_dir(): + return BRAVE_DIR_NAME == os.path.basename(os.getcwd()) + + +def to_src_relative_path(*args): + if _is_in_brave_dir(): + return tuple(os.path.join(BRAVE_DIR_NAME, d) for d in args) + return args + + +def to_cwd_relative_path(*args): + if _is_in_brave_dir(): + return tuple(os.path.relpath(d, BRAVE_DIR_NAME) for d in args) + return args