refactor: enhance Git fallback strategy in edit processing

- Improved logging within the applyGitFallback function to provide clearer insights during the commit and cherry-pick processes.
- Streamlined error handling to ensure more informative console outputs when operations fail.
- Maintained consistent formatting and indentation for better readability and maintainability of the code.
- Ensured temporary directory cleanup is handled correctly in all scenarios, preventing potential resource leaks.
This commit is contained in:
Daniel Riccio
2025-01-14 17:00:51 -05:00
parent 3d901929c2
commit a323a1008e

View File

@@ -137,23 +137,19 @@ async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResu
let tmpDir: tmp.DirResult | undefined;
try {
// Create temporary directory
tmpDir = tmp.dirSync({ unsafeCleanup: true });
const git: SimpleGit = simpleGit(tmpDir.name);
// Initialize git repo
await git.init();
await git.addConfig('user.name', 'Temp');
await git.addConfig('user.email', 'temp@example.com');
const filePath = path.join(tmpDir.name, 'file.txt');
// Build the search text (context + removals)
const searchLines = hunk.changes
.filter(change => change.type === 'context' || change.type === 'remove')
.map(change => change.originalLine || (change.indent + change.content));
// Build the replace text (context + additions)
const replaceLines = hunk.changes
.filter(change => change.type === 'context' || change.type === 'add')
.map(change => change.originalLine || (change.indent + change.content));
@@ -162,29 +158,28 @@ async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResu
const replaceText = replaceLines.join('\n');
const originalText = content.join('\n');
// Strategy 1: O->S->R, cherry-pick R onto O
try {
// Original commit - use full file content
fs.writeFileSync(filePath, originalText);
await git.add('file.txt');
const originalCommit = await git.commit('original');
console.log('Strategy 1 - Original commit:', originalCommit.commit);
// Search commit - just the search text
fs.writeFileSync(filePath, searchText);
await git.add('file.txt');
await git.commit('search');
const searchCommit1 = await git.commit('search');
console.log('Strategy 1 - Search commit:', searchCommit1.commit);
// Replace commit - just the replace text
fs.writeFileSync(filePath, replaceText);
await git.add('file.txt');
const replaceCommit = await git.commit('replace');
console.log('Strategy 1 - Replace commit:', replaceCommit.commit);
// Go back to original and cherry-pick
await git.checkout(originalCommit.commit);
console.log('Strategy 1 - Attempting checkout of:', originalCommit.commit);
await git.raw(['checkout', originalCommit.commit]);
try {
console.log('Strategy 1 - Attempting cherry-pick of:', replaceCommit.commit);
await git.raw(['cherry-pick', '--minimal', replaceCommit.commit]);
// Read result
const newText = fs.readFileSync(filePath, 'utf-8');
const newLines = newText.split('\n');
return {
@@ -193,40 +188,40 @@ async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResu
strategy: 'git-fallback'
};
} catch (cherryPickError) {
console.log('Strategy 1 failed with merge conflict');
console.error('Strategy 1 failed with merge conflict');
}
} catch (error) {
console.log('Strategy 1 failed:', error);
console.error('Strategy 1 failed:', error);
}
// Strategy 2: S->R, S->O, cherry-pick R onto O
try {
// Reset repo
await git.init();
await git.addConfig('user.name', 'Temp');
await git.addConfig('user.email', 'temp@example.com');
// Search commit - just the search text
fs.writeFileSync(filePath, searchText);
await git.add('file.txt');
const searchCommit = await git.commit('search');
const searchHash = searchCommit.commit.replace(/^HEAD /, '');
console.log('Strategy 2 - Search commit:', searchHash);
// Replace commit - just the replace text
fs.writeFileSync(filePath, replaceText);
await git.add('file.txt');
const replaceCommit = await git.commit('replace');
const replaceHash = replaceCommit.commit.replace(/^HEAD /, '');
console.log('Strategy 2 - Replace commit:', replaceHash);
// Go back to search and create original with full file content
await git.checkout(searchCommit.commit);
console.log('Strategy 2 - Attempting checkout of:', searchHash);
await git.raw(['checkout', searchHash]);
fs.writeFileSync(filePath, originalText);
await git.add('file.txt');
await git.commit('original');
const originalCommit2 = await git.commit('original');
console.log('Strategy 2 - Original commit:', originalCommit2.commit);
try {
// Cherry-pick replace onto original
await git.raw(['cherry-pick', '--minimal', replaceCommit.commit]);
console.log('Strategy 2 - Attempting cherry-pick of:', replaceHash);
await git.raw(['cherry-pick', '--minimal', replaceHash]);
// Read result
const newText = fs.readFileSync(filePath, 'utf-8');
const newLines = newText.split('\n');
return {
@@ -235,20 +230,18 @@ async function applyGitFallback(hunk: Hunk, content: string[]): Promise<EditResu
strategy: 'git-fallback'
};
} catch (cherryPickError) {
console.log('Strategy 2 failed with merge conflict');
console.error('Strategy 2 failed with merge conflict');
}
} catch (error) {
console.log('Strategy 2 failed:', error);
console.error('Strategy 2 failed:', error);
}
// If both strategies fail, return no confidence
console.log('Git fallback failed');
console.error('Git fallback failed');
return { confidence: 0, result: content, strategy: 'git-fallback' };
} catch (error) {
console.log('Git fallback strategy failed:', error);
console.error('Git fallback strategy failed:', error);
return { confidence: 0, result: content, strategy: 'git-fallback' };
} finally {
// Clean up temporary directory
if (tmpDir) {
tmpDir.removeCallback();
}