Skip to content

Commit c2f96c2

Browse files
authored
Merge pull request #5963 from Shopify/remove-remote-repo-theme-init
Remove git remote on theme init
2 parents 3498616 + db84d8b commit c2f96c2

File tree

5 files changed

+85
-4
lines changed

5 files changed

+85
-4
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"@shopify/theme": patch
3+
"@shopify/cli-kit": patch
4+
---
5+
6+
Remove git remote after cloning theme in `theme init` command to prevent accidental pushes to the skeleton theme repository

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ const mockedCommit = vi.fn(async () => ({}))
1313
const mockedRaw = vi.fn(async () => '')
1414
const mockedCheckout = vi.fn(async () => ({}))
1515
const mockedGitStatus = vi.fn(async (): Promise<{isClean: () => boolean}> => ({isClean: () => false}))
16+
const mockedRemoveRemote = vi.fn(async () => {})
17+
const mockedGetRemotes = vi.fn(async () => [] as {name: string; refs: any}[])
1618
const mockedTags = vi.fn(async () => ({}))
1719
const simpleGitProperties = {
1820
clone: mockedClone,
@@ -25,6 +27,8 @@ const simpleGitProperties = {
2527
checkoutLocalBranch: mockedCheckout,
2628
status: mockedGitStatus,
2729
tags: mockedTags,
30+
removeRemote: mockedRemoveRemote,
31+
getRemotes: mockedGetRemotes,
2832
}
2933

3034
vi.mock('simple-git')
@@ -506,3 +510,44 @@ describe('addToGitIgnore()', () => {
506510
})
507511
})
508512
})
513+
514+
describe('removeGitRemote()', () => {
515+
beforeEach(() => {
516+
mockedGetRemotes.mockReset()
517+
mockedRemoveRemote.mockReset()
518+
})
519+
520+
test('calls simple-git to remove a remote successfully', async () => {
521+
// Given
522+
const directory = '/test/directory'
523+
const remoteName = 'origin'
524+
mockedGetRemotes.mockResolvedValue([{name: 'origin', refs: {}}])
525+
mockedRemoveRemote.mockResolvedValue(undefined)
526+
527+
// When
528+
await git.removeGitRemote(directory, remoteName)
529+
530+
// Then
531+
expect(simpleGit).toHaveBeenCalledWith(directory)
532+
expect(mockedGetRemotes).toHaveBeenCalled()
533+
expect(mockedRemoveRemote).toHaveBeenCalledWith(remoteName)
534+
})
535+
536+
test('does nothing when remote does not exist', async () => {
537+
// Given
538+
const directory = '/test/directory'
539+
const remoteName = 'nonexistent'
540+
mockedGetRemotes.mockResolvedValue([
541+
{name: 'origin', refs: {}},
542+
{name: 'upstream', refs: {}},
543+
])
544+
545+
// When
546+
await git.removeGitRemote(directory, remoteName)
547+
548+
// Then
549+
expect(simpleGit).toHaveBeenCalledWith(directory)
550+
expect(mockedGetRemotes).toHaveBeenCalled()
551+
expect(mockedRemoveRemote).not.toHaveBeenCalled()
552+
})
553+
})

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

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -361,3 +361,30 @@ export async function getLatestTag(directory?: string): Promise<string | undefin
361361
const tags = await git({baseDir: directory}).tags()
362362
return tags.latest
363363
}
364+
365+
/**
366+
* Remove a git remote from the given directory.
367+
*
368+
* @param directory - The directory where the git repository is located.
369+
* @param remoteName - The name of the remote to remove (defaults to 'origin').
370+
* @returns A promise that resolves when the remote is removed.
371+
*/
372+
export async function removeGitRemote(directory: string, remoteName = 'origin'): Promise<void> {
373+
outputDebug(outputContent`Removing git remote ${remoteName} from ${outputToken.path(directory)}...`)
374+
await ensureGitIsPresentOrAbort()
375+
376+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
377+
// @ts-ignore
378+
const repo = git(directory)
379+
380+
// Check if remote exists first
381+
const remotes = await repo.getRemotes()
382+
const remoteExists = remotes.some((remote: {name: string}) => remote.name === remoteName)
383+
384+
if (!remoteExists) {
385+
outputDebug(outputContent`Remote ${remoteName} does not exist, no action needed`)
386+
return
387+
}
388+
389+
await repo.removeRemote(remoteName)
390+
}

packages/theme/src/cli/services/init.test.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,13 @@ describe('cloneRepoAndCheckoutLatestTag()', async () => {
2424
const repoUrl = 'https://github.com/Shopify/dawn.git'
2525
const destination = 'destination'
2626
const latestTag = true
27+
const shallow = true
2728

2829
// When
2930
await cloneRepoAndCheckoutLatestTag(repoUrl, destination)
3031

3132
// Then
32-
expect(downloadGitRepository).toHaveBeenCalledWith({repoUrl, destination, latestTag})
33+
expect(downloadGitRepository).toHaveBeenCalledWith({repoUrl, destination, latestTag, shallow})
3334
})
3435
})
3536

@@ -38,12 +39,12 @@ describe('cloneRepo()', async () => {
3839
// Given
3940
const repoUrl = 'https://github.com/Shopify/dawn.git'
4041
const destination = 'destination'
41-
42+
const shallow = true
4243
// When
4344
await cloneRepo(repoUrl, destination)
4445

4546
// Then
46-
expect(downloadGitRepository).toHaveBeenCalledWith({repoUrl, destination})
47+
expect(downloadGitRepository).toHaveBeenCalledWith({repoUrl, destination, shallow})
4748
})
4849
})
4950

packages/theme/src/cli/services/init.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import {renderSelectPrompt, renderTasks} from '@shopify/cli-kit/node/ui'
2-
import {downloadGitRepository} from '@shopify/cli-kit/node/git'
2+
import {downloadGitRepository, removeGitRemote} from '@shopify/cli-kit/node/git'
33
import {joinPath} from '@shopify/cli-kit/node/path'
44
import {mkdir, writeFile} from '@shopify/cli-kit/node/fs'
55
import {fetch} from '@shopify/cli-kit/node/http'
@@ -21,7 +21,9 @@ async function downloadRepository(repoUrl: string, destination: string, latestTa
2121
repoUrl,
2222
destination,
2323
latestTag,
24+
shallow: true,
2425
})
26+
await removeGitRemote(destination)
2527
},
2628
},
2729
])

0 commit comments

Comments
 (0)