Update from Git Manager GUI
This commit is contained in:
57
src/git/gitHandler.js
Normal file
57
src/git/gitHandler.js
Normal 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 };
|
||||
Reference in New Issue
Block a user