Skip to content
Merged
Show file tree
Hide file tree
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
48 changes: 41 additions & 7 deletions .github/PRE_COMMIT_HOOK.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,42 @@ This repository uses a pre-commit hook to ensure code quality and prevent broken

The pre-commit hook automatically runs the following checks before allowing any commit:

1. **Code Formatting** (`make format`)
1. **Badge Updates and README Check** (`make update-badges` and `make readme-check`)

- Automatically updates README badges when problem files are modified
- Ensures README.md is up to date with current problem counts
- Automatically stages updated README.md if badges are modified

2. **Code Formatting** (`make format`)

- Formats C++ files using `clang-format`
- Formats Python files using `ruff` (or `black` as fallback)
- Automatically stages formatted files if changes are made

2. **Code Linting** (`make lint`)
3. **Code Linting** (`make lint`)

- Lints C++ files using `clang-tidy`
- Lints Python files using `ruff` (or `flake8` as fallback)

3. **Tests** (`make test:all`)
4. **Tests** (`make test:all`)
- Runs all C++ tests using Google Test
- Runs all Python tests using pytest

## Installation

The pre-commit hook is already installed in this repository. If you're setting up a new clone, the hook should work automatically.
The pre-commit hook needs to be installed after cloning the repository. To install the hook, run:

```bash
make install-hooks
```

This will copy the pre-commit hook from the `hooks/` directory to `.git/hooks/` and make it executable.

To uninstall the hooks (if needed), run:

```bash
make uninstall-hooks
```

## Manual Testing

Expand All @@ -45,6 +63,18 @@ git commit --no-verify -m "your commit message"

## Troubleshooting

### Hook fails on badge updates

- The hook automatically updates badges when problem files are modified
- If badge update fails, check that you have Python 3 installed and the required dependencies:
```bash
pip install -r requirements.txt
```
- Ensure the `scripts/update_badges.py` script is executable and working:
```bash
make update-badges
```

### Hook fails on formatting

- The hook automatically fixes formatting issues and stages the corrected files
Expand Down Expand Up @@ -98,13 +128,17 @@ sudo apt-get install libgtest-dev

The pre-commit hook uses the existing Makefile targets. To modify the behavior:

1. **Formatting rules**: Modify the `format-cpp` and `format-python` targets in `Makefile`
2. **Linting rules**: Modify the `lint-cpp` and `lint-python` targets in `Makefile`
3. **Test configuration**: Modify the `test:all` target in `Makefile`
1. **Badge updates**: Modify the `update-badges` target in `Makefile` or the `scripts/update_badges.py` script
2. **README checks**: Modify the `readme-check` and `readme` targets in `Makefile`
3. **Formatting rules**: Modify the `format-cpp` and `format-python` targets in `Makefile`
4. **Linting rules**: Modify the `lint-cpp` and `lint-python` targets in `Makefile`
5. **Test configuration**: Modify the `test:all` target in `Makefile`

## Benefits

- **Up-to-date documentation**: README badges are automatically updated when problems are added or modified
- **Consistent code style**: All code follows the same formatting standards
- **Early error detection**: Linting catches potential issues before they reach the repository
- **Reliable codebase**: Tests ensure that new changes don't break existing functionality
- **Team productivity**: Reduces time spent on code review for style and basic issues
- **Automated maintenance**: No need for separate CI workflows for badge updates
50 changes: 0 additions & 50 deletions .github/workflows/update-badges.yml

This file was deleted.

21 changes: 21 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,25 @@ debug-gtest:
fi; \
fi

# =============================================================================
# Git Hooks
# =============================================================================

# Install Git hooks
.PHONY: install-hooks
install-hooks: ## Install Git hooks for code quality
@echo "Installing Git hooks..."
@cp hooks/pre-commit .git/hooks/pre-commit
@chmod +x .git/hooks/pre-commit
@echo "$(call color_green,Git hooks installed successfully.)"

# Uninstall Git hooks
.PHONY: uninstall-hooks
uninstall-hooks: ## Uninstall Git hooks
@echo "Uninstalling Git hooks..."
@rm -f .git/hooks/pre-commit
@echo "$(call color_green,Git hooks uninstalled.)"

