Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/old-poems-speak.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@shopify/cli-kit': patch
---

Fix error reporting of cli-kit methods that try to run git when git is unavailable
19 changes: 19 additions & 0 deletions packages/cli-kit/src/public/node/git.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,16 @@ describe('ensurePresentOrAbort()', () => {
})

describe('ensureInsideGitDirectory()', () => {
test('throws a friendly error if git is not present', async () => {
// Given
vi.mocked(hasGit).mockResolvedValue(false)

// Then
await expect(() => git.ensureInsideGitDirectory()).rejects.toThrowError(
/Git is necessary in the environment to continue/,
)
})

test('throws an error if not inside a git directory', async () => {
const error = Object.assign(new Error('not a git repo'), {exitCode: 128})
mockedExeca.mockRejectedValue(error)
Expand All @@ -326,6 +336,15 @@ describe('ensureInsideGitDirectory()', () => {
})

describe('insideGitDirectory()', () => {
test('returns false if git is not present', async () => {
// Given
vi.mocked(hasGit).mockResolvedValue(false)

// Then
await expect(git.insideGitDirectory()).resolves.toBe(false)
expect(mockedExeca).not.toHaveBeenCalled()
})

test('returns true if inside a git directory', async () => {
mockGitCommand('.git')

Expand Down
12 changes: 11 additions & 1 deletion packages/cli-kit/src/public/node/git.ts
Original file line number Diff line number Diff line change
Expand Up @@ -349,7 +349,9 @@ export class OutsideGitDirectoryError extends AbortError {}
* @param directory - The directory to check.
*/
export async function ensureInsideGitDirectory(directory?: string): Promise<void> {
if (!(await insideGitDirectory(directory))) {
await ensureGitIsPresentOrAbort()

if (!(await checkIfInsideGitDirectory(directory))) {
throw new OutsideGitDirectoryError(`${outputToken.path(directory ?? cwd())} is not a Git directory`)
}
}
Expand All @@ -361,6 +363,14 @@ export async function ensureInsideGitDirectory(directory?: string): Promise<void
* @returns True if the directory is inside a .git directory tree.
*/
export async function insideGitDirectory(directory?: string): Promise<boolean> {
if (!(await hasGit())) {
return false
}

return checkIfInsideGitDirectory(directory)
}

async function checkIfInsideGitDirectory(directory?: string): Promise<boolean> {
try {
await execa('git', ['rev-parse', '--git-dir'], {cwd: directory})
return true
Expand Down
Loading