diff --git a/git_cl.py b/git_cl.py index d48f21f3c..f35846ad9 100755 --- a/git_cl.py +++ b/git_cl.py @@ -46,6 +46,7 @@ import gerrit_util import git_common import git_footers import git_new_branch +import google_java_format import metrics import metrics_utils import owners_client @@ -6142,34 +6143,16 @@ def _RunClangFormatDiff(opts, clang_diff_files, top_dir, upstream_commit): return return_value -def _FindGoogleJavaFormat(): - # Allow non-chromium projects to use a custom location. - primary_solution_path = gclient_paths.GetPrimarySolutionPath() - if primary_solution_path: - override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH') - if override: - # Make relative to solution root if not an absolute path. - return os.path.join(primary_solution_path, override) - - path = os.path.join(primary_solution_path, 'third_party', - 'google-java-format', 'google-java-format') - # Check that the .jar exists, since it is conditionally downloaded via - # DEPS conditions. - if os.path.exists(path) and os.path.exists(path + '.jar'): - return path - return None - - def _RunGoogleJavaFormat(opts, paths, top_dir, upstream_commit): """Runs google-java-format and sets a return value if necessary.""" - google_java_format = _FindGoogleJavaFormat() - if google_java_format is None: + tool = google_java_format.FindGoogleJavaFormat() + if tool is None: # Fail silently. It could be we are on an old chromium revision, or that # it is a non-chromium project. https://crbug.com/1491627 print('google-java-format not found, skipping java formatting.') return 0 - base_cmd = [google_java_format, '--aosp'] + base_cmd = [tool, '--aosp'] if not opts.diff: if opts.dry_run: base_cmd += ['--dry-run'] diff --git a/google-java-format b/google-java-format new file mode 100755 index 000000000..2c5e99df1 --- /dev/null +++ b/google-java-format @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. + +base_dir=$(dirname "$0") + +PYTHONDONTWRITEBYTECODE=1 exec python3 "$base_dir/google_java_format.py" "$@" diff --git a/google-java-format.bat b/google-java-format.bat new file mode 100644 index 000000000..1d95e0407 --- /dev/null +++ b/google-java-format.bat @@ -0,0 +1,12 @@ +@echo off +:: Copyright 2014 The Chromium Authors. All rights reserved. +:: Use of this source code is governed by a BSD-style license that can be +:: found in the LICENSE file. +setlocal + +:: Ensure that "depot_tools" is somewhere in PATH so this tool can be used +:: standalone, but allow other PATH manipulations to take priority. +set PATH=%PATH%;%~dp0 + +:: Defer control. +python3 "%~dp0\google_java_format.py" %* diff --git a/google_java_format.py b/google_java_format.py new file mode 100755 index 000000000..ec030d132 --- /dev/null +++ b/google_java_format.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 +# Copyright 2014 The Chromium Authors. All rights reserved. +# Use of this source code is governed by a BSD-style license that can be +# found in the LICENSE file. +"""Redirects to the version of google-java-format checked into the Chrome tree. + +google-java-format executable is pulled down from the cipd storage whenever +you sync Chrome. This script finds and runs the executable. +""" + +import gclient_paths +import os +import subprocess +import sys + + +def FindGoogleJavaFormat(): + """Returns the path to the google-java-format executable.""" + # Allow non-chromium projects to use a custom location. + primary_solution_path = gclient_paths.GetPrimarySolutionPath() + if primary_solution_path: + override = os.environ.get('GOOGLE_JAVA_FORMAT_PATH') + if override: + # Make relative to solution root if not an absolute path. + return os.path.join(primary_solution_path, override) + + path = os.path.join(primary_solution_path, 'third_party', + 'google-java-format', 'google-java-format') + # Check that the .jar exists, since it is conditionally downloaded via + # DEPS conditions. + if os.path.exists(path) and os.path.exists(path + '.jar'): + return path + return None + + +def main(args): + google_java_format = FindGoogleJavaFormat() + if google_java_format is None: + # Fail silently. It could be we are on an old chromium revision, + # or that it is a non-chromium project. https://crbug.com/1491627. + print('google-java-format not found, skipping java formatting.') + return 0 + + # Add some visibility to --help showing where the tool lives, since this + # redirection can be a little opaque. + help_syntax = ('-h', '--help', '-help', '-help-list', '--help-list') + if any(match in args for match in help_syntax): + print('\nDepot tools redirects you to the google-java-format at:\n' + + ' %s\n' % google_java_format) + + return subprocess.call([google_java_format] + args) + + +if __name__ == '__main__': + try: + sys.exit(main(sys.argv[1:])) + except KeyboardInterrupt: + sys.stderr.write('interrupted\n') + sys.exit(1)