Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 8 additions & 7 deletions book/B-embedding-git/sections/jgit.asc
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ JGit 项目由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit[]
==== 起步

有很多种方式可以让 JGit 连接你的项目,并依靠它去写代码。
最简单的方式也许就是使用 Maven 。你可以通过在你的 pom.xml 文件里的 `dependencies` 标签中增加像下面这样的片段来完成这个整合。
最简单的方式也许就是使用 Maven 。你可以通过在你的 pom.xml 文件里的 `<dependencies>` 标签中增加像下面这样的片段来完成这个整合。

[source,xml]
----
Expand All @@ -25,7 +25,7 @@ JGit 项目由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit[]
如果你想自己管理二进制的依赖包,那么你可以从 http://www.eclipse.org/jgit/download[] 获得预构建的 JGit 二进制文件。
你可以像下面这样执行一个命令来将它们构建进你的项目。

[source,shell]
[source,console]
----
javac -cp .:org.eclipse.jgit-3.5.0.201409260305-r.jar App.java
java -cp .:org.eclipse.jgit-3.5.0.201409260305-r.jar App
Expand All @@ -41,9 +41,10 @@ JGit 的 API 有两种基本的层次:底层命令和高层命令。

[source,java]
----
// 创建一个新仓库,路径必须存在
// 创建一个新仓库
Repository newlyCreatedRepo = FileRepositoryBuilder.create(
new File("/tmp/new_repo/.git"));
newlyCreatedRepo.create();

// 打开一个存在的仓库
Repository existingRepo = new FileRepositoryBuilder()
Expand All @@ -69,21 +70,21 @@ ObjectId masterTip = master.getObjectId();
ObjectId obj = repo.resolve("HEAD^{tree}");

// 装载对象原始内容
ObjectLoader loader = r.open(masterTip);
ObjectLoader loader = repo.open(masterTip);
loader.copyTo(System.out);

// 创建分支
RefUpdate createBranch1 = r.updateRef("refs/heads/branch1");
RefUpdate createBranch1 = repo.updateRef("refs/heads/branch1");
createBranch1.setNewObjectId(masterTip);
createBranch1.update();

// 删除分支
RefUpdate deleteBranch1 = r.updateRef("refs/heads/branch1");
RefUpdate deleteBranch1 = repo.updateRef("refs/heads/branch1");
deleteBranch1.setForceUpdate(true);
deleteBranch1.delete();

// 配置
Config cfg = r.getConfig();
Config cfg = repo.getConfig();
String name = cfg.getString("user", null, "name");
----

Expand Down
5 changes: 3 additions & 2 deletions book/B-embedding-git/sections/libgit2.asc
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,9 @@ Python 的 Libgit2 绑定叫做 Pygit2 ,你可以在 http://www.pygit2.org/[]
[source,python]
----
pygit2.Repository("/path/to/repo") # 打开版本库
.head.resolve() # 获取直接引用
.get_object().message # 获取提交,读取信息。
.head # get the current branch
.peel(pygit2.Commit) # walk down to the commit
.message # read the message
----


Expand Down