[presubmit] Support option to run on all files (#4870)

This commit is contained in:
jonathanmetzman 2020-12-18 10:43:22 -08:00 committed by GitHub
parent 2a1194c939
commit c2165341d2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 21 additions and 6 deletions

View File

@ -351,6 +351,13 @@ def run_tests():
return not result.failures and not result.errors
def get_all_files():
"""Returns a list of absolute paths of files in this repo."""
get_all_files_command = ['git', 'ls-files']
output = subprocess.check_output(get_all_files_command).decode().splitlines()
return [os.path.abspath(path) for path in output if os.path.isfile(path)]
def main():
"""Check changes on a branch for common issues before submitting."""
# Get program arguments.
@ -358,30 +365,38 @@ def main():
parser.add_argument('command',
choices=['format', 'lint', 'license', 'infra-tests'],
nargs='?')
parser.add_argument('--all-files',
action='store_true',
help='Run presubmit check(s) on all files',
default=False)
args = parser.parse_args()
changed_files = get_changed_files()
if args.all_files:
relevant_files = get_all_files()
else:
relevant_files = get_changed_files()
os.chdir(_SRC_ROOT)
# Do one specific check if the user asked for it.
if args.command == 'format':
success = yapf(changed_files, False)
success = yapf(relevant_files, False)
return bool_to_returncode(success)
if args.command == 'lint':
success = lint(changed_files)
success = lint(relevant_files)
return bool_to_returncode(success)
if args.command == 'license':
success = check_license(changed_files)
success = check_license(relevant_files)
return bool_to_returncode(success)
if args.command == 'infra-tests':
return bool_to_returncode(run_tests())
success = run_tests()
return bool_to_returncode(success)
# Do all the checks (but no tests).
success = do_checks(changed_files)
success = do_checks(relevant_files)
return bool_to_returncode(success)