MCPcopy Create free account
hub / github.com/5e-bits/5e-database / getOldFileContent

Function getOldFileContent

scripts/update/gitUtils.ts:126–163  ·  view source on GitHub ↗
(gitPath: string)

Source from the content-addressed store, hash-verified

124 * @returns A promise resolving to the raw string content, or an empty string on error.
125 */
126export async function getOldFileContent(gitPath: string): Promise<string> {
127 const fromRef = process.env.GIT_FROM_REF || 'HEAD~1';
128 return new Promise((resolve, reject) => {
129 const gitShow = spawn('git', ['show', `${fromRef}:${gitPath}`]);
130 let oldFileContent = '';
131 let errorOutput = '';
132
133 gitShow.stdout.on('data', (data: Buffer) => {
134 oldFileContent += data.toString();
135 });
136
137 gitShow.stderr.on('data', (data: Buffer) => {
138 errorOutput += data.toString();
139 });
140
141 gitShow.on('error', (err: Error) => {
142 // Handle errors spawning the process itself
143 console.error(`Error spawning git show for ${fromRef}:${gitPath}:`, err);
144 resolve(''); // Resolve with empty string on spawn error
145 });
146
147 gitShow.on('close', (code: number | null) => {
148 if (code === 0) {
149 resolve(oldFileContent);
150 } else {
151 // Handle errors reported by git show (like file not found)
152 if (errorOutput.includes('exists on disk, but not in')) {
153 console.warn(`Previous version not found in git history (${fromRef}:${gitPath}).`);
154 } else {
155 console.error(
156 `Error running git show for ${fromRef}:${gitPath} (code ${code}): ${errorOutput}`
157 );
158 }
159 resolve(''); // Resolve with empty string if git show fails
160 }
161 });
162 });
163}

Callers 3

_handleFileModifiedFunction · 0.90
_handleFileRenamedFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected