(gitPath: string)
| 124 | * @returns A promise resolving to the raw string content, or an empty string on error. |
| 125 | */ |
| 126 | export 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 | } |
no outgoing calls
no test coverage detected