File tree Expand file tree Collapse file tree
packages/cli-kit/src/public/node Expand file tree Collapse file tree Original file line number Diff line number Diff line change @@ -382,6 +382,16 @@ describe('ensurePresentOrAbort()', () => {
382382} )
383383
384384describe ( 'ensureInsideGitDirectory()' , ( ) => {
385+ test ( 'throws a friendly error if git is not present' , async ( ) => {
386+ // Given
387+ vi . mocked ( hasGit ) . mockResolvedValue ( false )
388+
389+ // Then
390+ await expect ( ( ) => git . ensureInsideGitDirectory ( ) ) . rejects . toThrowError (
391+ / G i t i s n e c e s s a r y i n t h e e n v i r o n m e n t t o c o n t i n u e / ,
392+ )
393+ } )
394+
385395 test ( 'throws an error if not inside a git directory' , async ( ) => {
386396 // Given
387397 mockedCheckIsRepo . mockResolvedValue ( false )
@@ -400,6 +410,15 @@ describe('ensureInsideGitDirectory()', () => {
400410} )
401411
402412describe ( 'insideGitDirectory()' , ( ) => {
413+ test ( 'returns false if git is not present' , async ( ) => {
414+ // Given
415+ vi . mocked ( hasGit ) . mockResolvedValue ( false )
416+
417+ // Then
418+ await expect ( git . insideGitDirectory ( ) ) . resolves . toBe ( false )
419+ expect ( simpleGit ) . not . toHaveBeenCalled ( )
420+ } )
421+
403422 test ( 'returns true if inside a git directory' , async ( ) => {
404423 // Given
405424 mockedCheckIsRepo . mockResolvedValue ( true )
Original file line number Diff line number Diff line change @@ -318,9 +318,11 @@ export class OutsideGitDirectoryError extends AbortError {}
318318 * @param directory - The directory to check.
319319 */
320320export async function ensureInsideGitDirectory ( directory ?: string ) : Promise < void > {
321+ await ensureGitIsPresentOrAbort ( )
322+
321323 // eslint-disable-next-line @typescript-eslint/ban-ts-comment
322324 // @ts -ignore
323- if ( ! ( await insideGitDirectory ( directory ) ) ) {
325+ if ( ! ( await checkIfInsideGitDirectory ( directory ) ) ) {
324326 throw new OutsideGitDirectoryError ( `${ outputToken . path ( directory || cwd ( ) ) } is not a Git directory` )
325327 }
326328}
@@ -332,7 +334,11 @@ export async function ensureInsideGitDirectory(directory?: string): Promise<void
332334 * @returns True if the directory is inside a .git directory tree.
333335 */
334336export async function insideGitDirectory ( directory ?: string ) : Promise < boolean > {
335- return withGit ( { directory} , ( repo ) => repo . checkIsRepo ( ) )
337+ if ( ! ( await hasGit ( ) ) ) {
338+ return false
339+ }
340+
341+ return checkIfInsideGitDirectory ( directory )
336342}
337343
338344export class GitDirectoryNotCleanError extends AbortError { }
@@ -394,6 +400,10 @@ export async function removeGitRemote(directory: string, remoteName = 'origin'):
394400 } )
395401}
396402
403+ async function checkIfInsideGitDirectory ( directory ?: string ) : Promise < boolean > {
404+ return withGit ( { directory} , ( repo ) => repo . checkIsRepo ( ) )
405+ }
406+
397407async function withGit < T > (
398408 {
399409 directory,
You can’t perform that action at this time.
0 commit comments