BLOG-145 Add pre-commit hooks #147

Merged
squid merged 5 commits from BLOG-145_add_pre_commit_hooks into main 2025-10-15 13:11:26 +08:00
Owner

Description

  • READMEs are also updated!

Package Changes

No response

Screenshots

No response

Reference

Resolves #145.

Checklist

  • A milestone is set
  • The related issuse has been linked to this branch
### Description - READMEs are also updated! ### Package Changes _No response_ ### Screenshots _No response_ ### Reference Resolves #145. ### Checklist - [x] A milestone is set - [x] The related issuse has been linked to this branch
squid added this to the 0.4 milestone 2025-10-15 12:53:37 +08:00
squid added 2 commits 2025-10-15 12:53:37 +08:00
docs: update README files to enhance development setup instructions and add frontend documentation
All checks were successful
Frontend CI / build (push) Successful in 1m37s
Auto Comment On PR / add_improve_comment (pull_request) Successful in 17s
PR Title Check / pr-title-check (pull_request) Successful in 17s
60e608001c
Collaborator

/improve

/improve
Collaborator

PR Code Suggestions

CategorySuggestion                                                                                                                                    Impact
Possible issue
Validate and stage generated SQLx data

Modify the sqlx-prepare.sh script to ensure that the sqlx-data.json file is
up-to-date and staged. If the cargo sqlx prepare --workspace command modifies
sqlx-data.json but it's not staged, the hook should fail, prompting the developer to
stage the changes. This prevents committing an outdated sqlx-data.json file.

script/sqlx-prepare.sh [1-6]

 #!/bin/sh
 
 SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
 
 cd "$SCRIPT_DIR/../backend"
+
+# Generate sqlx-data.json
 cargo sqlx prepare --workspace
 
+# Check if sqlx-data.json was modified and is not staged
+if ! git diff --quiet sqlx-data.json; then
+  echo "Error: sqlx-data.json was modified by 'cargo sqlx prepare' but is not staged."
+  echo "Please run 'git add backend/sqlx-data.json' and try again."
+  exit 1
+fi
+
Suggestion importance[1-10]: 8

__

Why: This suggestion addresses a potential issue where sqlx-data.json might be modified but not staged, leading to an outdated file in the repository. The proposed git diff check and error handling significantly improve the pre-commit hook's reliability.

Medium
General
Standardize hook language for scripts

For consistency and robustness, consider changing the language for custom shell
scripts from system to script. This ensures the hooks are executed via a shell
interpreter, avoiding potential issues if the scripts lack execute permissions.

.pre-commit-config.yaml [11-18]

 ...
       - id: backend-check
         name: backend check
-        language: system
+        language: script
         entry: ./script/backend-check.sh
         pass_filenames: false
       - id: frontend-lint
         name: frontend lint
-        language: system
+        language: script
         entry: ./script/frontend-lint.sh
         pass_filenames: false
Suggestion importance[1-10]: 7

__

Why: Changing language: system to language: script for custom shell scripts in .pre-commit-config.yaml is a good practice that improves robustness and consistency, ensuring scripts are executed correctly via a shell interpreter.

Medium
Align setup instructions for consistency

For consistency, update the "Full Project Setup" section to refer to
frontend/README.md for detailed frontend setup, similar to how it references
backend/README.md. This ensures users are directed to the most comprehensive
instructions for both parts of the project.

README.md [75-76]

 ...
 3. Set up backend (see backend/README.md)
-4. Set up frontend (see above)
+4. Set up frontend (see frontend/README.md)
 5. Start both servers for full-stack development
Suggestion importance[1-10]: 6

__

Why: Updating the README.md to consistently refer to frontend/README.md for detailed setup instructions, similar to the backend, enhances documentation clarity and user experience. It's a good improvement for maintainability.

