【问题标题】:Node.js spawn arguments causing git update-index to failNode.js 生成参数导致 git update-index 失败
【发布时间】:2020-04-01 11:33:11
【问题描述】:

我有一个简单的函数来运行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");

【问题讨论】:

    标签: javascript node.js git ecmascript-6 spawn


    【解决方案1】:

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

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

    你目前正在做相当于

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

    (注意引号)

    【讨论】:

    • 我根据您的建议更新了答案,但现在它抛出了一个不同的错误。
    猜你喜欢
    • 1970-01-01
    • 2012-12-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-12-28
    • 2011-08-31
    • 1970-01-01
    • 2017-10-06
    相关资源
    最近更新 更多