-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Export ${STAGED_FILES} for pre-commit hooks ⭐
- Loading branch information
Showing
8 changed files
with
199 additions
and
6 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
#!/bin/sh | ||
# shellcheck disable=SC2153 | ||
if [ -n "${STAGED_FILES}" ]; then | ||
echo "* Staged files:" | ||
|
||
for STAGED_FILE in $STAGED_FILES; do | ||
echo " - $STAGED_FILE" | ||
done | ||
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
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
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,36 @@ | ||
#!/bin/sh | ||
# Test: | ||
# Direct template execution: list of staged files (simple) | ||
|
||
mkdir -p /tmp/test095/.githooks/pre-commit && | ||
cd /tmp/test095 && git init || | ||
exit 1 | ||
|
||
echo "Test" >>sample.txt | ||
echo "Test" >>second.txt | ||
|
||
cat <<EOF >.githooks/pre-commit/print-changes | ||
for STAGED in \${STAGED_FILES}; do | ||
echo "staged: \${STAGED}" >> /tmp/test095.out | ||
done | ||
EOF | ||
|
||
git add sample.txt second.txt | ||
|
||
HOOK_NAME=pre-commit HOOK_FOLDER=$(pwd)/.git/hooks ACCEPT_CHANGES=A \ | ||
sh /var/lib/githooks/base-template.sh | ||
|
||
if ! grep 'staged: sample.txt' /tmp/test095.out; then | ||
echo "! Failed to find expected output (1)" | ||
exit 1 | ||
fi | ||
|
||
if ! grep 'staged: second.txt' /tmp/test095.out; then | ||
echo "! Failed to find expected output (2)" | ||
exit 1 | ||
fi | ||
|
||
if grep -vE '(sample|second)\.txt' /tmp/test095.out; then | ||
echo "! Unexpected additional output" | ||
exit 1 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
#!/bin/sh | ||
# Test: | ||
# Direct template execution: list of staged files (special paths) | ||
|
||
mkdir -p /tmp/test096/.githooks/pre-commit && | ||
cd /tmp/test096 && git init || | ||
exit 1 | ||
|
||
mkdir -p sub/folder\ with\ space/x | ||
printf "Test" >>sub/test.txt | ||
printf "Test" >>sub/folder\ with\ space/test.txt | ||
printf "Test" >>sub/folder\ with\ space/x/test.txt | ||
printf "Test" >>file\ with\ spaces.txt | ||
|
||
cat <<EOF >.githooks/pre-commit/print-changes | ||
IFS=" | ||
" | ||
for STAGED in \${STAGED_FILES}; do | ||
echo ">\${STAGED}< "\$(cat "\${STAGED}" | wc -c) >> /tmp/test096.out | ||
done | ||
EOF | ||
|
||
git add sub f* | ||
|
||
HOOK_NAME=pre-commit HOOK_FOLDER=$(pwd)/.git/hooks ACCEPT_CHANGES=A \ | ||
sh /var/lib/githooks/base-template.sh | ||
|
||
if [ "$(wc -l </tmp/test096.out)" != "4" ]; then | ||
echo "! Unexpected number of output rows" | ||
exit 1 | ||
fi | ||
|
||
if ! grep '>sub/folder with space/x/test.txt< 4' /tmp/test096.out; then | ||
echo "! Failed to find expected output" | ||
exit 1 | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
#!/bin/sh | ||
# Test: | ||
# Direct template execution: list of staged files (hook types) | ||
|
||
MANAGED_HOOK_NAMES=" | ||
applypatch-msg pre-applypatch post-applypatch | ||
pre-commit prepare-commit-msg commit-msg post-commit | ||
pre-rebase post-checkout post-merge pre-push | ||
pre-receive update post-receive post-update | ||
push-to-checkout pre-auto-gc post-rewrite sendemail-validate | ||
" | ||
|
||
mkdir -p /tmp/test097/.git/hooks && | ||
cd /tmp/test097 && | ||
git init && | ||
sh /var/lib/githooks/install.sh --single && | ||
git config githooks.autoupdate.enabled N || | ||
exit 1 | ||
|
||
for HOOK_TYPE in ${MANAGED_HOOK_NAMES}; do | ||
mkdir -p ".githooks/${HOOK_TYPE}" || exit 1 | ||
|
||
cat <<EOF >".githooks/${HOOK_TYPE}/${HOOK_TYPE}" || exit 1 | ||
printf "hook=\$(basename \$0) -- " >> /tmp/test097.out | ||
for STAGED in \${STAGED_FILES}; do | ||
echo "\${STAGED}" >> /tmp/test097.out | ||
done | ||
EOF | ||
done | ||
|
||
echo "test" >testing.txt | ||
git add testing.txt | ||
|
||
ACCEPT_CHANGES=A git commit -m 'testing hooks' | ||
|
||
cat /tmp/test097.out | ||
|
||
if [ "$(grep -c "testing.txt" /tmp/test097.out)" != "3" ]; then | ||
echo "! Unexpected number of output rows" | ||
exit 1 | ||
fi | ||
|
||
if [ "$(grep -c "hook=" /tmp/test097.out)" != "4" ]; then | ||
echo "! Unexpected number of hooks run" | ||
exit 1 | ||
fi |