kylelhk / Pictionary

CITS5505 Agile Web Development - Group Project
MIT License
1 stars 0 forks source link

Issue #61: Add pre-commit hook to avoid code style inconsistencies #62

Closed iheathers closed 2 months ago

iheathers commented 2 months ago

Change Summary

  1. Installed Pre-commit Package:

    • Purpose: To enforce consistent code style across the team despite different local code formatters being used by team members.
    • Benefit: Ensures code consistency, especially useful when working with a large team.
  2. Removed .editorconfig:

    • Reason: The .editorconfig file was not effective and was difficult to set up.
  3. Configured Black as the Code Formatter:

    • Used Black as the code formatter within the pre-commit hooks to ensure uniform code formatting. Black is a PEP 8 compliant opinionated formatter. https://peps.python.org/pep-0008/
  4. Reformatted All Files:

    • Reformatted the entire codebase using Black to align with the enforced code style.

    Issue: #61

Here's a step-by-step guide on how to set up and use pre-commit, starting from installing the dependencies listed in requirements.txt and including instructions for team members.


Setting Up Pre-commit Hooks

Follow these steps to set up and use pre-commit hooks for your project once you merge this PR

1. Install Dependencies

First, ensure all the dependencies, including pre-commit, are installed. Run the following command in your terminal:

pip install -r requirements.txt

4. Install Pre-commit Hooks

To install the pre-commit hooks defined in the configuration file, run the following. This step is a must. While doing pip install -r requirements.txt, we are just downloading the package. The following step register the rule for the project

pre-commit install

This will set up the hooks to run automatically on git commit.

5. Stage Your Changes

Before committing, you need to stage the files you want to include in the commit. You can do this using the git add command. For example, to stage all modified files, run:

git add .

2. Commit Your Changes

Once the files are staged, commit your changes using the git commit command. Pre-commit hooks will automatically run at this stage.

git commit -m "Your descriptive commit message"

3. Handle Pre-commit Hook Failures

Since, local formatting might be different than that enforced by PEP8, you might see error message like the one below. The best part is, the files are already reformatted. You don't have to save them again or format them manually. It's already done for you.

image

The formatting is done. so, you will have to add the changes again by running following. Make sure you run the following again, otherwise, you will see the following error

git add .
image

4. Retry Committing

After adding the files to staging by running git add . , commit again by running following.

git commit -m "Your descriptive commit message"

5. Push Your Changes

Once the commit is successful, you can push your changes to the remote repository:

git push origin your-branch-name

Change Form

Other Information