-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refine our
pre-commit
git hook (#233)
* 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
1 parent
01524b6
commit 874b6ef
Showing
3 changed files
with
102 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}`); |