Files
parths-life-manager/git-commit.mjs
T

65 lines
1.7 KiB
JavaScript

import git from 'isomorphic-git';
import fs from 'fs';
import path from 'path';
const dir = process.cwd();
async function run() {
// 1. Initialize git repo if not present
try {
await git.init({ fs, dir, defaultBranch: 'main' });
console.log('Git repository initialized.');
} catch (err) {
console.log('Git init note:', err.message);
}
// 2. Scan and stage files ignoring node_modules, .next, .git
console.log('Scanning repository files...');
const matrix = await git.statusMatrix({
fs,
dir,
filter: (filepath) => {
if (filepath.startsWith('node_modules')) return false;
if (filepath.startsWith('.next')) return false;
if (filepath.startsWith('.git')) return false;
return true;
}
});
let stagedCount = 0;
for (const [filepath, headStatus, workdirStatus, stageStatus] of matrix) {
if (workdirStatus === 0) {
await git.remove({ fs, dir, filepath });
} else {
await git.add({ fs, dir, filepath });
stagedCount++;
}
}
console.log(`Staged ${stagedCount} files.`);
// 3. Create Commit
const sha = await git.commit({
fs,
dir,
author: {
name: 'Parth',
email: 'parth@example.com',
},
message: 'added 1RM and macro calculators to fitness dashboard.',
});
console.log(`Commit created successfully: ${sha}`);
// 4. Verify Log
const commits = await git.log({ fs, dir, depth: 3 });
console.log('\n--- Git Commit Log ---');
commits.forEach(c => {
console.log(`Commit: ${c.oid.slice(0, 7)} | Author: ${c.commit.author.name} | Message: ${c.commit.message.trim()}`);
});
}
run().catch((err) => {
console.error('Error committing:', err);
process.exit(1);
});