# =============================================================================
# Help and Documentation
# =============================================================================
Expand Down Expand Up @@ -357,6 +376,8 @@ help:
@echo " readme - Generate README from problem configurations"
@echo " readme-check - Check if README is up to date"
@echo " update-badges - Update README badges with current problem counts"
@echo " install-hooks - Install Git hooks for code quality"
@echo " uninstall-hooks - Uninstall Git hooks"
@echo " debug-gtest - Debug Google Test configuration"
@echo " help - Show this help message"
@echo ""
Expand Down
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
### 📊 Repository Stats

[![Last Commit](https://img.shields.io/github/last-commit/mathusanm6/LeetCode?style=for-the-badge&logo=git&logoColor=white&color=blue)](https://github.com/mathusanm6/LeetCode/commits/main)
[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-3-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-3-blue?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![C++ Solutions](https://img.shields.io/badge/C%2B%2B%20Solutions-4-blue?style=for-the-badge&logo=cplusplus&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)
[![Python Solutions](https://img.shields.io/badge/Python%20Solutions-4-blue?style=for-the-badge&logo=python&logoColor=white)](https://github.com/mathusanm6/LeetCode/tree/main/problems)

</div>

Expand Down Expand Up @@ -184,7 +184,7 @@ This repository covers a comprehensive range of algorithmic patterns and data st
| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|-------|----------|------|-------|------------|-----|------|
| 1 | [Two Sum](https://leetcode.com/problems/two-sum/) | [Python](./problems/two_sum/two_sum.py), [C++](./problems/two_sum/two_sum.cc) | _O(n)_ | _O(n)_ | Easy | | |
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/description/) | [Python](./problems/contains_duplicate/contains_duplicate.py), [C++](./problems/contains_duplicate/contains_duplicate.cc) | _O(n)_ | _O(n)_ | Easy | | |
| 217 | [Contains Duplicate](https://leetcode.com/problems/contains-duplicate/) | [Python](./problems/contains_duplicate/contains_duplicate.py), [C++](./problems/contains_duplicate/contains_duplicate.cc) | _O(n)_ | _O(n)_ | Easy | | |

## Two Pointers

Expand All @@ -196,4 +196,4 @@ This repository covers a comprehensive range of algorithmic patterns and data st

| # | Title | Solution | Time | Space | Difficulty | Tag | Note |
|---|-------|----------|------|-------|------------|-----|------|
| 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/description/) | [Python](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.py), [C++](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.cc) | _O(n)_ | _O(1)_ | Hard | | _n_ is the number of nodes in the binary tree. |
| 2313 | [Minimum Flips in Binary Tree to Get Result](https://leetcode.com/problems/minimum-flips-in-binary-tree-to-get-result/) | [Python](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.py), [C++](./problems/minimum_flips_in_binary_tree_to_get_result/minimum_flips_in_binary_tree_to_get_result.cc) | _O(n)_ | _O(1)_ | Hard | | _n_ is the number of nodes in the binary tree. |
41 changes: 41 additions & 0 deletions hooks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
# Git Hooks

This directory contains Git hooks that can be installed to maintain code quality.

## Available Hooks

### pre-commit

The pre-commit hook ensures code quality by running the following checks before allowing any commit:

1. **Badge Updates and README Check** - Automatically updates README badges when problem files are modified
2. **Code Formatting** - Formats C++ and Python files using clang-format and ruff
3. **Code Linting** - Lints C++ and Python files using clang-tidy and ruff
4. **Tests** - Runs all tests to ensure code functionality

## Installation

To install the hooks, run:

```bash
make install-hooks
```

To uninstall the hooks, run:

```bash
make uninstall-hooks
```

## Manual Installation

If you prefer to install manually:

```bash
cp hooks/pre-commit .git/hooks/pre-commit
chmod +x .git/hooks/pre-commit
```

## More Information

See [`../.github/PRE_COMMIT_HOOK.md`](../.github/PRE_COMMIT_HOOK.md) for detailed documentation about the pre-commit hook functionality and troubleshooting.
Loading