Skip to content

Commit

Permalink
Update pre-commit hook
Browse files Browse the repository at this point in the history
Add pre-commit hook to prevent committing .zip files
  • Loading branch information
SaumilPatel03 committed Feb 5, 2025
1 parent b13b468 commit 3b87b57
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -1433,4 +1433,9 @@ repos:
pass_filenames: false
files: ^airflow/migrations/versions/.*\.py$|^docs/apache-airflow/migrations-ref\.rst$
additional_dependencies: ['rich>=12.4.4']
- id: check-zip-files
name: Forbid committing .zip files
entry: python ./scripts/ci/pre_commit/check_zip_files.py
language: python
files: \.zip$
## ONLY ADD PRE-COMMITS HERE THAT REQUIRE CI IMAGE
2 changes: 2 additions & 0 deletions contributing-docs/08_static_code_checks.rst
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ require Breeze Docker image to be built locally.
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| check-xml | Check XML files with xmllint | |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| check-zip-files | Forbid committing .zip files | * |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| codespell | Run codespell | |
+-----------------------------------------------------------+--------------------------------------------------------+---------+
| compile-ui-assets | Compile ui assets (manual) | |
Expand Down
2 changes: 1 addition & 1 deletion dev/breeze/doc/images/output_static-checks.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
8b916a8eceeecdae4936ee72ec060491
6bb06b6e0cf75f8c01e2a1727edf4515
1 change: 1 addition & 0 deletions dev/breeze/src/airflow_breeze/pre_commit_ids.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@
"check-urlparse-usage-in-code",
"check-usage-of-re2-over-re",
"check-xml",
"check-zip-files",
"codespell",
"compile-ui-assets",
"compile-ui-assets-dev",
Expand Down
51 changes: 51 additions & 0 deletions scripts/ci/pre_commit/check_zip_files.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/usr/bin/env python3
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.
from __future__ import annotations

import subprocess
import sys


def get_staged_files():
"""Return a list of files that are staged for commit."""
result = subprocess.run(
["git", "diff", "--cached", "--name-only"],
capture_output=True,
text=True,
)
if result.returncode != 0:
print("Error: Unable to retrieve staged files.", file=sys.stderr)
sys.exit(1)
return result.stdout.splitlines()


def main():
staged_files = get_staged_files()
# Look for any file ending with .zip (case-insensitive)
zip_files = [f for f in staged_files if f.lower().endswith(".zip")]
if zip_files:
print("Error: Committing .zip files is not allowed.")
print("The following .zip file(s) were found in your staged changes:")
for file in zip_files:
print(" -", file)
sys.exit(1)
sys.exit(0)


if __name__ == "__main__":
main()

0 comments on commit 3b87b57

Please sign in to comment.