An open API service indexing awesome lists of open source software.

https://github.com/djeada/git-hooks

Welcome to this collection of useful Git hooks! In this repository, you will find a variety of scripts written in Bash and Python that you can use to improve your workflow and ensure that your code meets certain standards.
https://github.com/djeada/git-hooks

Last synced: 10 months ago
JSON representation

Welcome to this collection of useful Git hooks! In this repository, you will find a variety of scripts written in Bash and Python that you can use to improve your workflow and ensure that your code meets certain standards.

Awesome Lists containing this project

README

          

# Git-Hooks
Welcome to this collection of useful Git hooks! In this repository, you will find a variety of scripts written in Bash and Python that you can use to improve your workflow and ensure that your code meets certain standards.

![Capture](https://user-images.githubusercontent.com/37275728/186116675-44002347-b151-4da6-a0f5-edd5f199d73b.PNG)

## Set Up for Unix, MacOS

1. Download the code from the repository:

```Bash
git clone https://github.com/djeada/Git-Hooks.git
cd Git-Hooks
```

2. Install modules via VENV:

```Bash
virtualenv env
source env/bin/activate
pip install -r requirements.txt
```

3. Run any script from src directory:

```Bash
python src/example_script.py
```

## Understanding Hooks

When you explore a well-structured repository, you'll come across a `hooks` folder. Git itself includes a set of hooks that can execute scripts in various scenarios, such as before pushing your code to the repository, after pulling from the remote, or before creating a new commit.

## Pre-commit

Pre-commit scripts are executed when the `git commit` command is used. These scripts are often employed to run tasks that verify your code conforms to the coding style adopted by your development team.

For instance, Python projects may use tools like `black` and `flake8` to ensure code adheres to established standards. If your code doesn't meet these standards, the tools will prevent you from creating a new commit and modify your code accordingly.

You can then review your code, confirm everything is in order, and attempt to commit again. This time, the commit should be successful.

Although you can run tests using pre-commit, they typically take more than a few seconds to execute. Therefore, we generally advise against running tests during pre-commit.

## Implementing a Custom Hook

To use a custom hook, create a file named pre-commit in the `.git/hooks` directory. The filename is essential, as Git will only recognize your script if it's named accordingly. Edit the pre-commit file and add the commands you want Git to execute whenever you attempt to make a commit. For this repository, we use the following script:

```Bash
#!/usr/bin/env bash
hooks/_run_all.sh
```

Don't forget to grant execution permissions to the pre-commit script!

## Available scripts

| Script | Description | Python | Bash |
|:------:|:-----------:|:------:|:----:|
| remove diacritics | Removes diacritics from every file in a given directory. | remove_diacritics.py | remove_diacritics.sh |
| remove carriage return | Removes carriage returns from every file in a given directory. | remove_carriage_returns.py | remove_carriage_returns.sh |
| remove trailing whitespace | Removes trailing whitespaces from every file in a given directory. | remove_trailing_whitespaces.py | remove_trailing_whitespaces.sh |
| last line empty | Ensures that every file a given directory ends with a newline. | last_line_empty.py | last_line_empty.sh |
| no binaries | Checks if there are any binaries in the staging area. | no_binaries.py | no_binaries.sh |
| correct file names | Ensures that no filename has spaces in it. | correct_file_names.py | correct_file_names.sh
| correct docstrings | Unify formatting in Python docstrings. | correct_docstrings.py | correct_docstrings.sh |
| python formatter | Beautify and format every python file found in the current repository. | python_format.py | python_format.sh |
| cpp formatter | Beautify and format every cpp file found in the current repository. | cpp_format.py | cpp_format.sh |
| shell formatter | Beautify and format every shell script found in the current repository. | shell_format.py | shell_format.sh |

## Refrences

* https://www.git-scm.com/docs/githooks
* https://githooks.com/
* https://tbaggery.com/2008/04/19/a-note-about-git-commit-messages.html

## Contributing
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate.

## License
[MIT](https://choosealicense.com/licenses/mit/)