Skip to content

Commit

Permalink
Add pre-commit hook + clang-format GHA
Browse files Browse the repository at this point in the history
  • Loading branch information
lhearachel committed Jul 6, 2024
1 parent 36045f7 commit 8714748
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 10 deletions.
9 changes: 9 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#!/bin/sh
# Redirect output to stderr.
exec 1>&2

# Run clang-format on staged files; abort the commit if any files are changed
if ! git clang-format ; then
echo "linting made changes to source files; aborting commit"
exit 1
fi
30 changes: 30 additions & 0 deletions .github/workflows/pr-lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
name: pr-lint
on:
pull_request:
branches: [main]
paths: ['**.c', '**.h']

jobs:
pr-lint:
runs-on: ubuntu-latest

permissions:
contents: read
pull-requests: read

steps:
- uses: actions/checkout@v4

- uses: cpp-linter/cpp-linter-action@v2
id: linter
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
style: 'file' # Use repository .clang-format file
tidy-checks: '-*' # Disable clang-tidy checks
version: '18'
files-changed-only: false # Github returns error code 406 if more than 300 files are changed in a PR

- name: fail fast
if: steps.linter.outputs.clang-format-checks-failed > 0
run: exit 1
26 changes: 16 additions & 10 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
# Contributing to pret/pokeplatinum

This document provides a synopsis and loose guidelines for how to contribute to this project. It is a work in progress. Maintainers should expand this document.

## Contents
<!--toc:start-->
- [Editor Enhancements](#editor-enhancements)
- [Code Formatting](#code-formatting)
<!--toc:end-->

This document provides a synopsis and loose guidelines for how to contribute to this project. It is a work in progress. Maintainers should expand this document.

<a href="editor-enhancements"></a>
## Editor Enhancements


This repository includes a script to generate a `compile_commands.json` that is compatible with C language servers such as `clangd`.

### Requirements
Expand All @@ -25,20 +26,25 @@ This repository includes a script to generate a `compile_commands.json` that is

This will create a file named `compile_commands.json` in the project root, overwriting the previous copy.

<a href="code-formatting"></a>
## Code Formatting

This repository includes an opinionated `clang-format` specification which is integrated into the build system for convenience in ensuring that your code adheres to repository style guidelines.
This repository includes an opinionated `clang-format` specification to ensure that we maintain a common code style. For convenience, a pre-commit hook is also provided in `.githooks` which will run `clang-format` against any staged changes prior to executing a commit.

### Requirements

- `clang-format`
- `clang-format@17` or newer

### Usage

```bash
./build.sh format
To set up the pre-commit hook:

```sh
git config --local core.hooksPath .githooks/
```

This will traverse the source tree and format all found C sources and headers according to the specified style rules.
To run the formatter on the full source tree:

```bash
./format.sh
```

0 comments on commit 8714748

Please sign in to comment.