ci4sManagement-cloud/react-ui/scripts/git-merge.js

67 lines
1.9 KiB
JavaScript

#!/usr/bin/env node
const { exec, execSync } = require('child_process');
function usage() {
console.log('Usage: node scripts/git-merge.js <source> <target>');
console.log('Example: node scripts/git-merge.js dev-zw dev-opt');
}
const args = process.argv.slice(2);
let source;
let target;
// If one arg provided: it's the target, source is current branch.
// If two args provided: first is source, second is target.
if (args.length === 1) {
target = args[0];
let currentBranch;
try {
currentBranch = execSync('git rev-parse --abbrev-ref HEAD', { encoding: 'utf8' }).trim();
if (!currentBranch || currentBranch === 'HEAD') {
throw new Error('detached HEAD or empty branch');
}
} catch (err) {
console.error('无法检测当前 git 分支,请在一个 git 仓库的分支上运行,或者传入两个参数。');
console.error(err.message || err);
process.exitCode = 3;
return;
}
source = currentBranch;
console.log(`Detected current branch: ${source}`);
} else if (args.length === 2) {
[source, target] = args;
} else {
usage();
process.exitCode = 1;
return;
}
const baseUrl = 'https://www.gitlink.org.cn/ci4s/ci4sManagement-cloud/compare';
// For compare URLs we want `compare/{target}...{source}` so that
// the page shows changes from source into target (base...head).
const url = `${baseUrl}/${encodeURIComponent(target)}...${encodeURIComponent(source)}`;
console.log(`Opening: ${url}`);
function openUrl(u) {
const platform = process.platform;
let cmd;
if (platform === 'darwin') cmd = `open "${u}"`;
else if (platform === 'win32') cmd = `start "" "${u}"`;
else cmd = `xdg-open "${u}"`;
exec(cmd, (err) => {
if (err) {
console.error('Failed to open URL in default browser:', err.message || err);
console.log('You can open it manually:', u);
process.exitCode = 2;
}
});
}
openUrl(url);