Skip to content

Commit

Permalink
show error if --absolute-git-dir isn't supported
Browse files Browse the repository at this point in the history
  • Loading branch information
typicode committed Jun 27, 2019
1 parent b60a1a9 commit ca57293
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
22 changes: 6 additions & 16 deletions package-lock.json

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

4 changes: 2 additions & 2 deletions src/installer/__tests__/gitRevParse.ts
Expand Up @@ -5,12 +5,12 @@ import gitRevParse from '../gitRevParse'
const root = path.join(__dirname, '../../..')

describe('gitRevParse', (): void => {
it('should return topLevel and gitDir', (): void => {
it('should return topLevel and absoluteGitDir', (): void => {
expect(gitRevParse()).toStrictEqual({
// Git rev-parse uses a different separator on Linux/MacOS and Windows
// slash is used to normalized the returned value for tests
topLevel: slash(root),
gitDir: slash(path.join(root, '.git'))
absoluteGitDir: slash(path.join(root, '.git'))
})
})
})
8 changes: 4 additions & 4 deletions src/installer/bin.ts
Expand Up @@ -21,17 +21,17 @@ try {
)

// Get top level and git dir
const { topLevel, gitDir } = gitRevParse()
const { topLevel, absoluteGitDir } = gitRevParse()

// Debug
debug(`topLevel: ${topLevel}`)
debug(`gitDir: ${gitDir}`)
debug(`gitDir: ${absoluteGitDir}`)

// Install or uninstall
if (action === 'install') {
install(topLevel, gitDir, huskyDir, isCI)
install(topLevel, absoluteGitDir, huskyDir, isCI)
} else {
uninstall(gitDir, huskyDir)
uninstall(absoluteGitDir, huskyDir)
}
} catch (error) {
console.log(error.message.trim())
Expand Down
27 changes: 18 additions & 9 deletions src/installer/gitRevParse.ts
Expand Up @@ -3,22 +3,31 @@ import execa from 'execa'

export default function(): {
topLevel: string
gitDir: string
absoluteGitDir: string
} {
let result
try {
const { stdout } = execa.sync('git', [
result = execa.sync('git', [
'rev-parse',
'--show-toplevel',
'--absolute-git-dir'
])

const [topLevel, gitDir] = stdout
.trim()
.split('\n')
// Normalize for Windows
.map(slash)
return { topLevel, gitDir }
} catch (error) {
throw new Error(error.stderr)
}

const [topLevel, absoluteGitDir] = result.stdout
.trim()
.split('\n')
// Normalize for Windows
.map(slash)

// Git rev-parse returns unknown options as is.
// If we get --absolute-git-dir in the output,
// it probably means that an older version of Git has been used.
if (absoluteGitDir === '--absolute-git-dir') {
throw new Error('Husky requires Git >= 2.13.2, please updade Git')
}

return { topLevel, absoluteGitDir }
}

0 comments on commit ca57293

Please sign in to comment.