Skip to content

Commit 22e6643

Browse files
committed
document pre-commit in modules 1 and 6
1 parent cc23d0d commit 22e6643

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

modules/module_1/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ In this module, the focus is on **code quality and static analysis** in ROS 2.
1111
- [Frequent Conflicts and Incompatibilities](#frequent-conflicts-and-incompatibilities)
1212
- [Recommendations](#recommendations)
1313
- [Beyond Static Analysis: Runtime Sanitizers](#beyond-static-analysis-runtime-sanitizers)
14+
- [Pre-commit hooks](#pre-commit-hooks)
1415
- [Other Useful Tools](#other-useful-tools)
1516
- [Colcon lint](#colcon-lint)
1617
- [ROS 2 doctor](#ros-2-doctor)
@@ -128,6 +129,37 @@ Unlike the tools above (which analyze code without running it), sanitizers are s
128129

129130
While configuring them is beyond the scope of this module, it's crucial to know they exist. They are the ultimate tools for finding those bugs that only appear randomly. They can be enabled in the colcon build by passing compiler flags.
130131

132+
## Pre-commit hooks
133+
134+
After exploring the main tools that ensure code quality in ROS 2, let’s look at a lightweight way to integrate them seamlessly into everyday development: **pre-commit hooks**.
135+
136+
[Pre-commit](https://pre-commit.com) hooks are a **local, optional step** that run before a Git commit is finalized. Their main goal is to provide **immediate feedback** and automatically correct simple issues such as code formatting or basic linting on the **staged files only**. This keeps the commit history clean and avoids unnecessary “fix formatting” commits, while also speeding up the remote CI process.
137+
138+
Typical hooks include formatters like `clang-format`, linters such as `clang-tidy`, and scripts that check file headers or trailing whitespace. These checks are intentionally fast so they don’t interrupt development flow.
139+
140+
Developers can still bypass the hooks when needed by running `git commit --no-verify`.
141+
142+
To enable pre-commit in a repository:
143+
144+
1. Add a `.pre-commit-config.yaml` listing the desired hooks.
145+
2. Run once to install:
146+
147+
```bash
148+
pip install pre-commit
149+
pre-commit install
150+
```
151+
152+
3. Each `git commit` will now automatically trigger the configured checks. To run all hooks manually:
153+
154+
```bash
155+
pre-commit run --all-files
156+
```
157+
158+
Pre-commit hooks are best suited for **fast, local hygiene checks**. They complement full workspace analyses done by `colcon test` and `ament_lint_auto`, which can also be run locally or in CI (introduced in Module 6).
159+
160+
> [!NOTE]
161+
> This workshop does not use pre-commit for static analysis or linters, since those checks are demonstrated through `ament_lint_auto` and `colcon test`.
162+
131163
## Other Useful Tools
132164

133165
### Colcon lint
@@ -221,6 +253,7 @@ The task is complete when the command `colcon lint --packages-select module_1` i
221253
## References
222254

223255
- [ROS 2 code style guide](https://docs.ros.org/en/jazzy/The-ROS2-Project/Contributing/Code-Style-Language-Versions.html)
256+
- [Pre-commit](https://pre-commit.com)
224257
- [ament_lint_auto docs](https://github.com/ament/ament_lint/blob/jazzy/ament_lint_auto/doc/index.rst)
225258
- [ament_lint repo](https://github.com/ament/ament_lint/tree/jazzy) (all the available linters for ROS 2 packages)
226259
- [ament_lint CLI utilities](https://docs.ros.org/en/jazzy/Tutorials/Advanced/Ament-Lint-For-Clean-Code.html)

modules/module_6/README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,11 @@ Integrating CI into the workflow is essential because it:
4848

4949
For projects hosted on GitHub, the easiest and most popular way to implement CI is with **GitHub Actions**.
5050

51+
> [!TIP]
52+
> **Use Pre-commit Hooks to Optimize Your Workflow**
53+
>
54+
> While **CI is the mandatory quality gate** for merging, it's often faster for developers to catch simple style errors **locally** before they push. Tools like **pre-commit hooks** (as discussed in Module 1) run fast checks like formatting locally, saving the developer time waiting for the CI pipeline to run just to fail on a style inconsistency. They complement the CI by ensuring your commits are clean and focused on functional changes.
55+
5156
### Introduction to GitHub Actions
5257

5358
GitHub Actions is a CI/CD platform built directly into GitHub. Automation workflows are defined in a **YAML file** in a special directory in the repository: `.github/workflows/`. GitHub automatically detects these files and runs them based on a set of custom-defined rules or triggers, such as when code is pushed, a pull request is opened, or a scheduled job is due.

0 commit comments

Comments
 (0)