Skip to content

Commit

Permalink
fix(install): do not fail if not inside a Git directory
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Feb 8, 2021
1 parent 4e6fe7d commit f8f1afe
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 3 deletions.
2 changes: 1 addition & 1 deletion .husky/.gitignore
Original file line number Diff line number Diff line change
@@ -1 +1 @@
_
_
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
"prepublish": "pinst --disable",
"postpublish": "pinst --enable",
"pretest": "npm run build --silent && npm pack --silent",
"test": "sh ./test/default.sh && sh ./test/sub-dir.sh && sh ./test/config-dir.sh",
"test": "sh ./test/default.sh && sh ./test/sub-dir.sh && sh ./test/config-dir.sh && sh ./test/not-git-dir.sh",
"posttest": "rm husky-*.tgz",
"commit": "commit"
},
Expand Down
13 changes: 12 additions & 1 deletion src/commands/install.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,13 @@ import path from 'path'
import cp from 'child_process'

export function install(dir = '.husky'): void {
// Ensure that we're inside a git repository
if (cp.spawnSync('git', ['rev-parse']).status !== 0) {
// Do not fail to let projects downloaded as zip files have their dependencies installed
console.log('husky - not a Git repository, skipping hooks installation')
return
}

// Ensure that we're not trying to install outside cwd
const absoluteHooksDir = path.resolve(process.cwd(), dir)
if (!absoluteHooksDir.startsWith(process.cwd())) {
Expand All @@ -27,7 +34,11 @@ export function install(dir = '.husky'): void {
path.join(dir, '_/husky.sh'),
)

cp.spawnSync('git', ['config', 'core.hooksPath', dir])
// Configure repo
const { error } = cp.spawnSync('git', ['config', 'core.hooksPath', dir])
if (error) {
throw error
}
} catch (e) {
console.log('husky - Git hooks failed to install')
throw e
Expand Down
13 changes: 13 additions & 0 deletions test/not-git-dir.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# shellcheck shell=bash

# shellcheck source=./_functions.sh
. "$(dirname "$0")/_functions.sh"

title "not git dir"
tempDir="/tmp/husky-not-git-dir"

rm -rf $tempDir
cd_and_install_tgz $tempDir

# Should not fail
npx --no-install husky install && echo -e "\e[0;32mOK\e[m"

0 comments on commit f8f1afe

Please sign in to comment.