【发布时间】:2018-03-28 15:36:39
【问题描述】:
我正在尝试使用节点脚本以编程方式将分支合并到主分支中。我尝试的第一件事是来自Execute a command line binary with Node.js,它可以很好地执行终端命令。但是我在更改工作目录时遇到了困难。我想将工作目录更改为 git 存储库,然后从那里运行 git 命令。执行cd ~/repo 或cd /home/me/repo 根本没有改变工作目录,除了使用cd 之外我找不到任何方法来改变它。
然后我尝试从脚本目录执行 git 命令,但指向我的仓库。我尝试了git --git-dir 和git --work-tree,但这些命令都不起作用。错误是一样的:fatal: Not a git repository (or any of the parent directories): .git 我猜这是因为运行脚本的目录不是 git repo。
所以,我要么想将 git 命令发送到我运行脚本的目录以外的目录,要么我想改变脚本的工作目录。最好是后者。我的完整代码和输出如下:
import JiraData from './jiradata';
const jiraData = new JiraData();
const { exec } = require('child_process');
(async function () {
const cards = await jiraData.getCardsInTest();
let commands = ['cd /home/me/repo', 'pwd'];
let proceeding = true;
// for (const card of cards) {
// commands.push(`git merge origin/${card} --commit --no-edit`);
// }
for (const command of commands) {
if (proceeding) {
console.log(`Executing command "${command}"`);
exec(command, (err, stdout, stderr) => {
if (err) {
proceeding = false;
console.log(stderr);
return;
}
console.log(stdout);
})
}
}
})();
输出:
> ci@1.0.0 start /home/me/CI
> babel-node index.js --presets es2015,stage-2
Executing command "cd /home/me/repo"
Executing command "pwd"
/home/me/CI
【问题讨论】:
-
试试
git --git-dir=/home/me/repo/.git --work-tree=/home/me/repo merge foo。不要使用~替换路径中的/home/me。 -
您将其作为子进程而不是使用 npm git 包的任何特殊原因?使用它大约容易一百万倍......npmjs.com/package/nodegit