From 9f87b007642a7b45e7d327f42a37ebeda187e26c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Sat, 22 Jul 2023 12:48:42 +0100 Subject: [PATCH 1/7] Add pre-commit hook --- pre-commit-hook.sh | 14 ++++++++++++++ uni/README.md | 9 +++++++++ 2 files changed, 23 insertions(+) create mode 100644 pre-commit-hook.sh diff --git a/pre-commit-hook.sh b/pre-commit-hook.sh new file mode 100644 index 000000000..ee0bb41f5 --- /dev/null +++ b/pre-commit-hook.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +tee .git/hooks/pre-commit << EOF +#!/bin/sh + +FILES="\$(git diff --name-only --cached | grep .*\.dart )" + + + +echo "\$FILES" | xargs dart format +echo "\$FILES" | xargs git add +EOF + +chmod +x .git/hooks/pre-commit \ No newline at end of file diff --git a/uni/README.md b/uni/README.md index ab4b56a28..b44cf3d3c 100644 --- a/uni/README.md +++ b/uni/README.md @@ -16,6 +16,15 @@ The token is read from the file assets/env/env.json, which you may need to creat } ``` +### Automated formatting + +In order to contribute, you must format you files using `dart format` manually or formatting on save using your IDE automatically, or you can install the git pre-commit hook doing the following command at the root of the directory that automatically formats staged files: + +``` bash + chmod +x pre-commit-hook.sh && ./pre-commit-hook.sh +``` + + ## Project structure ### Overview From fc4929563004b6ac4e3e5a10ed399f741bbffd22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Sat, 22 Jul 2023 20:23:53 +0100 Subject: [PATCH 2/7] Remove generated dart files from the pre commit hook --- pre-commit-hook.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pre-commit-hook.sh b/pre-commit-hook.sh index ee0bb41f5..88318e1dc 100644 --- a/pre-commit-hook.sh +++ b/pre-commit-hook.sh @@ -3,7 +3,7 @@ tee .git/hooks/pre-commit << EOF #!/bin/sh -FILES="\$(git diff --name-only --cached | grep .*\.dart )" +FILES="\$(git diff --name-only --cached | grep .*\.dart | grep -v .*\.g\.dart)" From bc94bbf4d8b87a903605a3cec271b48c842733ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Wed, 26 Jul 2023 16:55:43 +0100 Subject: [PATCH 3/7] Update format_lint_test.yaml --- .github/workflows/format_lint_test.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/format_lint_test.yaml b/.github/workflows/format_lint_test.yaml index 636ccc151..1163a17aa 100644 --- a/.github/workflows/format_lint_test.yaml +++ b/.github/workflows/format_lint_test.yaml @@ -17,7 +17,7 @@ jobs: with: flutter-version: ${{ env.FLUTTER_VERSION }} - - run: dart format . --set-exit-if-changed + - run: dart format $(find . -type f -name "*.dart" -a -not -name "*.g.dart") --set-exit-if-changed lint: name: 'Lint' From 09905413c6090154e25a69bd4e64f83d1d6272b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Wed, 26 Jul 2023 17:05:45 +0100 Subject: [PATCH 4/7] Make README.md formatting clear --- uni/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/uni/README.md b/uni/README.md index b44cf3d3c..054aef52f 100644 --- a/uni/README.md +++ b/uni/README.md @@ -18,7 +18,7 @@ The token is read from the file assets/env/env.json, which you may need to creat ### Automated formatting -In order to contribute, you must format you files using `dart format` manually or formatting on save using your IDE automatically, or you can install the git pre-commit hook doing the following command at the root of the directory that automatically formats staged files: +In order to contribute, you must format your changed files using `dart format` manually or enabing _formatting on save_ using your IDE ([VSCode or IntelliJ](https://docs.flutter.dev/tools/formatting)). Alternatively, you can install the git pre-commit hook that formats your changed files when you commit, doing the following command at the **root directory of the repository**: ``` bash chmod +x pre-commit-hook.sh && ./pre-commit-hook.sh From 2fe47844b4de8b75ba97cb6e4c5123adbbd823eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Thu, 27 Jul 2023 21:21:32 +0100 Subject: [PATCH 5/7] fix executable permissions --- pre-commit-hook.sh | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 pre-commit-hook.sh diff --git a/pre-commit-hook.sh b/pre-commit-hook.sh old mode 100644 new mode 100755 From 46f5259b364d2099104dc14a966adc2b46ca439c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Thu, 27 Jul 2023 21:29:14 +0100 Subject: [PATCH 6/7] fix install script on cases that the hook directory doesn't exist --- pre-commit-hook.sh | 4 ++-- uni/README.md | 7 ++++++- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/pre-commit-hook.sh b/pre-commit-hook.sh index 88318e1dc..db570395d 100755 --- a/pre-commit-hook.sh +++ b/pre-commit-hook.sh @@ -1,5 +1,5 @@ #!/bin/sh - +mkdir -p .git/hooks #it seems that are some cases where git will not create a hook directory if someone removed the hook templates tee .git/hooks/pre-commit << EOF #!/bin/sh @@ -11,4 +11,4 @@ echo "\$FILES" | xargs dart format echo "\$FILES" | xargs git add EOF -chmod +x .git/hooks/pre-commit \ No newline at end of file +chmod +x .git/hooks/pre-commit diff --git a/uni/README.md b/uni/README.md index 054aef52f..f9d2ee3c1 100644 --- a/uni/README.md +++ b/uni/README.md @@ -24,6 +24,11 @@ In order to contribute, you must format your changed files using `dart format` m chmod +x pre-commit-hook.sh && ./pre-commit-hook.sh ``` +In order to remove it, is it as simple as running the following command, from the **root directory of the repository**: + +```bash + rm .git/hooks/pre-commit +``` ## Project structure @@ -45,4 +50,4 @@ The *view* part of the app is made of *widgets* (stateful or stateless). They ea ### Controller -The *controller* directory contains all artifacts that are not directly related to the view or the model. This includes the parsers, the networking code, the database code and the logic that handles the global state of the app. \ No newline at end of file +The *controller* directory contains all artifacts that are not directly related to the view or the model. This includes the parsers, the networking code, the database code and the logic that handles the global state of the app. From f9b8164bdf86241ba3f26aa5d7d24e7f33a754db Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Thu, 27 Jul 2023 23:25:05 +0100 Subject: [PATCH 7/7] Skip pre commit if there are no .dart files changed --- pre-commit-hook.sh | 1 + 1 file changed, 1 insertion(+) diff --git a/pre-commit-hook.sh b/pre-commit-hook.sh index db570395d..838253711 100755 --- a/pre-commit-hook.sh +++ b/pre-commit-hook.sh @@ -5,6 +5,7 @@ tee .git/hooks/pre-commit << EOF FILES="\$(git diff --name-only --cached | grep .*\.dart | grep -v .*\.g\.dart)" +[ -z "\$FILES" ] && exit 0 echo "\$FILES" | xargs dart format