Skip to content

DevOps: Add Pre-Push Git Hook for Ruff Linting and Formatting #27

@hbseong97

Description

@hbseong97

DevOps: Add Pre-Push Git Hook for Ruff Linting and Formatting

Summary

Implement a Git pre-push hook to automatically run ruff check and ruff format before code is pushed to the remote repository. This will catch linting and formatting issues earlier in the development workflow, preventing CI failures and maintaining code quality standards.

Background

Currently, the project has:

  • ✅ Pre-commit hooks configured in .pre-commit-config.yaml with ruff
  • ✅ CI workflow (.github/workflows/ci.yml) that runs ruff checks on push/PR
  • ✅ Ruff configuration in pyproject.toml (line-length: 119, target: py310)

However, developers may bypass pre-commit hooks (e.g., git commit --no-verify) or not have them installed, leading to CI failures when code is pushed.

Objective

Add a pre-push hook that:

  1. Runs ruff check --fix to catch and auto-fix linting issues
  2. Runs ruff format --check to verify code formatting
  3. Blocks the push if either check fails
  4. Provides clear error messages to guide developers

Proposed Implementation

Option 1: Extend pre-commit configuration (Recommended)

Add a pre-push stage to .pre-commit-config.yaml:

repos:
  - repo: https://github.com/astral-sh/ruff-pre-commit
    rev: v0.8.3
    hooks:
      - id: ruff
        args: [--fix, --exit-non-zero-on-fix]
        stages: [pre-commit, pre-push]
      - id: ruff-format
        args: [--check]
        stages: [pre-commit, pre-push]

Option 2: Custom Git hook script

Create .git/hooks/pre-push script (or use a tool like Husky for Node.js projects, though less common in Python).

Acceptance Criteria

  • Pre-push hook is configured and documented
  • Hook runs ruff check with appropriate arguments
  • Hook runs ruff format --check to verify formatting
  • Hook prevents push if checks fail with clear error messages
  • Documentation updated in docs/contributing.md with setup instructions
  • README.md updated if necessary
  • Tested on local development environment

Configuration Details

Use existing ruff configuration from pyproject.toml:

  • Line length: 119
  • Target version: Python 3.10
  • Exclude: third_party/
  • Lint ignore: E402

Documentation Updates Needed

Update docs/contributing.md section "Pre-commit Checks" to include:

# Install pre-commit hooks (including pre-push)
pre-commit install --hook-type pre-push

# Run pre-push checks manually
pre-commit run --hook-stage push --all-files

Benefits

  • ✅ Catch formatting/linting issues before CI runs
  • ✅ Reduce CI failures and iteration time
  • ✅ Enforce code quality standards consistently
  • ✅ Align with existing CI workflow checks
  • ✅ Minimal developer friction (auto-fix when possible)

Related Files

  • .pre-commit-config.yaml - Main configuration file
  • pyproject.toml - Ruff settings
  • .github/workflows/ci.yml - CI checks to mirror
  • docs/contributing.md - Developer documentation

Priority

Medium - Improves developer experience and code quality, but not blocking current development.

Labels

devops, tooling, code-quality, developer-experience

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions