MCPcopy Index your code
hub / github.com/CodebuffAI/codebuff / applyChange

Function applyChange

sdk/src/tools/change-file.ts:75–107  ·  view source on GitHub ↗
(params: {
  change: FileChange
  resolvedPath: ResolvedProjectPath
  fs: CodebuffFileSystem
})

Source from the content-addressed store, hash-verified

73}
74
75async function applyChange(params: {
76 change: FileChange
77 resolvedPath: ResolvedProjectPath
78 fs: CodebuffFileSystem
79}): Promise<ApplyChangeResult> {
80 const { change, resolvedPath, fs } = params
81 const { content, type } = change
82 const { fullPath, relativePath } = resolvedPath
83
84 try {
85 const exists = await fileExists({ filePath: fullPath, fs })
86 if (!exists) {
87 const dirPath = path.dirname(fullPath)
88 await fs.mkdir(dirPath, { recursive: true })
89 }
90
91 if (type === 'file') {
92 await fs.writeFile(fullPath, content)
93 } else {
94 const oldContent = await fs.readFile(fullPath, 'utf-8')
95 const newContent = applyPatch(oldContent, content)
96 if (newContent === false) {
97 return { status: 'patchFailed', file: relativePath, patch: content }
98 }
99 await fs.writeFile(fullPath, newContent)
100 }
101
102 return { status: exists ? 'modified' : 'created', file: relativePath }
103 } catch (error) {
104 console.error(`Failed to apply patch to ${relativePath}:`, error, content)
105 return { status: 'invalid', file: relativePath }
106 }
107}

Callers 1

changeFileFunction · 0.85

Calls 3

fileExistsFunction · 0.90
writeFileMethod · 0.80
readFileMethod · 0.80

Tested by

no test coverage detected