Low
## PR Code Suggestions ✨ <!-- --> <table><thead><tr><td><strong>Category</strong></td><td align=left><strong>Suggestion&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; </strong></td><td align=center><strong>Impact</strong></td></tr><tbody><tr><td rowspan=1>Possible issue</td> <td> <details><summary>Validate and stage generated SQLx data</summary> ___ **Modify the <code>sqlx-prepare.sh</code> script to ensure that the <code>sqlx-data.json</code> file is <br>up-to-date and staged. If the <code>cargo sqlx prepare --workspace</code> command modifies <br><code>sqlx-data.json</code> but it's not staged, the hook should fail, prompting the developer to <br>stage the changes. This prevents committing an outdated <code>sqlx-data.json</code> file.** [script/sqlx-prepare.sh [1-6]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/script/sqlx-prepare.sh#L1-L6) ```diff #!/bin/sh SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) cd "$SCRIPT_DIR/../backend" + +# Generate sqlx-data.json cargo sqlx prepare --workspace +# Check if sqlx-data.json was modified and is not staged +if ! git diff --quiet sqlx-data.json; then + echo "Error: sqlx-data.json was modified by 'cargo sqlx prepare' but is not staged." + echo "Please run 'git add backend/sqlx-data.json' and try again." + exit 1 +fi + ``` <details><summary>Suggestion importance[1-10]: 8</summary> __ Why: This suggestion addresses a potential issue where `sqlx-data.json` might be modified but not staged, leading to an outdated file in the repository. The proposed `git diff` check and error handling significantly improve the pre-commit hook's reliability. </details></details></td><td align=center>Medium </td></tr><tr><td rowspan=2>General</td> <td> <details><summary>Standardize hook language for scripts</summary> ___ **For consistency and robustness, consider changing the <code>language</code> for custom shell <br>scripts from <code>system</code> to <code>script</code>. This ensures the hooks are executed via a shell <br>interpreter, avoiding potential issues if the scripts lack execute permissions.** [.pre-commit-config.yaml [11-18]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/.pre-commit-config.yaml#L11-L18) ```diff ... - id: backend-check name: backend check - language: system + language: script entry: ./script/backend-check.sh pass_filenames: false - id: frontend-lint name: frontend lint - language: system + language: script entry: ./script/frontend-lint.sh pass_filenames: false ``` <details><summary>Suggestion importance[1-10]: 7</summary> __ Why: Changing `language: system` to `language: script` for custom shell scripts in `.pre-commit-config.yaml` is a good practice that improves robustness and consistency, ensuring scripts are executed correctly via a shell interpreter. </details></details></td><td align=center>Medium </td></tr><tr><td> <details><summary>Align setup instructions for consistency</summary> ___ **For consistency, update the "Full Project Setup" section to refer to <br><code>frontend/README.md</code> for detailed frontend setup, similar to how it references <br><code>backend/README.md</code>. This ensures users are directed to the most comprehensive <br>instructions for both parts of the project.** [README.md [75-76]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/README.md#L75-L76) ```diff ... 3. Set up backend (see backend/README.md) -4. Set up frontend (see above) +4. Set up frontend (see frontend/README.md) 5. Start both servers for full-stack development ``` <details><summary>Suggestion importance[1-10]: 6</summary> __ Why: Updating the `README.md` to consistently refer to `frontend/README.md` for detailed setup instructions, similar to the backend, enhances documentation clarity and user experience. It's a good improvement for maintainability. </details></details></td><td align=center>Low </td></tr></tr></tbody></table>
squid added 3 commits 2025-10-15 13:03:59 +08:00
Author
Owner

For consistency, update the "Full Project Setup" section to refer to
frontend/README.md for detailed frontend setup, similar to how it references
backend/README.md. This ensures users are directed to the most comprehensive
instructions for both parts of the project.

README.md [75-76]

 ...
 3. Set up backend (see backend/README.md)
-4. Set up frontend (see above)
+4. Set up frontend (see frontend/README.md)
 5. Start both servers for full-stack development

Addressed in feaddd2fed.

