Skip to content

Commit

Permalink
build: introduce pre-commit (#18)
Browse files Browse the repository at this point in the history
**Description:**
This PR introduces consistent formatting for all C++ and Python files to
ensure uniform code style across the repository. While this is not a
final set of formatting rules, it serves as an initial baseline that can
be refined over time.

Since this change affects most files, merging it will help keep future
PRs clean and focused on functional changes rather than formatting
inconsistencies. After merging, I will follow up with another PR to
remove this change from git history, preventing unnecessary git blame
noise.

**Next Steps:**
- Specific formatting rules can be adjusted as needed.
- A follow-up PR will clean up the commit history to maintain meaningful
git blame tracking.

Let me know if you have any concerns or suggestions!
  • Loading branch information
fvranicTT authored Feb 25, 2025
1 parent 3af8549 commit 7e81cb9
Show file tree
Hide file tree
Showing 212 changed files with 18,999 additions and 17,394 deletions.
46 changes: 46 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
BasedOnStyle: Google
ColumnLimit: 120
IndentWidth: 4

# This will make access modifiers (public/protected/private) sit on the same indentation as `class` keyword
AccessModifierOffset: -4

# Arguments, parameters and construction initializer are broken as following:
# - Try to fit everything into single line (controlled by ColumnLimit).
# - If it doesn't fit, break immediately after open bracket (in case of arguments and parameters)
# or after colon in case of constructor initializers.
# - Try to fit everything else into the second line.
# - If it doesn't fit in second line, then each argument, parameter or initializer will sit in its own line.
AlignAfterOpenBracket: AlwaysBreak
AlignConsecutiveAssignments: true
AlignConsecutiveBitFields: true
AlignConsecutiveDeclarations: true
AlignTrailingComments: true
AlignEscapedNewlines: Left
AlignOperands: true
BinPackArguments: false
BinPackParameters: false

# When constructor initializers exist in the constructor definition, leave the colon as last thing on the original
# line instead of putting it on the next line.
BreakConstructorInitializers: AfterColon

# Pointer and reference alignment
DerivePointerAlignment: true
PointerAlignment: Left
ReferenceAlignment: Left

# Disallow single statements after if/else/for/while/do without curly braces.
InsertBraces: true
BreakBeforeBraces: Attach

# Separate definition blocks, including classes, structs, enums, and functions.
SeparateDefinitionBlocks: Always
SpaceBeforeCpp11BracedList: true
SpacesBeforeTrailingComments: 1
SpacesInAngles: Never
AllowShortLoopsOnASingleLine: true
AllowShortBlocksOnASingleLine: true
AllowShortEnumsOnASingleLine: false
RemoveSemicolon: true
SortUsingDeclarations: LexicographicNumeric
28 changes: 28 additions & 0 deletions .github/workflows/pre-commit.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# Run Pre-commit Hooks
name: Run Pre-commit Hooks

on:
workflow_dispatch:
pull_request:
push:
branches: ["main"]

jobs:
pre-commit:
name: Run Pre-commit Hooks
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Fetch all history so 'origin/main' is available

- name: Set up Python
uses: actions/setup-python@v4
with:
python-version: 3.8

- name: Run Pre-commit and Fix Issues
uses: pre-commit/action@v3.0.1
with:
extra_args: "--from-ref origin/main --to-ref HEAD"
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
build/
*.o
*.elf
*.d/
.cache/**
.envrc
.vscode/
*.log
*.csv
33 changes: 33 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v19.1.7
hooks:
- id: clang-format
args: ["-style=file"]
- repo: https://github.com/psf/black-pre-commit-mirror
rev: 24.8.0
hooks:
- id: black
language_version: python3
- repo: https://github.com/adrienverge/yamllint
rev: v1.35.1
hooks:
- id: yamllint
exclude: ^(?!\.github/).* # Only include files in .github/
- repo: https://github.com/espressif/check-copyright/
rev: v1.0.3
hooks:
- id: check-copyright
args: ['--config', 'infra/check_copyright_config.yaml', "--ignore", ""]
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v3.4.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
- id: check-added-large-files
args: ["--maxkb=500"]
- id: check-merge-conflict
- id: check-executables-have-shebangs
- id: check-docstring-first
- id: no-commit-to-branch
args: ["--branch", "main"]
11 changes: 11 additions & 0 deletions .yamllint
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
---

extends: default

rules:
# yaml documents should start optionally with ---
document-start: disable
line-length:
max: 120
# the "on:" is detected as truthy value and spawns a false warning
truthy: disable
39 changes: 39 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
## Contribution standards
This project has adopted C++ formatting and style as defined in `.clang-format`.
There are additional requirements such as license headers.

## Pre-commit Hook Integration for Formatting and Linting

As part of maintaining consistent code formatting across the project, we have integrated the [pre-commit](https://pre-commit.com/) framework into our workflow. The pre-commit hooks will help automatically check and format code before commits are made, ensuring that we adhere to the project's coding standards.

### What is Pre-commit?

Pre-commit is a framework for managing and maintaining multi-language pre-commit hooks. It helps catch common issues early by running a set of hooks before code is committed, automating tasks like:

- Formatting code (e.g., fixing trailing whitespace, enforcing end-of-file newlines)
- Running linters (e.g., `clang-format`, `black`, `flake8`)
- Checking for merge conflicts or other common issues.

For more details on pre-commit, you can visit the [official documentation](https://pre-commit.com/).

### How to Set Up Pre-commit Locally

To set up pre-commit on your local machine, follow these steps:

1. **Install Pre-commit**:
Ensure you have Python installed, then run:
```bash
pip install pre-commit
```
*Note:* pre-commit is already installed if you are using the python virtual environment.
2. **Install the Git Hook Scripts**:
In your local repository, run the following command to install the pre-commit hooks:
```bash
pre-commit install
```
This command will configure your local Git to run the defined hooks automatically before each commit.
3. **Run Pre-commit Hooks Manually**:
You can also run the hooks manually against all files at any time with:
```bash
pre-commit run --all-files
```
5 changes: 5 additions & 0 deletions infra/check_copyright_config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,11 @@ DEFAULT:
//
// SPDX-License-Identifier: {license}
new_notice_python: | # notice for new python files
# SPDX-FileCopyrightText: © {years} Tenstorrent AI ULC
#
# SPDX-License-Identifier: {license}
# comment lines matching:
# SPDX-FileCopyrightText: year[-year] Espressif Systems
# or
Expand Down
Loading

0 comments on commit 7e81cb9

Please sign in to comment.