diff --git a/main.js b/main.js index 7515a96..6dad3d3 100644 --- a/main.js +++ b/main.js @@ -1,3 +1,153 @@ +const { app, BrowserWindow, ipcMain, dialog, Menu, shell, clipboard } = require('electron'); +app.name = 'Auto-Git'; +const { exec } = require('child_process'); +const { spawn } = require('child_process'); +const { spawnSync } = require('child_process'); +const path = require('path'); +const fs = require('fs'); +const os = require('os'); +const Store = require('electron-store'); +const simpleGit = require('simple-git'); +//const fetch = require('node-fetch'); +const chokidar = require('chokidar'); + +const store = new Store({ + defaults: { + folders: [], + selected: null, + skymode: true, + skipGitPrompt: true, + intelligentCommitThreshold: 100 + } +}); + + +let folders = store.get('folders'); +if (Array.isArray(folders)) { + folders = folders.map(f => ({ + ...f, + linesChanged: 0, // zurück auf 0 + llmCandidates: [] // leeres Array + })); + store.set('folders', folders); +} + +// Map zum Speichern der Watcher pro Ordner +const repoWatchers = new Map(); + + + +// Debug Helper +function debug(msg) { + console.log(`[DEBUG ${new Date().toISOString()}] ${msg}`); +} + + +/** + * Erstellt das BrowserWindow und lädt index.html. + * Gibt das Window-Objekt zurück. + */ +function createWindow() { + const win = new BrowserWindow({ + width: 900, + height: 600, + minWidth: 600, + minHeight: 400, + title: 'Auto-Git', + webPreferences: { + preload: path.join(__dirname, 'preload.js'), + contextIsolation: true + } + }); + win.loadFile('index.html'); + return win; +} + + +// Settings-Fenster +let settingsWin; +function openSettings(win) { + if (settingsWin) { + settingsWin.focus(); + return; + } + settingsWin = new BrowserWindow({ + parent: win, + modal: true, + width: 400, + height: 300, + resizable: false, + webPreferences: { + preload: path.join(__dirname, 'preload.js'), + contextIsolation: true + } + }); + settingsWin.removeMenu(); + settingsWin.loadFile('settings.html'); + settingsWin.on('closed', () => settingsWin = null); +} + + +/** + * Startet einen File-Watcher auf .git/refs/heads/master, + * sendet bei Änderungen 'repo-updated' an den Renderer. + */ +function watchRepo(folder, win) { + const gitHead = path.join(folder, '.git', 'refs', 'heads', 'master'); + const watcher = chokidar.watch(gitHead, { ignoreInitial: true }); + watcher.on('change', () => { + win.webContents.send('repo-updated', folder); + }); + repoWatchers.set(folder, watcher); +} + +/** + * Initiiert ein Git-Repo in `folder`, falls noch nicht vorhanden, + * und erzeugt einen Initial-Commit mit Timestamp. + */ +async function initGitRepo(folder) { + const git = simpleGit(folder); + const gitDir = path.join(folder, '.git'); + if (!fs.existsSync(gitDir)) { + await git.init(); + const message = `Initial commit (generated by auto-git)`; + const readmePath = path.join(folder, 'README.md'); + fs.writeFileSync(readmePath, `# Projekt in ${path.basename(folder)}\n`); + await git.add('./*'); + await git.commit(message); + } +} + + +// Map für Monitoring-Watcher (nicht repoWatchers!) +const monitoringWatchers = new Map(); + +// Helper: Baut eine Commit-Message aus git.status() +function buildCommitMessageFromStatus(status, prefix = '[auto]') { + const changes = []; + status.not_added.forEach(f => changes.push(`[add] ${f}`)); + status.created.forEach(f => changes.push(`[add] ${f}`)); + status.modified.forEach(f => changes.push(`[change] ${f}`)); + status.deleted.forEach(f => changes.push(`[unlink] ${f}`)); + status.renamed.forEach(r => changes.push(`[rename] ${r.from} → ${r.to}`)); + return prefix + '\n' + changes.map(l => ` ${l}`).join('\n'); +} + +function startMonitoringWatcher(folderPath, win) { + if (monitoringWatchers.has(folderPath)) return; + const watcher = chokidar.watch(folderPath, { + ignored: /(^|[\/\\])\..|node_modules|\.git/, + ignoreInitial: true, + persistent: true, + depth: 99, + awaitWriteFinish: { stabilityThreshold: 300, pollInterval: 100 } + }); + + // Initialer Commit + (async () => { + debug(`[MONITOR] Starte initialen Commit-Check für ${folderPath}`); + + const git = simpleGit(folderPath); const status = await git.status();