Skip to content

Commit

Permalink
Refine our pre-commit git hook (#233)
Browse files Browse the repository at this point in the history
* feat: auto-set git hooks custom path

* chore: use a safer shebang

* chore: shellcheck hook file

* chore: make variables immutable

* refactor: space out warning for readability

* feat: improve hook when dealing with many files

- More efficient

The previous logic read all the content of `.remarkignore` in memory,
then loop over every lines. The new logic reads and stops when a match
has been found, making it less expensive.

- Improved

The script works better with multiple files.

Before, the warning message was displayed for each file that was not
present in the `.remarkignore` file.

Now, whatever the number of files not present in `.remarkignore`, the
script will display only one warning with the list of files that must be
added to `.remarkignore`.

* feat: auto-add missing files to `.remarkignore`

* feat: auto-stage `.remarkignore`

* chore: run logic only if necessary
  • Loading branch information
MrChocolatine authored Feb 9, 2024
1 parent 01524b6 commit 874b6ef
Show file tree
Hide file tree
Showing 3 changed files with 102 additions and 15 deletions.
86 changes: 71 additions & 15 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -1,17 +1,73 @@
#!/bin/sh
#!/usr/bin/env bash

FILE_IGNORED_PAGES=".remarkignore"
FILES_NOT_IGNORED=()
SRC_PATTERN="guides/"
changes=`git diff --cached --name-only | grep "$SRC_PATTERN"`
for translated in $changes; do
while read -r line; do
if [ "!$translated" = "$line" ]; then
exit 0
STAGED_FILES=$(git diff --cached --name-only)

readonly FILE_IGNORED_PAGES
readonly SRC_PATTERN
readonly STAGED_FILES

# No changes staged, abort
if [ -z "$STAGED_FILES" ]; then
exit 0
fi

FILES_TRANSLATED=$(echo "$STAGED_FILES" | grep "$SRC_PATTERN")
readonly FILES_TRANSLATED

if [ -n "$FILES_TRANSLATED" ]; then

# Find the files not declared in `$FILE_IGNORED_PAGES`
for translated in $FILES_TRANSLATED; do
if ! grep --quiet "$translated" "$FILE_IGNORED_PAGES"; then
FILES_NOT_IGNORED+=("$translated")
fi
done < ".remarkignore"
echo "******* WARNING *******"
echo "Check your spelling ;)"
echo You translated the file:
echo $translated
echo But it is not present in .remarkignore
echo "***********************"
done
exit 0
done

if [ ${#FILES_NOT_IGNORED[@]} -ne 0 ]; then
# Warn user about files missing from `$FILE_IGNORED_PAGES`

echo
echo "******* WARNING *******"
echo
echo "You have translated these files:"
echo

for file_path in "${FILES_NOT_IGNORED[@]}"; do
printf '%s\n' "- $file_path"
echo "!$file_path" >> $FILE_IGNORED_PAGES
done

echo
echo "But they were not present in \`$FILE_IGNORED_PAGES\`, so we have added them for you."
echo
echo "All you have to do now is:"
echo "1. To make sure entries are listed in alphabetical order"
echo "2. To stage and commit \`$FILE_IGNORED_PAGES\`"
echo
echo "***********************"
echo

exit 1

else

if ! echo "$STAGED_FILES" | grep --quiet "$FILE_IGNORED_PAGES"; then

git add "$FILE_IGNORED_PAGES"

echo
echo "******* WARNING *******"
echo
echo "\`$FILE_IGNORED_PAGES\` was not in your staged files, so we have added it for you. "
echo
echo "***********************"
echo

fi

fi

fi
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"test": "tests"
},
"scripts": {
"preinstall": "node scripts/preinstall.mjs",
"build": "ember build --environment=production",
"lint": "npm-run-all --aggregate-output --continue-on-error --parallel \"lint:!(fix)\"",
"lint:md": "remark . --frail",
Expand Down
30 changes: 30 additions & 0 deletions scripts/preinstall.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env node

import { existsSync } from 'fs';
import { exec } from 'child_process';

function printWarning(message = '') {
console.error([
'',
'**********************************************************************',
'',
message,
'',
'**********************************************************************',
'',
].join('\n'));
}

/**
* Relative to the root of the project
*/
const GIT_HOOKS_PATH='.githooks';

if (existsSync(GIT_HOOKS_PATH) === false) {

printWarning(`Error, please check the folder '${GIT_HOOKS_PATH}' exists.`);
process.exit(2);

}

exec(`git config core.hooksPath ${GIT_HOOKS_PATH}`);

0 comments on commit 874b6ef

Please sign in to comment.