def CheckPatchFormatted(input_api, output_api, check_js=False): import git_cl cmd = ['-C', input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run', '--presubmit'] if check_js: cmd.append('--js') presubmit_subdir = input_api.os_path.relpath( input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()) if presubmit_subdir.startswith('..') or presubmit_subdir == '.': presubmit_subdir = '' # If the PRESUBMIT.py is in a parent repository, then format the entire # subrepository. Otherwise, format only the code in the directory that # contains the PRESUBMIT.py. if presubmit_subdir: cmd.append(input_api.PresubmitLocalPath()) code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) if code == 2: short_path = input_api.basename(input_api.PresubmitLocalPath()) return [output_api.PresubmitPromptWarning( 'The %s directory requires source formatting. ' 'Please run: git cl format %s%s' % (short_path, '--js ' if check_js else '', presubmit_subdir))] # As this is just a warning, ignore all other errors if the user # happens to have a broken clang-format, doesn't use git, etc etc. return []
def CheckPatchFormatted(input_api, output_api): import git_cl cmd = ['cl', 'format', '--dry-run', input_api.PresubmitLocalPath()] code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) if code == 2: return [output_api.PresubmitPromptWarning( 'Your patch is not formatted, please run git cl format.')] # As this is just a warning, ignore all other errors if the user # happens to have a broken clang-format, doesn't use git, etc etc. return []
def CheckPatchFormatted(input_api, output_api): import git_cl cmd = ['cl', 'format', '--dry-run', input_api.PresubmitLocalPath()] code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) if code == 2: short_path = input_api.basename(input_api.PresubmitLocalPath()) full_path = input_api.os_path.relpath(input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()) return [output_api.PresubmitPromptWarning( 'The %s directory requires source formatting. ' 'Please run git cl format %s' % (short_path, full_path))] # As this is just a warning, ignore all other errors if the user # happens to have a broken clang-format, doesn't use git, etc etc. return []
def CheckPatchFormatted(input_api, output_api, bypass_warnings=True, check_js=False, check_python=None, result_factory=None): result_factory = result_factory or output_api.PresubmitPromptWarning import git_cl display_args = [] if check_js: display_args.append('--js') # Explicitly setting check_python to will enable/disable python formatting # on all files. Leaving it as None will enable checking patch formatting # on files that have a .style.yapf file in a parent directory. if check_python is not None: if check_python: display_args.append('--python') else: display_args.append('--no-python') cmd = ['-C', input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run', '--presubmit'] + display_args presubmit_subdir = input_api.os_path.relpath( input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()) if presubmit_subdir.startswith('..') or presubmit_subdir == '.': presubmit_subdir = '' # If the PRESUBMIT.py is in a parent repository, then format the entire # subrepository. Otherwise, format only the code in the directory that # contains the PRESUBMIT.py. if presubmit_subdir: cmd.append(input_api.PresubmitLocalPath()) code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=bypass_warnings) # bypass_warnings? Only fail with code 2. # As this is just a warning, ignore all other errors if the user # happens to have a broken clang-format, doesn't use git, etc etc. if code == 2 or (code and not bypass_warnings): if presubmit_subdir: short_path = presubmit_subdir else: short_path = input_api.basename(input_api.change.RepositoryRoot()) display_args.append(presubmit_subdir) return [result_factory( 'The %s directory requires source formatting. ' 'Please run: git cl format %s' % (short_path, ' '.join(display_args)))] return []
def CheckPatchFormatted( input_api, output_api, check_js=False, check_python=False, result_factory=None): result_factory = result_factory or output_api.PresubmitPromptWarning import git_cl display_args = [] if check_js: display_args.append('--js') if check_python: # --python requires --full display_args.extend(['--python', '--full']) cmd = ['-C', input_api.change.RepositoryRoot(), 'cl', 'format', '--dry-run', '--presubmit'] + display_args presubmit_subdir = input_api.os_path.relpath( input_api.PresubmitLocalPath(), input_api.change.RepositoryRoot()) if presubmit_subdir.startswith('..') or presubmit_subdir == '.': presubmit_subdir = '' # If the PRESUBMIT.py is in a parent repository, then format the entire # subrepository. Otherwise, format only the code in the directory that # contains the PRESUBMIT.py. if presubmit_subdir: cmd.append(input_api.PresubmitLocalPath()) code, _ = git_cl.RunGitWithCode(cmd, suppress_stderr=True) if code == 2: if presubmit_subdir: short_path = presubmit_subdir else: short_path = input_api.basename(input_api.change.RepositoryRoot()) display_args.append(presubmit_subdir) return [result_factory( 'The %s directory requires source formatting. ' 'Please run: git cl format %s' % (short_path, ' '.join(display_args)))] # As this is just a warning, ignore all other errors if the user # happens to have a broken clang-format, doesn't use git, etc etc. return []