@@ -84,26 +84,34 @@ export const installHooks = ({
8484 }
8585
8686 // Check Git before touching the filesystem so non-repositories have no side effects.
87- const repository = runGit ( cwd , [ 'rev-parse' , '--is-inside-work-tree' ] ) ;
87+ const repository = runGit ( cwd , [ 'rev-parse' , '--is-inside-work-tree' , '--show-prefix' ] ) ;
8888 if ( repository . error || repository . status === null ) {
8989 return gitFailure ( repository . error , repository . stderr ) ;
9090 }
91- if ( repository . status !== 0 || repository . stdout . trim ( ) !== 'true' ) {
91+
92+ const firstLineEnd = repository . stdout . indexOf ( '\n' ) ;
93+ const insideWorkTree = (
94+ firstLineEnd === - 1 ? repository . stdout : repository . stdout . slice ( 0 , firstLineEnd )
95+ ) . trim ( ) ;
96+
97+ if ( repository . status !== 0 ) {
98+ if ( insideWorkTree === 'true' ) {
99+ return fail (
100+ 'git-command-failed' ,
101+ `Failed to resolve the Git repository prefix: ${ repository . stderr . trim ( ) } ` ,
102+ ) ;
103+ }
92104 return { status : 'skipped' , reason : 'not-git-repository' } ;
93105 }
94106
95- const prefixResult = runGit ( cwd , [ 'rev-parse' , '--show-prefix' ] ) ;
96- if ( prefixResult . error || prefixResult . status === null ) {
97- return gitFailure ( prefixResult . error , prefixResult . stderr ) ;
98- }
99- if ( prefixResult . status !== 0 ) {
100- return fail (
101- 'git-command-failed' ,
102- `Failed to resolve the Git repository prefix: ${ prefixResult . stderr . trim ( ) } ` ,
103- ) ;
107+ if ( insideWorkTree !== 'true' ) {
108+ return { status : 'skipped' , reason : 'not-git-repository' } ;
104109 }
105110
106- const prefix = removeLineEnding ( prefixResult . stdout ) . replaceAll ( '\\' , '/' ) ;
111+ const prefix =
112+ firstLineEnd === - 1
113+ ? ''
114+ : removeLineEnding ( repository . stdout . slice ( firstLineEnd + 1 ) ) . replaceAll ( '\\' , '/' ) ;
107115 const hooksPath = `${ prefix } ${ resolvedDir } /_` ;
108116
109117 const config = runGit ( cwd , [ 'config' , '--local' , '--get' , 'core.hooksPath' ] ) ;
0 commit comments