> **For consistency, update the "Full Project Setup" section to refer to <br><code>frontend/README.md</code> for detailed frontend setup, similar to how it references <br><code>backend/README.md</code>. This ensures users are directed to the most comprehensive <br>instructions for both parts of the project.** > > [README.md [75-76]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/README.md#L75-L76) > > ```diff > ... > 3. Set up backend (see backend/README.md) > -4. Set up frontend (see above) > +4. Set up frontend (see frontend/README.md) > 5. Start both servers for full-stack development > ``` Addressed in feaddd2fedf8e7c07ad4f51f108745c89f30b7dd.
Author
Owner

Modify the sqlx-prepare.sh script to ensure that the sqlx-data.json file is
up-to-date and staged. If the cargo sqlx prepare --workspace command modifies
sqlx-data.json but it's not staged, the hook should fail, prompting the developer to
stage the changes. This prevents committing an outdated sqlx-data.json file.

script/sqlx-prepare.sh [1-6]

 #!/bin/sh
 
 SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd)
 
 cd "$SCRIPT_DIR/../backend"
+
+# Generate sqlx-data.json
 cargo sqlx prepare --workspace
 
+# Check if sqlx-data.json was modified and is not staged
+if ! git diff --quiet sqlx-data.json; then
+  echo "Error: sqlx-data.json was modified by 'cargo sqlx prepare' but is not staged."
+  echo "Please run 'git add backend/sqlx-data.json' and try again."
+  exit 1
+fi
+

Addressed in 1d8a864ad4.

> **Modify the <code>sqlx-prepare.sh</code> script to ensure that the <code>sqlx-data.json</code> file is <br>up-to-date and staged. If the <code>cargo sqlx prepare --workspace</code> command modifies <br><code>sqlx-data.json</code> but it's not staged, the hook should fail, prompting the developer to <br>stage the changes. This prevents committing an outdated <code>sqlx-data.json</code> file.** > > [script/sqlx-prepare.sh [1-6]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/script/sqlx-prepare.sh#L1-L6) > > ```diff > #!/bin/sh > > SCRIPT_DIR=$(CDPATH= cd -- "$(dirname -- "$0")" && pwd) > > cd "$SCRIPT_DIR/../backend" > + > +# Generate sqlx-data.json > cargo sqlx prepare --workspace > > +# Check if sqlx-data.json was modified and is not staged > +if ! git diff --quiet sqlx-data.json; then > + echo "Error: sqlx-data.json was modified by 'cargo sqlx prepare' but is not staged." > + echo "Please run 'git add backend/sqlx-data.json' and try again." > + exit 1 > +fi > + > ``` Addressed in 1d8a864ad406f5c2847ab641e0feae06035438b3.
Author
Owner

For consistency and robustness, consider changing the language for custom shell
scripts from system to script. This ensures the hooks are executed via a shell
interpreter, avoiding potential issues if the scripts lack execute permissions.

.pre-commit-config.yaml [11-18]

 ...
       - id: backend-check
         name: backend check
-        language: system
+        language: script
         entry: ./script/backend-check.sh
         pass_filenames: false
       - id: frontend-lint
         name: frontend lint
-        language: system
+        language: script
         entry: ./script/frontend-lint.sh
         pass_filenames: false

Addressed in 65a2b0a6d6.

> **For consistency and robustness, consider changing the <code>language</code> for custom shell <br>scripts from <code>system</code> to <code>script</code>. This ensures the hooks are executed via a shell <br>interpreter, avoiding potential issues if the scripts lack execute permissions.** > > [.pre-commit-config.yaml [11-18]](https://git.squidspirit.com/squid/blog/src/branch/BLOG-145_add_pre_commit_hooks/.pre-commit-config.yaml#L11-L18) > > ```diff > ... > - id: backend-check > name: backend check > - language: system > + language: script > entry: ./script/backend-check.sh > pass_filenames: false > - id: frontend-lint > name: frontend lint > - language: system > + language: script > entry: ./script/frontend-lint.sh > pass_filenames: false > ``` Addressed in 65a2b0a6d69b39971850c54fabf2e64accc90d6b.
squid merged commit d1f085b255 into main 2025-10-15 13:11:26 +08:00
squid deleted branch BLOG-145_add_pre_commit_hooks 2025-10-15 13:11:26 +08:00
Sign in to join this conversation.
No description provided.