From 27efdf476fae766dd2e5a471d7dc67e7c4a04934 Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Mon, 7 Dec 2020 12:00:14 +0100 Subject: [PATCH 1/3] feat: support symlinked hook files & shellcheck improvements --- .husky/commit-msg | 6 ++++-- .husky/pre-commit | 4 +++- scripts/pre-commit | 2 +- src/commands/add.ts | 4 +++- test/default.sh | 6 ++++-- test/subdir.sh | 2 +- 6 files changed, 16 insertions(+), 8 deletions(-) diff --git a/.husky/commit-msg b/.husky/commit-msg index 4f15872e3..475759ce1 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,4 +1,6 @@ #!/bin/sh -. "$(dirname $0)/_/husky.sh" -npx --no-install commitlint --edit $1 +# shellcheck source=./_/husky.sh +. "$(dirname "$(readlink -f "$0")")/_/husky.sh" + +npx --no-install commitlint --edit "$1" diff --git a/.husky/pre-commit b/.husky/pre-commit index 84166368b..81e71de1d 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,4 +1,6 @@ #!/bin/sh -. "$(dirname $0)/_/husky.sh" + +# shellcheck source=./_/husky.sh +. "$(dirname "$(readlink -f "$0")")/_/husky.sh" npm test diff --git a/scripts/pre-commit b/scripts/pre-commit index aa41cd39a..0500d3701 100755 --- a/scripts/pre-commit +++ b/scripts/pre-commit @@ -4,7 +4,7 @@ echo 'husky - installing git hooks...' mkdir -p "$husky_dir/_" echo "_" > "$husky_dir/.gitignore" -cp "$(dirname $0)/husky.sh" "$husky_dir/_" +cp "$(dirname "$0")/husky.sh" "$husky_dir/_" git config core.hooksPath "$husky_dir" echo 'husky - done' diff --git a/src/commands/add.ts b/src/commands/add.ts index 911eb035e..730077e08 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -24,7 +24,9 @@ function createHookFile(cwd: string, hookName: string, cmd: string) { const data = [ '#!/bin/sh', - '. "$(dirname $0)/_/husky.sh"', + '', + '# shellcheck source=./_/husky.sh', + '. "$(dirname "$(readlink -f "$0")")/_/husky.sh"', '', cmd, ].join('\n') diff --git a/test/default.sh b/test/default.sh index aaae5cc1f..f06585d8f 100644 --- a/test/default.sh +++ b/test/default.sh @@ -1,4 +1,7 @@ -. $(dirname $0)/_functions.sh +#!/bin/bash + +# shellcheck source=./_functions.sh +. "$(dirname "$0")/_functions.sh" title "default" @@ -31,4 +34,3 @@ git commit -m "should fail" || echo -e "\e[0;32mOK\e[m" # Uninstall npx --no-install husky uninstall git config core.hooksPath || echo -e "\e[0;32mOK\e[m" - diff --git a/test/subdir.sh b/test/subdir.sh index 501bda582..3410e71a2 100644 --- a/test/subdir.sh +++ b/test/subdir.sh @@ -1,4 +1,4 @@ -. $(dirname $0)/_functions.sh +. $(dirname "$0")/_functions.sh title "subdir" From 3cbcfb53d364ead4a39611e2892720bd2165073a Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Tue, 26 Jan 2021 07:27:52 +0100 Subject: [PATCH 2/3] fix: implement requested changes --- .husky/commit-msg | 2 +- .husky/pre-commit | 2 +- src/commands/add.ts | 7 +------ test/config-dir.sh | 7 +++++-- test/default.sh | 2 +- 5 files changed, 9 insertions(+), 11 deletions(-) diff --git a/.husky/commit-msg b/.husky/commit-msg index 475759ce1..567ff71f0 100755 --- a/.husky/commit-msg +++ b/.husky/commit-msg @@ -1,6 +1,6 @@ #!/bin/sh # shellcheck source=./_/husky.sh -. "$(dirname "$(readlink -f "$0")")/_/husky.sh" +. "$(dirname "$0")/_/husky.sh" npx --no-install commitlint --edit "$1" diff --git a/.husky/pre-commit b/.husky/pre-commit index 81e71de1d..7d0a96b9c 100755 --- a/.husky/pre-commit +++ b/.husky/pre-commit @@ -1,6 +1,6 @@ #!/bin/sh # shellcheck source=./_/husky.sh -. "$(dirname "$(readlink -f "$0")")/_/husky.sh" +. "$(dirname "$0")/_/husky.sh" npm test diff --git a/src/commands/add.ts b/src/commands/add.ts index 8e84e39df..161fb90ea 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -15,12 +15,7 @@ function createHookFile(file: string, cmd: string) { throw new Error(`${file} already exists`) } - const data = [ - '#!/bin/sh', - '# shellcheck source=./_/husky.sh', - '. "$(dirname "$(readlink -f "$0")")/_/husky.sh"', - cmd, - ].join('\n') + const data = ['#!/bin/sh', '. "$(dirname "$0")/_/husky.sh"', cmd].join('\n') fs.writeFileSync(file, data, 'utf-8') // Show "./file" instead of just "file" diff --git a/test/config-dir.sh b/test/config-dir.sh index f916139be..a040983bb 100644 --- a/test/config-dir.sh +++ b/test/config-dir.sh @@ -1,6 +1,9 @@ -. $(dirname $0)/_functions.sh +# shellcheck shell=bash -# Example: +# shellcheck source=./_functions.sh +. "$(dirname "$0")/_functions.sh" + +# Example: # .config/husky title "config directory" tempDir="/tmp/husky-config-dir-test" diff --git a/test/default.sh b/test/default.sh index 1c43d4b99..882aad6f9 100644 --- a/test/default.sh +++ b/test/default.sh @@ -1,4 +1,4 @@ -#!/bin/bash +# shellcheck shell=bash # shellcheck source=./_functions.sh . "$(dirname "$0")/_functions.sh" From eab14baedb3948da9099cf37a19bbd23059b8fce Mon Sep 17 00:00:00 2001 From: Jonas Thelemann Date: Tue, 26 Jan 2021 07:32:10 +0100 Subject: [PATCH 3/3] fix(add): add missing empty quotes --- src/commands/add.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/commands/add.ts b/src/commands/add.ts index 161fb90ea..bbe6ef79c 100644 --- a/src/commands/add.ts +++ b/src/commands/add.ts @@ -15,7 +15,7 @@ function createHookFile(file: string, cmd: string) { throw new Error(`${file} already exists`) } - const data = ['#!/bin/sh', '. "$(dirname "$0")/_/husky.sh"', cmd].join('\n') + const data = ['#!/bin/sh', '. "$(dirname "$0")/_/husky.sh"', '', cmd].join('\n') fs.writeFileSync(file, data, 'utf-8') // Show "./file" instead of just "file"