Node.js 生成参数导致 git update-index 失败

Node.js spawn arguments causing git update-index to fail

我有一个简单的功能运行git update-index

exports.gitUpdateIndex = (path, pattern) => {
  return new Promise((resolve, reject) => {
    const error = [];
    const opt = {
      cwd: path
    };
    const process = spawn("git", ["update-index", "--chmod=+x", pattern], opt);
    process.on("close", () => {
      if (error.length > 0) {
        reject(error);
      }
      resolve();
    });
    process.stderr.on("data", data => error.push(data.toString().trim()));
  });
};

我试着这样称呼它 -

await gitUpdateIndex(dirPath, "./*.sh");

但这是抛出一个错误 -

[
  "Ignoring path *.sh\nfatal: git update-index: cannot chmod +x '*.sh'"
]

编辑:

似乎将绝对路径传递给函数而不是 unix glob 模式修复了它。

await gitUpdateIndex(dirPath, "C:\test\hello.sh");

您必须将每个参数定义为单独的数组元素:

spawn("git", ['update-index', '--chmod=+x', pattern], opt)

您目前所做的相当于

git 'update-index --chmod=+x ./*.sh'

(注意引号)