Skip to content

Commit efc3789

Browse files
committed
Handle missing Git in repo checks
1 parent 7b9999e commit efc3789

2 files changed

Lines changed: 31 additions & 2 deletions

File tree

packages/cli-kit/src/public/node/git.test.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -382,6 +382,16 @@ describe('ensurePresentOrAbort()', () => {
382382
})
383383

384384
describe('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+
/Git is necessary in the environment to continue/,
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

402412
describe('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)

packages/cli-kit/src/public/node/git.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -318,9 +318,11 @@ export class OutsideGitDirectoryError extends AbortError {}
318318
* @param directory - The directory to check.
319319
*/
320320
export 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
*/
334336
export 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

338344
export 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+
397407
async function withGit<T>(
398408
{
399409
directory,

0 commit comments

Comments
 (0)