使用 child_process.spawn 运行的 Git 命令找不到 git-lfs 子命令

分享于2022年11月15日 electron git git-lfs macos node.js 问答
【问题标题】:Git command run with child_process.spawn is unable to find git-lfs subcommand使用 child_process.spawn 运行的 Git 命令找不到 git-lfs 子命令
【发布时间】:2022-08-19 08:29:27
【问题描述】:

我有一个在 macOS Monterey (v12.5) 上运行的电子应用程序 (Electron v17.4.10),在启动时它会尝试执行命令 git lfs install 。它通过使用 Node child_process 包的 spawn 命令来调用对独立 git 可执行文件的直接调用来实现这一点。

安装电子应用程序时,它会提供自己的 git 版本,该版本未添加到 PATH 以防止干扰可能已安装的任何 git 版本。独立版本的 git (v2.33.0) 具有如下图所示的文件夹结构,并且 git-lfs 二进制文件 (v3.2.0) 包含在 /PortableGit/git/libexec/git-core/

Standalone Git Directory Structure

执行的完整命令是:
\"/Applications/MyApp/Utilities/PortableGit/bin/git\" lfs install

输出是:

git: \'lfs\' is not a git command. See \'git --help\'.

The most similar command is
    log

如何让 git 独立安装以识别子命令 lfs 而无需将其放在 PATH 上?像我描述的那样有一个独立的 git 包是可能的吗?还是我应该放弃这种方法并确保在目标机器上正确安装了 git?


【解决方案1】:

这个问题的答案是 Electron 的 process.env 与目标系统的 env 不同。因此,虽然 Git 会在 PATH 上查找子模块,但终端中使用的 PATH 将与子进程使用的 PATH 不同。

process.env.PATH 中附加一个“:”,后跟存储 git-lfs 二进制文件的路径,这将允许独立 git 安装正确识别 git-lfs 子命令。

IE。

process.env.PATH = process.env.PATH + ":" + "path/to/git/lfs"

【讨论】: