Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add code formatting tools and GitHub workflow #12

Merged
merged 9 commits into from
Mar 2, 2025
Merged
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
9 changes: 9 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
---
Language: Cpp
BasedOnStyle: LLVM
IndentWidth: 4 # 4 spaces for indentation
UseTab: Never # Always use spaces, never tabs

BreakBeforeBraces: Attach # Puts braces on the same line

ColumnLimit: 80 # Wrap lines longer than 80 characters
5 changes: 5 additions & 0 deletions .github/CODEOWNERS
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/src/ @shua1090
/include/ @shua1090
/lib/ @shua1090

/src/vitals_node/ @Torrey0
22 changes: 22 additions & 0 deletions .github/workflows/check-format.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Check Code Formatting

on:
pull_request:
branches: [ main ]

jobs:
check-format:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
submodules: recursive

- name: Install clang-format
run: |
sudo apt-get update
sudo apt-get install -y clang-format
- name: Check formatting
run: ./check_format.sh
26 changes: 26 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,31 @@
# PROVE Lab - Mila (Electric Endurance Vehicle)
![Formatting Workflow](https://github.com/PROVELab/ProveMila-embedded/actions/workflows/check-format/badge.svg)

Mila is an electric sports car with the goal of going 1000 miles on a single charge. This is the GitHub repository for the Low Voltage system. It's built on [PlatformIO](https://platformio.org/).

## Project Structure
The `src` folder contains the source code for all the embedded components of the system. Inside the `src` folder, there are folders for each node of the system and a `common` folder for shared code and drivers. See the READMEs within each folder for further information about each node.

## Formatting
To format all c/cpp files in the repo (src and include), run:
```
./format.sh
```
This will not affect lib (since it's external libraries)

### Git Pre-Commit Hook
A pre-commit hook is available to automatically check and apply formatting before each commit. To install the hook, run:
```
./install-hooks.sh
```

This will:
1. Format your C/C++ code automatically before each commit
2. Re-stage the formatted files so changes aren't lost
3. Allow commits to proceed with a warning if clang-format isn't installed

#### Installing clang-format
The formatting tools require clang-format to be installed on your system:
- **macOS**: `brew install clang-format`
- **Ubuntu/Debian**: `sudo apt-get install clang-format`
- **Windows with Chocolatey**: `choco install llvm`
19 changes: 19 additions & 0 deletions check_format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#!/bin/bash

# Assert clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo "clang-format could not be found in your PATH"
echo "Please install clang-format and try again"
exit 1
fi

# Find files
files=$(find src/ include/ -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cpp")

# Check formatting (without modifying files)
if ! echo "$files" | xargs clang-format --dry-run -Werror; then
echo "Formatting issues detected! Please run ./format.sh to fix them."
exit 1 # Ensures GitHub Actions fails on error
fi

echo "All files are properly formatted."
12 changes: 12 additions & 0 deletions format.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# Assert clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo "clang-format could not be found in your PATH"
echo "Please install clang-format and try again"
exit 1
fi

# Format all h/hpp/c/cpp files in the project
find src/ -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cpp" | xargs clang-format -i
find include/ -name "*.h" -o -name "*.hpp" -o -name "*.c" -o -name "*.cpp" | xargs clang-format -i

echo "Formatted all files in the project"
46 changes: 46 additions & 0 deletions hooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Check if clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo -e "${YELLOW}Warning: clang-format is not installed.${NC}"
echo -e "${YELLOW}Your code will not be automatically formatted.${NC}"
echo -e "${YELLOW}To install clang-format:${NC}"
echo -e "${YELLOW} - macOS: brew install clang-format${NC}"
echo -e "${YELLOW} - Ubuntu/Debian: sudo apt-get install clang-format${NC}"
echo -e "${YELLOW} - Windows with Chocolatey: choco install llvm${NC}"
echo -e "${YELLOW}Commit will proceed, but please consider installing clang-format.${NC}"
exit 0
fi

# Get list of staged files that are C/C++ files
staged_files=$(git diff --cached --name-only --diff-filter=ACM | grep -E '\.(c|cpp|h|hpp)$')

# If there are no C/C++ files staged, exit early
if [ -z "$staged_files" ]; then
echo -e "${GREEN}No C/C++ files to format.${NC}"
exit 0
fi

echo -e "${GREEN}Running format.sh to format your code...${NC}"
./format.sh

# Check if formatting would change any files
echo -e "${GREEN}Checking if formatting is correct...${NC}"
if ! ./check_format.sh > /dev/null 2>&1; then
# If formatting failed, add the changes and show what was changed
echo -e "${YELLOW}Automatically fixing formatting issues...${NC}"

# Add the changes back to staging
echo "$staged_files" | xargs git add

echo -e "${GREEN}Formatting issues fixed and changes staged.${NC}"
fi

echo -e "${GREEN}Pre-commit hook completed successfully!${NC}"
exit 0
38 changes: 38 additions & 0 deletions install-hooks.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash

# Colors for output
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m' # No Color

# Make sure the hooks directory exists
mkdir -p hooks

# Make the pre-commit hook executable
chmod +x hooks/pre-commit

# Get the git hooks directory
HOOKS_DIR=$(git rev-parse --git-path hooks)

# Create a symbolic link from the git hooks directory to our pre-commit hook
if [ -f "$HOOKS_DIR/pre-commit" ]; then
echo -e "${YELLOW}A pre-commit hook already exists. Backing it up.${NC}"
mv "$HOOKS_DIR/pre-commit" "$HOOKS_DIR/pre-commit.backup"
fi

# Create symbolic link
ln -sf "$(pwd)/hooks/pre-commit" "$HOOKS_DIR/pre-commit"

echo -e "${GREEN}Git pre-commit hook installed successfully!${NC}"
echo -e "${GREEN}The hook will automatically format your code before each commit.${NC}"
echo -e "${YELLOW}Note: If you don't have clang-format installed, the hook will warn you but allow commits to proceed.${NC}"

# Check if clang-format is installed
if ! command -v clang-format &> /dev/null; then
echo -e "${YELLOW}Warning: clang-format is not installed on your system.${NC}"
echo -e "${YELLOW}To install clang-format:${NC}"
echo -e "${YELLOW} - macOS: brew install clang-format${NC}"
echo -e "${YELLOW} - Ubuntu/Debian: sudo apt-get install clang-format${NC}"
echo -e "${YELLOW} - Windows with Chocolatey: choco install llvm${NC}"
fi
7 changes: 2 additions & 5 deletions src/mcu_node/main.cpp
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
#include <mbed.h>

int main()
{
while(1)
{

int main() {
while (1) {
}
}
14 changes: 4 additions & 10 deletions src/telemetry_node/main.cpp
Original file line number Diff line number Diff line change
@@ -4,15 +4,9 @@
// GITHUB NOTES
// We should convert PROVELab GitHub to an organization
// and make Low Voltage leads owners
// Set permissions for the main branch to require pull requests approved by leads + anyone else
// Succession: new leads are added as owners
// Set permissions for the main branch to require pull requests approved by
// leads + anyone else Succession: new leads are added as owners

void setup()
{
void setup() {}

}

void loop()
{

}
void loop() {}