Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update script to run clang-format #37

Merged
merged 1 commit into from
Nov 6, 2024
Merged
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
26 changes: 17 additions & 9 deletions tools/run-clang-format.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ def file_filter(fname: str):
return False


def check_format_changes(git_dir: Path, base_commit: str):
def check_format_changes(git_dir: Path, base_commit: str, clang_format_exe: str):
git_files = get_files(git_dir, base_commit)
files_to_update = []
for fname in filter(file_filter, git_files):
# Do a dry run, and stop on the first error found
clang_format_dry = ["clang-format", "-n", "--ferror-limit=1", str(git_dir.absolute() / fname)]
clang_format_dry = [clang_format_exe, "-n", "--ferror-limit=1", str(git_dir.absolute() / fname)]
clang_format_output = (
subprocess.run(clang_format_dry, check=True, capture_output=True)
.stderr.decode("utf-8").strip()
Expand All @@ -45,18 +45,18 @@ def check_format_changes(git_dir: Path, base_commit: str):
files_to_update.append(str(git_dir.absolute() / fname))
if len(files_to_update) > 0:
sep="\n\t"
print(f"Found clang-format changes in files\n{sep.join(files_to_update)}"
"\nPlease address clang-format errors and commit the changes")
print(f"Found /usr/bin/clang-format changes in files\n{sep.join(files_to_update)}"
"\nPlease address /usr/bin/clang-format errors and commit the changes")
return 1
print("No clang-format changes found")
print(f"No changes found with '{clang_format_exe}'")
return 0


def apply_clang_format(git_dir: Path, base_commit: str):
def apply_clang_format(git_dir: Path, base_commit: str, clang_format_exe: str):
git_files = get_files(git_dir, base_commit)
# Apply clang format to all of the files we are interested in
for fname in filter(file_filter, git_files):
clang_format_apply = ["clang-format", "-i", str(git_dir.absolute() / fname)]
clang_format_apply = [clang_format_exe, "-i", str(git_dir.absolute() / fname)]
subprocess.run(clang_format_apply, check=True)
return 0

Expand All @@ -71,11 +71,19 @@ def apply_clang_format(git_dir: Path, base_commit: str):
help="Base commit to find files with changes in. "
"If not given, runs clang format on the whole repo"
)
def main(run_type, base_commit):
@click.option(
"--clang-format-exe",
help="Command to use for clang-format. This needs to point to the same "
"version of clang-format as run on the CI, otherwise you may end up "
"with different corrections. At the time of writing (2024/11), this is "
"version 14.0",
default="clang-format"
)
def main(run_type, base_commit, clang_format_exe):
git_dir = Path(__file__).absolute().parent.parent

clang_format_func = check_format_changes if (run_type == "check") else apply_clang_format
sys.exit(clang_format_func(git_dir, base_commit))
sys.exit(clang_format_func(git_dir, base_commit, clang_format_exe))


if __name__ == "__main__":
Expand Down
Loading