从 Node 的 API 运行 git Blame
我正在构建一个 VS Code 扩展,它作为该过程的一部分运行 gitblame。
我读到我可以使用 Node 的 API 从 JavaScript 执行命令行命令,使用 子进程 类似这个:
var exec = require('child_process').exec;
exec(`ls`
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
运行这个非常简单的 ls 命令会在控制台中打印出目录。
但是,当我用 exec(gitblame -L ${startLine},${endLine}extension.ts
替换 exec(ls
) 时,它说我不在 git 中请注意,
我也尝试使用 sudo 运行该命令,但它会抛出错误
:需要终端来读取密码;或者使用 -S 选项来读取密码。从标准输入或配置询问通行证助手 sudo:需要密码
/private/var/folders/th/40j77cv1587_zndgl34g0m100000gn/T/AppTranslocation/C0CFF9C7-F5C7-4C37-B7BD-1990096D4E41/d/Visual Studio Code.app/Contents/Resources/app/out/bootstrap-fork.js:5
我该怎么做才能使其正常工作?
I'm building a VS Code extension which runs git blame as part of the process.
I read that I can execute command line commands from JavaScript with Node's API, using a child process like this:
var exec = require('child_process').exec;
exec(`ls`
function (error, stdout, stderr) {
console.log('stdout: ' + stdout);
console.log('stderr: ' + stderr);
if (error !== null) {
console.log('exec error: ' + error);
}
});
Running this very simple ls command does print out in the console the directory.
However, when I replace exec(ls
with exec(git blame -L ${startLine},${endLine} extension.ts
it says I'm not in a git repository. Please note that executing the git blame command from my actual terminal does work.
I tried running the command with sudo as well. It throws the error
sudo: a terminal is required to read the password; either use the -S option to read from standard input or configure an askpass helper
sudo: a password is required
/private/var/folders/th/40j77cv1587_zndgl34g0m100000gn/T/AppTranslocation/C0CFF9C7-F5C7-4C37-B7BD-1990096D4E41/d/Visual Studio Code.app/Contents/Resources/app/out/bootstrap-fork.js:5
What can I do to make it work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
结果是 Node 的子进程在根目录而不是工作目录上运行命令。
由于我正在开发 VS Code 扩展,
var currentOpenTabfilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
解决了它。值得一提的是,我尝试了不同的方法那些不起作用的事情。它们是:
Turns out that Node's child process was running the command on the root directory, not the working directory.
Since I'm developing a VS Code extension,
var currentlyOpenTabfilePath = vscode.window.activeTextEditor?.document.uri.fsPath;
solved it.It's worth mentioning that I tried different things that didn't work. Those are: