Skip to content

Commit

Permalink
[Setup] Add install pre commit function (#33693)
Browse files Browse the repository at this point in the history
  • Loading branch information
CelianR authored Feb 4, 2025
1 parent 5025a79 commit 0f9cd2a
Showing 1 changed file with 28 additions and 4 deletions.
32 changes: 28 additions & 4 deletions tasks/setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,16 +168,27 @@ def update_python_dependencies(ctx) -> Generator[SetupResult]:
yield SetupResult(f"Update Python dependencies from {requirement_file}", Status.OK)


def enable_pre_commit(ctx) -> SetupResult:
@task
def pre_commit(ctx, interactive=True):
"""Will set up pre-commit hooks.
Note:
pre-commit hooks will be uninstalled / cleaned before being installed.
"""

print(color_message("Enabling pre-commit...", Color.BLUE))

status = Status.OK
message = ""

if not ctx.run("pre-commit --version", hide=True, warn=True).ok:
return SetupResult(
"Enable pre-commit", Status.FAIL, "Please install pre-commit first: https://pre-commit.com/#installation."
)
message = "Please install pre-commit binary first: https://pre-commit.com/#installation."
status = Status.FAIL

if interactive:
raise Exit(code=1, message=f'{color_message("Error:", Color.RED)} {message}')

return Status.FAIL, message

try:
# Some dd-hooks can mess up with pre-commit
Expand Down Expand Up @@ -209,11 +220,24 @@ def enable_pre_commit(ctx) -> SetupResult:

# Uninstall in case someone switch from one config to the other
ctx.run("pre-commit uninstall", hide=True)
ctx.run("pre-commit clean", hide=True)
ctx.run(f"pre-commit install --config {config_file}", hide=True)

if hooks_path:
ctx.run(f"git config --global core.hooksPath {hooks_path}", hide=True)

if interactive:
if message:
print(f'{color_message("Warning:", Color.ORANGE)} {message}')

print(color_message("Pre-commit installed and enabled.", Color.GREEN))

return status, message


def enable_pre_commit(ctx) -> SetupResult:
status, message = pre_commit(ctx, interactive=False)

return SetupResult("Enable pre-commit", status, message)


Expand Down

0 comments on commit 0f9cd2a

Please sign in to comment.