Files
huishou/git-push2.js
T
2026-07-27 14:07:26 +08:00

76 lines
2.3 KiB
JavaScript

const { exec } = require('child_process');
const fs = require('fs');
const gitPath = 'D:\\Git\\bin\\git.exe';
const cwd = __dirname;
function runGit(cmd, env) {
return new Promise((resolve, reject) => {
console.log(`Running: ${cmd}`);
const child = exec(cmd, { cwd, env, timeout: 120000 }, (error, stdout, stderr) => {
if (error) {
console.error(`Error: ${error.message}`);
console.error(`stderr: ${stderr}`);
reject(error);
} else {
console.log(`stdout: ${stdout.slice(0, 2000)}`);
resolve({ stdout, stderr });
}
});
});
}
async function main() {
try {
// Remove index.lock if exists
console.log('\n=== Step 1: Remove index.lock ===');
const lockFile = `${cwd}\\.git\\index.lock`;
if (fs.existsSync(lockFile)) {
fs.unlinkSync(lockFile);
console.log('Removed index.lock');
}
// Set git config
console.log('\n=== Step 2: Set git config ===');
await runGit(`${gitPath} config user.email "jiapengyu@example.com"`);
await runGit(`${gitPath} config user.name "jiapengyu"`);
// Add all files
console.log('\n=== Step 3: Add all files ===');
await runGit(`${gitPath} add --all`);
// Check status
console.log('\n=== Step 4: Check git status ===');
const status = await runGit(`${gitPath} status --short`);
console.log('Status:', status.stdout);
// Commit
console.log('\n=== Step 5: Commit ===');
await runGit(`${gitPath} commit -m "Initial commit - full huishou project"`);
// Set remote
console.log('\n=== Step 6: Set remote ===');
try {
await runGit(`${gitPath} remote add origin git@git.torchcloud.cn:jiapengyu/huishou.git`);
} catch (e) {
console.log('Remote already exists, updating...');
await runGit(`${gitPath} remote set-url origin git@git.torchcloud.cn:jiapengyu/huishou.git`);
}
// Push
console.log('\n=== Step 7: Push ===');
const env = { ...process.env };
env.GIT_SSH_COMMAND = 'ssh -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null';
await runGit(`${gitPath} push -f origin master`, env);
console.log('\n=== Success! ===');
} catch (error) {
console.error('\n=== Failed ===');
console.error(error.message);
process.exit(1);
}
}
main();