1

我想通过 Node.js 做到这一点:

git archive remote=git@example.com:repo.git HEAD | tar -x - -C /path/to/extracted/

所以,我写了这样的代码:

git = spawn 'git', ['archive', "--remote=git@example.com:repo.git", 'HEAD']
tar = spawn 'tar', ['-x', '-', '-C /path/to/extracted']

git.stderr.on 'data', (data) ->
  console.log "git error: #{data}"

git.stdout.on 'data', (data) ->
  tar.stdin.write data

tar.stderr.on 'data', (data) ->
  console.log "tar error: #{data}"

git.on 'exit', (code) ->
  console.log "git process done with code #{code}"

tar.on 'exit', (code) ->
  console.log "tar process done with code #{code}"

然而,这并不像我预期的那样工作。我应该改变什么才能使其正常工作?

提前致谢。

更新

-正确的命令实际上不需要-xand-C

git archive remote=git@example.com:repo.git HEAD | tar -x -C /path/to/extracted/
4

2 回答 2

0

在这种情况下,您应该使用函数 exec 而不是 spawn。如执行文档中所见,它接受管道执行文档

exec('git archive remote=git@example.com:repo.git HEAD | tar -x - -C /path/to/extracted/', function (error, stdout, stderr) {
    // logic here
}
于 2013-03-26T14:27:59.200 回答
0

使用 child_process.exec 时应注意命令注入。您可以使用 spawn 来完成类似的行为.pipe,您可以将其应用于流(这是 spawn 使用的)。https://nodejs.org/api/stream.html#stream_event_pipe

于 2018-04-10T09:32:53.317 回答