Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: typicode/husky
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v9.0.10
Choose a base ref
...
head repository: typicode/husky
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v9.0.11
Choose a head ref
  • 5 commits
  • 10 files changed
  • 2 contributors

Commits on Feb 4, 2024

  1. Copy the full SHA
    d1a8ed1 View commit details
  2. docs: update how-to.md

    typicode authored Feb 4, 2024
    Copy the full SHA
    d2e831d View commit details
  3. docs: update how-to.md

    typicode authored Feb 4, 2024
    Copy the full SHA
    095a4fe View commit details

Commits on Feb 13, 2024

  1. fix: husky=0 in init (#1395)

    * refactor: husky and add tests
    
    * style: indent
    typicode authored Feb 13, 2024
    Copy the full SHA
    29056db View commit details
  2. 9.0.11

    typicode committed Feb 13, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    a9c6584 View commit details
Showing with 89 additions and 46 deletions.
  1. +5 −2 docs/how-to.md
  2. +2 −3 husky
  3. +3 −3 package-lock.json
  4. +1 −1 package.json
  5. +4 −2 test.sh
  6. 0 test/{8_time.sh → 10_time.sh}
  7. +15 −0 test/7_set_u.sh
  8. +22 −0 test/8_husky_0.sh
  9. +1 −0 test/{7_init.sh → 9_init.sh}
  10. +36 −35 test/functions.sh
7 changes: 5 additions & 2 deletions docs/how-to.md
Original file line number Diff line number Diff line change
@@ -8,6 +8,8 @@ Husky allows you to execute local commands before running hooks. It reads comman
- `~/.config/husky/init.sh`
- `~/.huskyrc` (deprecated)

On Windows: `C:\Users\yourusername\.config\husky\init.sh`

## Skipping Git Hooks

### For a Single Command
@@ -223,7 +225,7 @@ Run the `husky` command once in your repo. Ideally, include it in the `prepare`
}
```

```json [pnpm]
```json [bun]
{
"scripts": {
"prepare": "husky" // [!code hl]
@@ -246,7 +248,8 @@ pnpm run prepare
```

```sh [yarn]
yarn run prepare
# Yarn doesn't support `prepare`
yarn run postinstall
```

```sh [bun]
5 changes: 2 additions & 3 deletions husky
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#!/usr/bin/env sh
H="$HUSKY"
[ "$H" = "2" ] && set -x
[ "$HUSKY" = "2" ] && set -x
h="${0##*/}"
s="${0%/*/*}/$h"

@@ -11,7 +10,7 @@ for f in "${XDG_CONFIG_HOME:-$HOME/.config}/husky/init.sh" "$HOME/.huskyrc"; do
[ -f "$f" ] && . "$f"
done

[ "$H" = "0" ] && exit 0
[ "${HUSKY-}" = "0" ] && exit 0

sh -e "$s" "$@"
c=$?
6 changes: 3 additions & 3 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "husky",
"version": "9.0.10",
"version": "9.0.11",
"description": "Modern native Git hooks",
"keywords": [
"git",
6 changes: 4 additions & 2 deletions test.sh
Original file line number Diff line number Diff line change
@@ -8,5 +8,7 @@ sh test/3_from-sub-dir.sh
sh test/4_not-git-dir.sh
sh test/5_git_command_not_found.sh
sh test/6_command_not_found.sh
sh test/7_init.sh
# sh test/8_time.sh
sh test/7_set_u.sh
sh test/8_husky_0.sh
sh test/9_init.sh
sh test/10_time.sh
File renamed without changes.
15 changes: 15 additions & 0 deletions test/7_set_u.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#!/bin/sh
. test/functions.sh
setup
install

npx --no-install husky
expect_hooksPath_to_be ".husky/_"

git add package.json
echo "echo \"pre-commit\"" >.husky/pre-commit

# Should not fail if set -u is used
mkdir -p config/husky
echo "set -u" > config/husky/init.sh
XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo"
22 changes: 22 additions & 0 deletions test/8_husky_0.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/sh
. test/functions.sh
setup
install

# Should not setup hooks when HUSKY=0
HUSKY=0 npx --no-install husky
expect_hooksPath_to_be ""

# Should setup hooks
npx --no-install husky
expect_hooksPath_to_be ".husky/_"

# Should not commit
git add package.json
echo "echo \"pre-commit\" && exit 1" >.husky/pre-commit
expect 1 "git commit -m foo"

# Should commit when HUSKY=0
mkdir -p config/husky
echo "export HUSKY=0" > config/husky/init.sh
XDG_CONFIG_HOME="$(pwd)/config" expect 0 "git commit -m foo"
1 change: 1 addition & 0 deletions test/7_init.sh → test/9_init.sh
Original file line number Diff line number Diff line change
@@ -3,4 +3,5 @@
setup
install

# Test init command
expect 0 "npx --no-install husky init"
71 changes: 36 additions & 35 deletions test/functions.sh
Original file line number Diff line number Diff line change
@@ -2,54 +2,55 @@
set -eu

setup() {
name="$(basename -- "$0")"
testDir="/tmp/husky-test-$name"
echo
echo "-------------------"
echo "+ $name"
echo "-------------------"
echo

# Create test directory
rm -rf "$testDir"
mkdir -p "$testDir"
cd "$testDir"

# Init git
git init --quiet
git config user.email "test@test"
git config user.name "test"

# Init package.json
npm_config_loglevel="error" npm init -y 1>/dev/null
name="$(basename -- "$0")"
testDir="/tmp/husky-test-$name"
echo
echo "-------------------"
echo "+ $name"
echo "-------------------"
echo

# Create test directory
rm -rf "$testDir"
mkdir -p "$testDir"
cd "$testDir"

# Init git
git init --quiet
git config user.email "test@test"
git config user.name "test"

# Init package.json
npm_config_loglevel="error" npm init -y 1>/dev/null
}

install() {
npm install ../husky.tgz
npm install ../husky.tgz
}

expect() {
set +e
sh -c "$2"
exitCode="$?"
set -e
if [ $exitCode != "$1" ]; then
error "expect command \"$2\" to exit with code $1 (got $exitCode)"
fi
set +e
sh -c "$2"
exitCode="$?"
set -e
if [ $exitCode != "$1" ]; then
error "expect command \"$2\" to exit with code $1 (got $exitCode)"
fi
}

expect_hooksPath_to_be() {
hooksPath=$(git config core.hooksPath)
if [ "$hooksPath" != "$1" ]; then
error "core.hooksPath should be $1, was $hooksPath"
fi
set +e
hooksPath=$(git config core.hooksPath)
if [ "$hooksPath" != "$1" ]; then
error "core.hooksPath should be $1, was $hooksPath"
fi
}

error() {
echo -e "\e[0;31mERROR:\e[m $1"
exit 1
echo -e "\e[0;31mERROR:\e[m $1"
exit 1
}

ok() {
echo -e "\e[0;32mOK\e[m"
echo -e "\e[0;32mOK\e[m"
}