英文原文正文为项目原始 README(英文),本站后续会翻译为中文,当前仅剔除图片与无关章节并统一排版。
NodeGit
Node 与 libgit2 项目的绑定。
稳定版 (libgit2@v0.28.3): 0.28.3
有问题吗?来和我们聊天吧!
访问 slack.libgit2.org 注册,然后加入我们的 #nodegit。
由以下维护
Alex Aveillán @AlexaXs 在大量 优秀贡献者 的帮助下完成!
校友维护者
Ian Hattendorf @ianhattendorf,John Alden @zawata,Tyler Ang-Wanek @twwanek,Tim Branyen @tbranyen,John Haley @johnhaley81,Max Korp @maxkorp,Steve Smith @orderedlist,Michael Robinson @codeofinterest,和 Nick Kallen @nk
API 文档。
入门指南。
NodeGit 可以在大多数系统上开箱即用,无需任何本地依赖。
npm install nodegit
如果你在构建 Travis-CI 时收到关于 libstdc 的错误,这类错误很常见,你可以通过升级到最新的 libstdc -4.9 来解决。
在 Ubuntu 中:
sudo add-apt-repository ppa:ubuntu-toolchain-r/test
sudo apt-get update
sudo apt-get install libstdc++-4.9-dev
在特拉维斯:
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- libstdc++-4.9-dev
在 CircleCI 中:
dependencies:
pre:
- sudo add-apt-repository -y ppa:ubuntu-toolchain-r/test
- sudo apt-get update
- sudo apt-get install -y libstdc++-4.9-dev
如果你收到关于 lifecycleScripts preinstall/install 的错误,你可能缺少 Ubuntu 中的 libssl-dev:
sudo apt-get install libssl-dev
您需要在您的 Linux 机器上安装以下库:
- libpcre
- libpcreposix
- libkrb5
- libk5crypto
- libcom_err
在本地构建时,您还需要 Kerberos 和 PCRE 的开发包,因此这两个工具都必须存在于您的机器上:
- pcre-config
- krb5-配置
如果在安装过程中仍然遇到问题,您应该尝试 从源码构建 的说明。
API 示例。
克隆一个仓库并读取文件:
var Git = require("nodegit");
// Clone a given repository into the `./tmp` folder.
Git.Clone("https://github.com/nodegit/nodegit", "./tmp")
// Look up this known commit.
.then(function(repo) {
// Use a known commit sha from this repository.
return repo.getCommit("59b20b8d5c6ff8d09518454d4dd8b7b30f095ab5");
})
// Look up a specific file within that commit.
.then(function(commit) {
return commit.getEntry("README.md");
})
// Get the blob contents from the file.
.then(function(entry) {
// Patch the blob to contain a reference to the entry.
return entry.getBlob().then(function(blob) {
blob.entry = entry;
return blob;
});
})
// Display information about the blob.
.then(function(blob) {
// Show the path, sha, and filesize in bytes.
console.log(blob.entry.path() + blob.entry.sha() + blob.rawsize() + "b");
// Show a spacer.
console.log(Array(72).join("=") + "\n\n");
// Show the entire file.
console.log(String(blob));
})
.catch(function(err) { console.log(err); });
模拟 git log:
var Git = require("nodegit");
// Open the repository directory.
Git.Repository.open("tmp")
// Open the master branch.
.then(function(repo) {
return repo.getMasterCommit();
})
// Display information about commits on master.
.then(function(firstCommitOnMaster) {
// Create a new history event emitter.
var history = firstCommitOnMaster.history();
// Create a counter to only show up to 9 entries.
var count = 0;
// Listen for commit events from the history.
history.on("commit", function(commit) {
// Disregard commits past 9.
if (++count >= 9) {
return;
}
// Show the commit sha.
console.log("commit " + commit.sha());
// Store the author object.
var author = commit.author();
// Display author information.
console.log("Author:\t" + author.name() + " <" + author.email() + ">");
// Show the commit date.
console.log("Date:\t" + commit.date());
// Give some space and show the message.
console.log("\n " + commit.message());
});
// Start emitting events.
history.start();
});
更多示例,请查看 examples/ 文件夹。
单元测试。
在运行测试之前,您需要在本地进行构建。请参见上文。
npm test- 本文标题:nodegit - Git 的原生 Node 绑定
- 本文链接:https://cn121.com/desktop/nodegit-nodegit.html
- 原项目:nodegit/nodegit 版权归原作者 nodegit 及贡献者所有
- 收录信息:本站于 2026-10-18 收录本项目,本页所列协议与仓库指标均为收录当时的状态;该日期之后原项目的版本更新与协议变更,本页不作同步。
- 开源协议:收录时本项目采用 MIT(查看 LICENSE 原文),本站转载其原始文档(未改动文字,仅剔除图片与无关章节);使用、修改、分发请以该仓库 LICENSE 原文为准。
- 站点出处:本文首发于 OneTwoOne,收录自 GitHub 开源项目 nodegit/nodegit。
- 内容说明:本页正文为原项目 README 原文(英文),本站后续会翻译为中文(当前尚未译出,仅将二级标题译为中文,便于按栏目定位;标题原文可在下方原仓库中查看),仅剔除了图片与赞助等无关章节、并把相对链接改为绝对地址;页首简介为机器翻译自仓库描述。
- 引用声明:商业转载、第三方聚合或 AI 检索训练引用时,请务必保留以上来源出处、本文永久链接,以及原项目的版权声明与许可信息。
- 下架通道:若原项目此后变更或收紧了许可协议、或作者/权利人认为本站的收录方式(译文、排版适配、简介翻译等)超出其授权范围,请通过 xyd3302001@163.com 发送下架通知,并附上项目地址与本页链接。本站核实后将第一时间删除本页内容,或改为不复制原文的目录性收录;署名更正等其他要求可一并提出。