MCPcopy
hub / github.com/codeaashu/claude-code / addFileGlobRuleToGitignore

Function addFileGlobRuleToGitignore

src/utils/git/gitignore.ts:53–99  ·  view source on GitHub ↗
(
  filename: string,
  cwd: string = getCwd(),
)

Source from the content-addressed store, hash-verified

51 * @param cwd The current working directory (optional)
52 */
53export async function addFileGlobRuleToGitignore(
54 filename: string,
55 cwd: string = getCwd(),
56): Promise<void> {
57 try {
58 if (!(await dirIsInGitRepo(cwd))) {
59 return
60 }
61
62 // First check if the pattern is already ignored by any gitignore file (including global)
63 const gitignoreEntry = `**/${filename}`
64 // For directory patterns (ending with /), check with a sample file inside
65 const testPath = filename.endsWith('/')
66 ? `${filename}sample-file.txt`
67 : filename
68 if (await isPathGitignored(testPath, cwd)) {
69 // File is already ignored by existing patterns (local or global)
70 return
71 }
72
73 // Use the global gitignore file in .config/git/ignore
74 const globalGitignorePath = getGlobalGitignorePath()
75
76 // Create the directory if it doesn't exist
77 const configGitDir = dirname(globalGitignorePath)
78 await mkdir(configGitDir, { recursive: true })
79
80 // Add the entry to the global gitignore
81 try {
82 const content = await readFile(globalGitignorePath, { encoding: 'utf-8' })
83 if (content.includes(gitignoreEntry)) {
84 return // Pattern already exists, don't add again
85 }
86 await appendFile(globalGitignorePath, `\n${gitignoreEntry}\n`)
87 } catch (e: unknown) {
88 const code = getErrnoCode(e)
89 if (code === 'ENOENT') {
90 // Create global gitignore with entry
91 await writeFile(globalGitignorePath, `${gitignoreEntry}\n`, 'utf-8')
92 } else {
93 throw e
94 }
95 }
96 } catch (error) {
97 logError(error)
98 }
99}
100

Callers 1

updateSettingsForSourceFunction · 0.85

Calls 8

getCwdFunction · 0.85
dirIsInGitRepoFunction · 0.85
isPathGitignoredFunction · 0.85
getGlobalGitignorePathFunction · 0.85
mkdirFunction · 0.85
readFileFunction · 0.85
getErrnoCodeFunction · 0.85
logErrorFunction · 0.50

Tested by

no test coverage detected