Refactor autoCommit function to handle multiple file chunks and improve commit logic
This commit is contained in:
125
main.js
125
main.js
@@ -1136,21 +1136,26 @@ function addMatchingFilesToGitignore(folderPath, pattern) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function autoCommit(folderPath, message, win) {
|
|
||||||
|
|
||||||
|
async function autoCommit(folderPath, message, win) {
|
||||||
const git = simpleGit(folderPath);
|
const git = simpleGit(folderPath);
|
||||||
const status = await git.status();
|
const status = await git.status();
|
||||||
if (
|
|
||||||
status.not_added.length === 0 &&
|
// Collect all files that need to be committed
|
||||||
status.created.length === 0 &&
|
let filesToAdd = [
|
||||||
status.deleted.length === 0 &&
|
...status.not_added,
|
||||||
status.modified.length === 0 &&
|
...status.created,
|
||||||
status.renamed.length === 0
|
...status.modified,
|
||||||
) {
|
...status.deleted,
|
||||||
|
...status.renamed.map(r => r.to)
|
||||||
|
].filter(Boolean);
|
||||||
|
|
||||||
|
if (filesToAdd.length === 0) {
|
||||||
debug('Auto-Commit: Keine Änderungen zum committen.');
|
debug('Auto-Commit: Keine Änderungen zum committen.');
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Make sure on correct branch
|
||||||
let currentBranch = null;
|
let currentBranch = null;
|
||||||
try {
|
try {
|
||||||
currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();
|
currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();
|
||||||
@@ -1161,7 +1166,6 @@ async function autoCommit(folderPath, message, win) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (!currentBranch || currentBranch === 'HEAD') {
|
if (!currentBranch || currentBranch === 'HEAD') {
|
||||||
// === Erweiterte Logs ===
|
|
||||||
const headCommit = (await git.revparse(['HEAD'])).trim();
|
const headCommit = (await git.revparse(['HEAD'])).trim();
|
||||||
let masterCommit = null;
|
let masterCommit = null;
|
||||||
let hasMaster = false;
|
let hasMaster = false;
|
||||||
@@ -1177,15 +1181,11 @@ async function autoCommit(folderPath, message, win) {
|
|||||||
debug(`[autoCommit] master: ${masterCommit}`);
|
debug(`[autoCommit] master: ${masterCommit}`);
|
||||||
|
|
||||||
if (hasMaster && headCommit === masterCommit) {
|
if (hasMaster && headCommit === masterCommit) {
|
||||||
// HEAD ist detached, zeigt aber exakt auf master-Tip → einfach auf master auschecken.
|
|
||||||
await git.checkout('master');
|
await git.checkout('master');
|
||||||
debug('[autoCommit] HEAD war detached, zeigte aber exakt auf master – jetzt zurück auf 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();
|
currentBranch = (await git.revparse(['--abbrev-ref', 'HEAD'])).trim();
|
||||||
debug(`[autoCommit] Nach checkout: Aktueller Branch: ${currentBranch}`);
|
debug(`[autoCommit] Nach checkout: Aktueller Branch: ${currentBranch}`);
|
||||||
// Jetzt **NICHT** weiter zur Umbenenn-Logik!
|
|
||||||
} else if (hasMaster) {
|
} 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 timestamp = new Date().toISOString().replace(/[:.]/g, '-');
|
||||||
const backupBranch = `backup-master-${timestamp}`;
|
const backupBranch = `backup-master-${timestamp}`;
|
||||||
await git.branch(['-m', 'master', backupBranch]);
|
await git.branch(['-m', 'master', backupBranch]);
|
||||||
@@ -1194,76 +1194,95 @@ async function autoCommit(folderPath, message, win) {
|
|||||||
debug('[autoCommit] Neuer master-Branch erstellt und ausgecheckt.');
|
debug('[autoCommit] Neuer master-Branch erstellt und ausgecheckt.');
|
||||||
currentBranch = 'master';
|
currentBranch = 'master';
|
||||||
} else {
|
} else {
|
||||||
// Kein master vorhanden (erstmalig)
|
|
||||||
await git.checkout(['-b', 'master']);
|
await git.checkout(['-b', 'master']);
|
||||||
debug('[autoCommit] Kein master-Branch vorhanden, neuer master erstellt.');
|
debug('[autoCommit] Kein master-Branch vorhanden, neuer master erstellt.');
|
||||||
currentBranch = 'master';
|
currentBranch = 'master';
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// --- Zeilenzählung ---
|
// We will track linesChanged across all chunks
|
||||||
let diffOutput = await git.diff(['--numstat']);
|
let totalChangedLines = 0;
|
||||||
// Zeilensumme berechnen:
|
|
||||||
let changedLines = 0;
|
|
||||||
for (let line of diffOutput.split('\n')) {
|
|
||||||
const match = line.match(/^(\d+|\-)\s+(\d+|\-)\s+(.*)$/);
|
|
||||||
if (match) {
|
|
||||||
const added = match[1] === '-' ? 0 : parseInt(match[1], 10);
|
|
||||||
const deleted = match[2] === '-' ? 0 : parseInt(match[2], 10);
|
|
||||||
changedLines += added + deleted;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Folders aus Store holen
|
|
||||||
let folders = store.get('folders') || [];
|
let folders = store.get('folders') || [];
|
||||||
let idx = folders.findIndex(f => f.path === folderPath);
|
let idx = folders.findIndex(f => f.path === folderPath);
|
||||||
if (idx !== -1) {
|
let allNewHeads = [];
|
||||||
folders[idx].linesChanged = (folders[idx].linesChanged || 0) + changedLines;
|
|
||||||
folders[idx].llmCandidates = folders[idx].llmCandidates || [];
|
|
||||||
|
|
||||||
// ===> WICHTIG: Wir commiten ja jetzt, daher merken wir uns gleich die neue Commit-Hash
|
// --- Chunked commit loop ---
|
||||||
// Vor git.commit: Merke alten HEAD
|
let commitCount = 0;
|
||||||
|
for (let i = 0; i < filesToAdd.length; i += MAX_FILES_PER_COMMIT) {
|
||||||
|
const chunk = filesToAdd.slice(i, i + MAX_FILES_PER_COMMIT);
|
||||||
|
if (chunk.length === 0) continue;
|
||||||
|
await git.add(chunk);
|
||||||
|
debug(`[autoCommit] Dateien gestaged: ${chunk.length}`);
|
||||||
|
|
||||||
|
// Count lines for this chunk only
|
||||||
|
let diffOutput = await git.diff(['--numstat', '--staged']);
|
||||||
|
let changedLines = 0;
|
||||||
|
for (let line of diffOutput.split('\n')) {
|
||||||
|
const match = line.match(/^(\d+|\-)\s+(\d+|\-)\s+(.*)$/);
|
||||||
|
if (match) {
|
||||||
|
const added = match[1] === '-' ? 0 : parseInt(match[1], 10);
|
||||||
|
const deleted = match[2] === '-' ? 0 : parseInt(match[2], 10);
|
||||||
|
changedLines += added + deleted;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
totalChangedLines += changedLines;
|
||||||
|
|
||||||
|
// Compose commit message with (part N) suffix if needed
|
||||||
|
commitCount++;
|
||||||
|
let commitMsg = message || '[auto]';
|
||||||
|
if (filesToAdd.length > MAX_FILES_PER_COMMIT) {
|
||||||
|
commitMsg += ` (part ${commitCount})`;
|
||||||
|
}
|
||||||
|
|
||||||
|
// Record old HEAD before commit
|
||||||
const oldHead = (await git.revparse(['HEAD'])).trim();
|
const oldHead = (await git.revparse(['HEAD'])).trim();
|
||||||
|
await git.commit(commitMsg);
|
||||||
// Stagen & Committen
|
|
||||||
await git.add(['-A']);
|
|
||||||
debug('[autoCommit] Alle Änderungen gestaged.');
|
|
||||||
await git.commit(message || '[auto]');
|
|
||||||
debug('[autoCommit] Commit erfolgreich erstellt.');
|
debug('[autoCommit] Commit erfolgreich erstellt.');
|
||||||
|
|
||||||
// --- Gamification: Commit-Statistik speichern ---
|
// --- Gamification/statistics for each chunk ---
|
||||||
const today = new Date().toISOString().slice(0, 10); // Format: 'YYYY-MM-DD'
|
const today = new Date().toISOString().slice(0, 10);
|
||||||
const stats = store.get('dailyCommitStats') || {};
|
const stats = store.get('dailyCommitStats') || {};
|
||||||
stats[today] = (stats[today] || 0) + 1;
|
stats[today] = (stats[today] || 0) + 1;
|
||||||
store.set('dailyCommitStats', stats);
|
store.set('dailyCommitStats', stats);
|
||||||
|
|
||||||
// Nach Commit: neuen HEAD ermitteln und in llmCandidates speichern
|
// After commit: store new HEAD in llmCandidates for the chunk
|
||||||
const newHead = (await git.revparse(['HEAD'])).trim();
|
const newHead = (await git.revparse(['HEAD'])).trim();
|
||||||
folders[idx].llmCandidates = folders[idx].llmCandidates || [];
|
allNewHeads.push(newHead);
|
||||||
folders[idx].llmCandidates.push(newHead);
|
|
||||||
if(folders[idx].llmCandidates.length == 1){
|
// folders[idx] is already loaded above
|
||||||
folders[idx].firstCandidateBirthday = Date.now();
|
if (idx !== -1) {
|
||||||
debug('[autoCommit] Erster Commit aufgenommen.');
|
folders[idx].llmCandidates = folders[idx].llmCandidates || [];
|
||||||
|
folders[idx].llmCandidates.push(newHead);
|
||||||
|
folders[idx].lastHeadHash = newHead;
|
||||||
|
if (folders[idx].llmCandidates.length === 1) {
|
||||||
|
folders[idx].firstCandidateBirthday = Date.now();
|
||||||
|
debug('[autoCommit] Erster Commit aufgenommen.');
|
||||||
|
}
|
||||||
}
|
}
|
||||||
folders[idx].lastHeadHash = newHead;
|
}
|
||||||
console.log(folders[idx].llmCandidates)
|
|
||||||
|
if (idx !== -1) {
|
||||||
|
// Update total linesChanged only once per full commit batch
|
||||||
|
folders[idx].linesChanged = (folders[idx].linesChanged || 0) + totalChangedLines;
|
||||||
|
console.log(folders[idx].llmCandidates);
|
||||||
|
|
||||||
// Threshold holen
|
// Threshold holen
|
||||||
const threshold = store.get('intelligentCommitThreshold') || 10;
|
const threshold = store.get('intelligentCommitThreshold') || 10;
|
||||||
if (folders[idx].linesChanged >= threshold) {
|
if (folders[idx].linesChanged >= threshold) {
|
||||||
debug('Congratulations! You changed enough lines of code :)');
|
debug('Congratulations! You changed enough lines of code :)');
|
||||||
await runLLMCommitRewrite(folders[idx], win);
|
await runLLMCommitRewrite(folders[idx], win);
|
||||||
//folders[idx].linesChanged = 0; // !!!!!!!!!!!!!!!!!!!! needs logic to handle several llm runs called at the same time test
|
//folders[idx].linesChanged = 0;
|
||||||
//folders[idx].llmCandidates = [];
|
|
||||||
//folders.[idx].firstCandidateBirthday = null
|
|
||||||
}
|
}
|
||||||
store.set('folders', folders);
|
store.set('folders', folders);
|
||||||
} else {
|
} else {
|
||||||
// Folder not found! (Debug)
|
|
||||||
debug(`[autoCommit] Warning: Folder ${folderPath} not found in store`);
|
debug(`[autoCommit] Warning: Folder ${folderPath} not found in store`);
|
||||||
}
|
}
|
||||||
|
debug(`[autoCommit] Committed ${filesToAdd.length} files in ${commitCount} chunk(s), ${totalChangedLines} lines changed.`);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
app.whenReady().then(main);
|
app.whenReady().then(main);
|
||||||
async function main() {
|
async function main() {
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user