From 12cd409616415e4be74652f8daac3a0e71efb31d Mon Sep 17 00:00:00 2001 From: Victor Giers Date: Sun, 1 Jun 2025 10:52:17 +0200 Subject: [PATCH] Refactor and enhance commit logic with error handling and additional logging --- main.js | 92 +++++++++++++++++++++++++++++++++++++-------------------- 1 file changed, 60 insertions(+), 32 deletions(-) diff --git a/main.js b/main.js index 11f5000..d338f7d 100644 --- a/main.js +++ b/main.js @@ -965,6 +965,27 @@ async function rewordCommitsSequentially(repoPath, commitMessageMap, hashes) { const commitMsg = msg.replace(/(["$`\\])/g, '\\$1'); const sequenceEditor = `sed -i '' '1s/pick/reword/'`; + try { + await new Promise((resolve, reject) => { + const abortProc = spawn('git', ['rebase', '--abort'], { + cwd: repoPath, + env: process.env, + stdio: 'inherit' + }); + abortProc.on('exit', code => { + if (code === 0) { + console.log('[AutoGit] Vorheriger Rebase-Prozess wurde abgebrochen.'); + } else { + console.log('[AutoGit] Kein laufender Rebase-Prozess zum Abbrechen (vermutlich).'); + } + resolve(); // Selbst bei Fehler einfach weiter + }); + }); + } catch (err) { + console.warn('[AutoGit] Fehler beim Abbrechen des Rebase-Prozesses:', err.message); + } + + // await auf Promise – aber OHNE zweiten for! await new Promise((resolve, reject) => { const proc = spawn('git', [ @@ -1104,6 +1125,7 @@ function addMatchingFilesToGitignore(folderPath, pattern) { async function autoCommit(folderPath, message, win) { + const git = simpleGit(folderPath); const status = await git.status(); if ( @@ -1127,6 +1149,7 @@ async function autoCommit(folderPath, message, win) { } if (!currentBranch || currentBranch === 'HEAD') { + // === Erweiterte Logs === const headCommit = (await git.revparse(['HEAD'])).trim(); let masterCommit = null; let hasMaster = false; @@ -1134,6 +1157,7 @@ async function autoCommit(folderPath, message, win) { masterCommit = (await git.revparse(['refs/heads/master'])).trim(); hasMaster = true; } catch (e) { + debug('[autoCommit] master branch existiert nicht.'); masterCommit = null; hasMaster = false; } @@ -1141,11 +1165,15 @@ async function autoCommit(folderPath, message, win) { debug(`[autoCommit] master: ${masterCommit}`); if (hasMaster && headCommit === masterCommit) { + // HEAD ist detached, zeigt aber exakt auf master-Tip → einfach auf master auschecken. await git.checkout('master'); debug('[autoCommit] HEAD war detached, zeigte aber exakt auf master – jetzt zurück auf master.'); + // Nach dem Checkout nochmal aktuellen Branch loggen: currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim(); debug(`[autoCommit] Nach checkout: Aktueller Branch: ${currentBranch}`); + // Jetzt **NICHT** weiter zur Umbenenn-Logik! } else if (hasMaster) { + // HEAD ist detached, zeigt auf einen anderen Commit → backup master und neuen master-Branch const timestamp = new Date().toISOString().replace(/[:.]/g, '-'); const backupBranch = `backup-master-${timestamp}`; await git.branch(['-m', 'master', backupBranch]); @@ -1154,14 +1182,16 @@ async function autoCommit(folderPath, message, win) { debug('[autoCommit] Neuer master-Branch erstellt und ausgecheckt.'); currentBranch = 'master'; } else { + // Kein master vorhanden (erstmalig) await git.checkout(['-b', 'master']); debug('[autoCommit] Kein master-Branch vorhanden, neuer master erstellt.'); currentBranch = 'master'; } } - // Zeilen zählen + // --- Zeilenzählung --- let diffOutput = await git.diff(['--numstat']); + // Zeilensumme berechnen: let changedLines = 0; for (let line of diffOutput.split('\n')) { const match = line.match(/^(\d+|\-)\s+(\d+|\-)\s+(.*)$/); @@ -1176,51 +1206,49 @@ async function autoCommit(folderPath, message, win) { let folders = store.get('folders') || []; let idx = folders.findIndex(f => f.path === folderPath); if (idx !== -1) { - let folderObj = folders[idx]; + folders[idx].linesChanged = (folders[idx].linesChanged || 0) + changedLines; + folders[idx].llmCandidates = folders[idx].llmCandidates || []; - // Wenn gerade ein Rebase läuft, committen wir nicht direkt in llmCandidates, - // sondern in pendingLLMCandidates. So hüten wir uns, das laufende Rebase zu stören. - if (folderObj.rebasing) { - await git.add(['-A']); - debug('[autoCommit] Alle Änderungen gestaged (während Rebase).'); - await git.commit(message || '[auto]'); - debug('[autoCommit] Commit erfolgreich erstellt (während Rebase).'); - const newHead = (await git.revparse(['HEAD'])).trim(); - folderObj.pendingLLMCandidates.push(newHead); - folderObj.linesChanged = (folderObj.linesChanged || 0) + changedLines; - folders[idx] = folderObj; - store.set('folders', folders); - return true; - } + // ===> WICHTIG: Wir commiten ja jetzt, daher merken wir uns gleich die neue Commit-Hash + // Vor git.commit: Merke alten HEAD + const oldHead = (await git.revparse(['HEAD'])).trim(); - // Standard-Fall: kein laufender Rebase, normal committen + // Stagen & Committen await git.add(['-A']); debug('[autoCommit] Alle Änderungen gestaged.'); await git.commit(message || '[auto]'); debug('[autoCommit] Commit erfolgreich erstellt.'); - const newHead = (await git.revparse(['HEAD'])).trim(); + + // --- Gamification: Commit-Statistik speichern --- + const today = new Date().toISOString().slice(0, 10); // Format: 'YYYY-MM-DD' + const stats = store.get('dailyCommitStats') || {}; + stats[today] = (stats[today] || 0) + 1; + store.set('dailyCommitStats', stats); - folderObj.linesChanged = (folderObj.linesChanged || 0) + changedLines; - folderObj.llmCandidates = folderObj.llmCandidates || []; - folderObj.llmCandidates.push(newHead); - if (folderObj.llmCandidates.length === 1) { - folderObj.firstCandidateBirthday = Date.now(); + // Nach Commit: neuen HEAD ermitteln und in llmCandidates speichern + const newHead = (await git.revparse(['HEAD'])).trim(); + folders[idx].llmCandidates = folders[idx].llmCandidates || []; + folders[idx].llmCandidates.push(newHead); + if(folders[idx].llmCandidates.length == 1){ + folders[idx].firstCandidateBirthday = Date.now(); debug('[autoCommit] Erster Commit aufgenommen.'); } - folderObj.lastHeadHash = newHead; + folders[idx].lastHeadHash = newHead; + console.log(folders[idx].llmCandidates) + // Threshold holen const threshold = store.get('intelligentCommitThreshold') || 10; - if (folderObj.linesChanged >= threshold) { - debug('Threshold erreicht – starte LLM-Rebase!'); - await runLLMCommitRewrite(folderObj, win); + if (folders[idx].linesChanged >= threshold) { + debug('Congratulations! You changed enough lines of code :)'); + await runLLMCommitRewrite(folders[idx], win); + //folders[idx].linesChanged = 0; // !!!!!!!!!!!!!!!!!!!! needs logic to handle several llm runs called at the same time test + //folders[idx].llmCandidates = []; + //folders.[idx].firstCandidateBirthday = null } - - folders[idx] = folderObj; store.set('folders', folders); - return true; } else { - debug(`[autoCommit] Warning: Folder ${folderPath} nicht gefunden im Store`); - return false; + // Folder not found! (Debug) + debug(`[autoCommit] Warning: Folder ${folderPath} not found in store`); } }