Skip to content

Commit 342a309

Browse files
committed
x function
1 parent f031ca8 commit 342a309

File tree

4 files changed

+97
-17
lines changed

4 files changed

+97
-17
lines changed

husky

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,23 @@ for f in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc"; do
99
# shellcheck disable=SC1090
1010
[ -f "$f" ] && . "$f"
1111
done
12-
1312
[ "${HUSKY-}" = "0" ] && exit 0
1413

15-
sh -e "$s" "$@"
16-
c=$?
14+
f=$([ "$h" = "pre-commit" ] && git diff --cached --name-only --diff-filter=ACMR | sed 's| |\\ |g')
15+
x() {
16+
[ -z "$f" ] && return
17+
g=$(echo "$f" | grep "$1")
18+
echo "$g" | xargs "$2"
19+
echo "$g" | xargs git add
20+
}
21+
22+
h() {
23+
[ $c != 0 ] && echo "husky - $h script failed (code $c)"
24+
[ $c = 127 ] && echo "husky - command not found in PATH=$PATH"
25+
exit 1
26+
}
27+
28+
trap 'c=$?; h' EXIT
1729

18-
[ $c != 0 ] && echo "husky - $h script failed (code $c)"
19-
[ $c = 127 ] && echo "husky - command not found in PATH=$PATH"
20-
exit $c
30+
set -e
31+
. "$s"

test.sh

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22
# To run tests, type ./test.sh in your terminal
33
set -e
44
npm pack && mv husky-*.tgz /tmp/husky.tgz
5-
sh test/1_default.sh
6-
sh test/2_in-sub-dir.sh
7-
sh test/3_from-sub-dir.sh
8-
sh test/4_not-git-dir.sh
9-
sh test/5_git_command_not_found.sh
10-
sh test/6_command_not_found.sh
11-
sh test/7_set_u.sh
12-
sh test/8_husky_0.sh
13-
sh test/9_init.sh
14-
sh test/10_time.sh
5+
# sh test/1_default.sh
6+
# sh test/2_in-sub-dir.sh
7+
# sh test/3_from-sub-dir.sh
8+
# sh test/4_not-git-dir.sh
9+
# sh test/5_git_command_not_found.sh
10+
# sh test/6_command_not_found.sh
11+
# sh test/7_set_u.sh
12+
# sh test/8_husky_0.sh
13+
# sh test/9_init.sh
14+
# sh test/10_time.sh
15+
sh test/11_x.sh

test/11_x.sh

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
#!/bin/sh
2+
. test/functions.sh
3+
setup
4+
install
5+
6+
npx --no-install husky
7+
8+
# Test core.hooksPath
9+
expect_hooksPath_to_be ".husky/_"
10+
11+
# Test pre-commit
12+
git add package.json
13+
echo -n one > one.js
14+
echo -n two > two.js
15+
echo -n three > three.js
16+
echo -n "four" > "fo ur.js" # Test file with space
17+
18+
git add one.js two.js "fo ur.js" package.json
19+
20+
cat >.husky/replace_content <<EOL
21+
#!/bin/sh
22+
for f in "\$@"; do
23+
echo "replace \$f"
24+
echo " ok" >> "\$f"
25+
done
26+
EOL
27+
chmod +x .husky/replace_content
28+
29+
cat >.husky/pre-commit <<EOL
30+
x "\.js$" .husky/replace_content
31+
EOL
32+
33+
git commit -m foo
34+
35+
# One
36+
if grep -q "one ok" one.js; then
37+
ok "one.js has been modified"
38+
else
39+
error "one.js has not been modified"
40+
fi
41+
42+
# Two
43+
if grep -q "two ok" two.js; then
44+
ok "two.js has been modified"
45+
else
46+
error "two.js has not been modified"
47+
fi
48+
49+
# Three
50+
if grep -q "^three$" three.js; then
51+
ok "three.js has not been modified"
52+
else
53+
error "three.js has been modified"
54+
fi
55+
56+
# Four
57+
if grep -q "four ok" "fo ur.js"; then
58+
ok "four.js has been modified"
59+
else
60+
error "four.js has not been modified"
61+
fi
62+
63+
# Package.json
64+
if grep -q "ok" package.json; then
65+
error "package.json has been modified"
66+
else
67+
ok "package.json has not been modified"
68+
fi

test/functions.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,5 +52,5 @@ error() {
5252
}
5353

5454
ok() {
55-
echo -e "\e[0;32mOK\e[m"
55+
echo -e "\e[0;32mOK\e[m $1"
5656
}

0 commit comments

Comments
 (0)