Skip to content
Open
Show file tree
Hide file tree
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
55 changes: 54 additions & 1 deletion .githooks/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,57 @@ if git diff --cached --name-only | grep -q "^torch_pin.py$"; then
fi
fi

exit 0
# --- lintrunner ---

if ! command -v lintrunner >/dev/null 2>&1; then
echo "Warning: lintrunner not found. Skipping lint checks."
echo "Install with: pip install lintrunner lintrunner-adapters && lintrunner init"
exit 0
fi

if [ ! -f .lintrunner.toml ]; then
echo "Warning: .lintrunner.toml not found. Skipping lint checks."
exit 0
fi

git_dir=$(git rev-parse --git-dir)

# Portable hash: sha256sum (Linux) or shasum (macOS)
if command -v sha256sum >/dev/null 2>&1; then
toml_hash=$(sha256sum .lintrunner.toml | cut -d' ' -f1)
else
toml_hash=$(shasum -a 256 .lintrunner.toml | cut -d' ' -f1)
fi
stored_hash=""
[ -f "${git_dir}/.lintrunner_init_hash" ] && stored_hash=$(cat "${git_dir}/.lintrunner_init_hash")

if [ "${toml_hash}" != "${stored_hash}" ]; then
echo "Running lintrunner init..."
if lintrunner init; then
echo "${toml_hash}" > "${git_dir}/.lintrunner_init_hash"
else
echo "lintrunner init failed. Run 'lintrunner init' manually."
exit 1
fi
fi

staged_files=$(git diff --cached --name-only --diff-filter=ACMR)

# Use HEAD^ if it exists (skip on initial commit)
revision_flag="--revision HEAD^"
if ! git rev-parse HEAD^ >/dev/null 2>&1; then
revision_flag=""
fi

lintrunner -a $revision_flag --skip MYPY
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Remind me why skip MYPY?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is taking too much time and every time if the developer amends the commit, it might annoy them.

lint_status=$?

# Re-stage only paths that were already staged before lintrunner ran.
while IFS= read -r path; do
[ -z "${path}" ] && continue
if [ -f "${path}" ]; then
git add -- "${path}" || true
fi
done <<< "${staged_files}"

exit $lint_status
5 changes: 5 additions & 0 deletions install_executorch.sh
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,8 @@
# so we avoid repeated symlink segments in downstream CMake paths.
cd -- "$( realpath "$( dirname -- "${BASH_SOURCE[0]}" )" )" &> /dev/null || /bin/true
./run_python_script.sh ./install_executorch.py "$@"

# Install git hooks if inside a git repo
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What if someone else (IIRC Arm) installs their own pre-commit hooks, would it collide with that?

Also Arm has more scripts here for push etc - https://github.com/pytorch/executorch/tree/19bbeac41ab4ba21aa95a44d464e72b8da571302/backends/arm/scripts

We should consolidate.

if git rev-parse --git-dir > /dev/null 2>&1; then
git config core.hooksPath .githooks
fi
Loading