From 170b8acc65eb95ec8056219d8fd06c37ee56ea4b Mon Sep 17 00:00:00 2001 From: Hugo Persson Date: Wed, 10 Jan 2024 20:36:10 +0100 Subject: [PATCH] Feature/Add git pre-commit hook for yarn format (#2840) * Set up git pre-commit hook for yarn format * Update changelog --- CHANGELOG.md | 1 + README.md | 1 + git-hooks/pre-commit | 26 ++++++++++++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100755 git-hooks/pre-commit diff --git a/CHANGELOG.md b/CHANGELOG.md index aa8361aac..817041403 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -37,6 +37,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Added a hint for _Time-Weighted Rate of Return_ (TWR) to the portfolio summary tab on the home page - Added support for REST APIs (`JSON`) via the scraper configuration - Enabled the _Redis_ authentication in the `docker-compose` files +- Set up a git-hook to format the code before any commit ### Changed diff --git a/README.md b/README.md index d128f7948..dd580dfc9 100644 --- a/README.md +++ b/README.md @@ -160,6 +160,7 @@ Ghostfolio is available for various home server systems, including [Runtipi](htt 1. Run `yarn install` 1. Run `docker compose --env-file ./.env -f docker/docker-compose.dev.yml up -d` to start [PostgreSQL](https://www.postgresql.org) and [Redis](https://redis.io) 1. Run `yarn database:setup` to initialize the database schema +1. Run `git config core.hooksPath ./git-hooks/` to setup git hooks 1. Start the server and the client (see [_Development_](#Development)) 1. Open http://localhost:4200/en in your browser 1. Create a new user via _Get Started_ (this first user will get the role `ADMIN`) diff --git a/git-hooks/pre-commit b/git-hooks/pre-commit new file mode 100755 index 000000000..673b3ba68 --- /dev/null +++ b/git-hooks/pre-commit @@ -0,0 +1,26 @@ +#!/bin/bash + +# Will check if "yarn format" is run before executing. +# Called by "git commit" with no arguments. The hook should +# exit with non-zero status after issuing an appropriate message if +# it wants to stop the commit. + +echo "Running yarn format" + +# Run the command and loop over its output +FILES_TO_STAGE="" +i=0 +while IFS= read -r line; do + # Process each line here + ((i++)) + if [ $i -le 2 ]; then + continue + fi + if [[ $line == Done* ]]; then + break + fi + FILES_TO_STAGE="$FILES_TO_STAGE $line" + +done < <(yarn format ) +git add $FILES_TO_STAGE +echo "Files formatted. Committing..."