Update from Git Manager GUI

This commit is contained in:
2025-12-28 18:06:55 +01:00
parent c510f52bdd
commit f7bb79c391
2 changed files with 372 additions and 0 deletions

57
src/git/gitHandler.js Normal file
View File

@@ -0,0 +1,57 @@
// src/git/gitHandler.js (CommonJS)
const path = require('path');
const fs = require('fs');
const simpleGit = require('simple-git');
function gitFor(folderPath) {
return simpleGit(folderPath);
}
async function initRepo(folderPath) {
const git = gitFor(folderPath);
if (!fs.existsSync(path.join(folderPath, '.git'))) {
await git.init();
}
return true;
}
async function commitAndPush(folderPath, branch = 'master', message = 'Update from Git Manager GUI', progressCb = null) {
const git = gitFor(folderPath);
await git.add('./*');
try {
await git.commit(message);
} catch (e) {
if (!/nothing to commit/i.test(String(e))) throw e;
}
const localBranches = (await git.branchLocal()).all;
if (!localBranches.includes(branch)) {
await git.checkoutLocalBranch(branch);
} else {
await git.checkout(branch);
}
if (progressCb) progressCb(30);
// push -u origin branch
await git.push(['-u', 'origin', branch]);
if (progressCb) progressCb(100);
return true;
}
async function getBranches(folderPath) {
const git = gitFor(folderPath);
const summary = await git.branchLocal();
return summary.all;
}
async function getCommitLogs(folderPath, count = 50) {
const git = gitFor(folderPath);
const log = await git.log({ n: count });
return log.all.map(c => `${c.hash.substring(0,7)} - ${c.message}`);
}
module.exports = { initRepo, commitAndPush, getBranches, getCommitLogs };