Skip to content

Commit c846e6f

Browse files
authored
Merge pull request #5314 from Shopify/gitignore-bug
Improve mechanism that adds `.shopify` to `.gitignore` to avoid duplicate entries
2 parents fd3ed80 + da606a6 commit c846e6f

File tree

5 files changed

+55
-4
lines changed

5 files changed

+55
-4
lines changed

.changeset/curvy-kangaroos-flow.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
'@shopify/cli-kit': patch
3+
'@shopify/theme': patch
4+
---
5+
6+
Improve mechanism that adds `.shopify` to `.gitignore` to avoid duplicate entries

packages/cli-kit/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@
137137
"gradient-string": "2.0.2",
138138
"graphql": "16.8.1",
139139
"graphql-request": "5.2.0",
140+
"ignore": "6.0.2",
140141
"ink": "4.4.1",
141142
"is-interactive": "2.0.0",
142143
"jose": "5.9.6",

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

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -444,4 +444,38 @@ describe('addToGitIgnore()', () => {
444444
expect(gitIgnoreContent).toBe('node_modules\ndist\n.shopify\n')
445445
})
446446
})
447+
448+
test('does nothing when .shopify/* pattern exists in .gitignore', async () => {
449+
await inTemporaryDirectory(async (tmpDir) => {
450+
// Given
451+
const gitIgnorePath = `${tmpDir}/.gitignore`
452+
const gitIgnoreContent = '.shopify/*\nnode_modules\n'
453+
454+
writeFileSync(gitIgnorePath, gitIgnoreContent)
455+
456+
// When
457+
git.addToGitIgnore(tmpDir, '.shopify')
458+
459+
// Then
460+
const actualContent = readFileSync(gitIgnorePath).toString()
461+
expect(actualContent).toBe(gitIgnoreContent)
462+
})
463+
})
464+
465+
test('does nothing when .shopify/** pattern exists in .gitignore', async () => {
466+
await inTemporaryDirectory(async (tmpDir) => {
467+
// Given
468+
const gitIgnorePath = `${tmpDir}/.gitignore`
469+
const gitIgnoreContent = '.shopify/**\nnode_modules\n'
470+
471+
writeFileSync(gitIgnorePath, gitIgnoreContent)
472+
473+
// When
474+
git.addToGitIgnore(tmpDir, '.shopify')
475+
476+
// Then
477+
const actualContent = readFileSync(gitIgnorePath).toString()
478+
expect(actualContent).toBe(gitIgnoreContent)
479+
})
480+
})
447481
})

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import {cwd, joinPath} from './path.js'
66
import {runWithTimer} from './metadata.js'
77
import {outputContent, outputToken, outputDebug} from '../../public/node/output.js'
88
import git, {TaskOptions, SimpleGitProgressEvent, DefaultLogFields, ListLogLine, SimpleGit} from 'simple-git'
9+
import ignore from 'ignore'
910

1011
/**
1112
* Initialize a git repository at the given directory.
@@ -83,8 +84,14 @@ export function addToGitIgnore(root: string, entry: string): void {
8384
const gitIgnoreContent = readFileSync(gitIgnorePath).toString()
8485
const eol = detectEOL(gitIgnoreContent)
8586

86-
if (gitIgnoreContent.split(eol).some((line) => line.trim() === entry.trim())) {
87-
// The file already existing in the .gitignore
87+
const lines = gitIgnoreContent.split(eol).map((line) => line.trim())
88+
const ignoreManager = ignore.default({allowRelativePaths: true}).add(lines)
89+
90+
const isIgnoredEntry = ignoreManager.ignores(joinPath(entry))
91+
const isIgnoredEntryAsDir = ignoreManager.ignores(joinPath(entry, 'ignored.txt'))
92+
const isAlreadyIgnored = isIgnoredEntry || isIgnoredEntryAsDir
93+
if (isAlreadyIgnored) {
94+
// The file is already ignored by an existing pattern
8895
return
8996
}
9097

pnpm-lock.yaml

Lines changed: 5 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)