You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The`git_repository`type represents a handle to a repository with a cache in memory.
35
-
This is the simplest method, for when you know the exact path to a repository's working directory or `.git`folder.
36
-
There's also the `git_repository_open_ext`which includes options for searching, `git_clone`and friends for making a local clone of a remote repository, and `git_repository_init`for creating an entirely new repository.
The second chunk of code uses rev-parse syntax (see <<_branch_references>> for more on this) to get the commit that HEAD eventually points to.
39
-
The type returned is a `git_object`pointer, which represents something that exists in the Git object database for a repository.
40
-
`git_object`is actually a ``parent'' type for several different kinds of objects; the memory layout for each of the ``child'' types is the same as for `git_object`, so you can safely cast to the right one.
41
-
In this case, `git_object_type(commit)`would return `GIT_OBJ_COMMIT`, so it's safe to cast to a `git_commit`pointer.
38
+
第二段代码使用了一种 rev-parse 语法(要了解更多,请看 <<_branch_references>> )来得到 HEAD 真正指向的提交。
That last one means it isn't very probable that you'll be writing C when using Libgit2.
56
-
Fortunately, there are a number of language-specific bindings available that make it fairly easy to work with Git repositories from your specific language and environment.
57
-
Let's take a look at the above example written using the Ruby bindings for Libgit2, which are named Rugged, and can be found at https://github.com/libgit2/rugged[].
Libgit2 has a couple of capabilities that are outside the scope of core Git.
115
-
One example is pluggability: Libgit2 allows you to provide custom ``backends'' for several types of operation, so you can store things in a different way than stock Git does.
116
-
Libgit2 allows custom backends for configuration, ref storage, and the object database, among other things.
The code below is borrowed from the set of backend examples provided by the Libgit2 team (which can be found at https://github.com/libgit2/libgit2-backends[]).
120
-
Here's how a custom backend for the object database is set up:
Well, that's the constructor for your own ODB implementation, and you can do whatever you want in there, so long as you fill in the `git_odb_backend` structure properly.
The subtlest constraint here is that `my_backend_struct`'s first member must be a `git_odb_backend` structure; this ensures that the memory layout is what the Libgit2 code expects it to be.
177
-
The rest of it is arbitrary; this structure can be as large or small as you need it to be.
The initialization function allocates some memory for the structure, sets up the custom context, and then fills in the members of the `parent` structure that it supports.
180
-
Take a look at the `include/git2/sys/odb_backend.h` file in the Libgit2 source for a complete set of call signatures; your particular use case will help determine which of these you'll want to support.
Here we show a small example using a few of the more complete bindings pakages as of this writing; libraries exist for many other languages, including C++, Go, Node.js, Erlang, and the JVM, all in various stages of maturity.
187
-
The official collection of bindings can be found by browsing the repositories at https://github.com/libgit2[].
188
-
The code we'll write will return the commit message from the commit eventually pointed to by HEAD (sort of like `git log -1`).
pygit2.Repository("/path/to/repo") # open repository
231
-
.head.resolve() # get a direct ref
232
-
.get_object().message # get commit, read message
230
+
pygit2.Repository("/path/to/repo") # 打开版本库
231
+
.head.resolve() # 获取直接引用
232
+
.get_object().message # 获取提交,读取信息。
233
233
----
234
234
235
235
236
-
==== Further Reading
236
+
==== 扩展阅读
237
237
238
-
Of course, a full treatment of Libgit2's capabilities is outside the scope of this book.
239
-
If you want more information on Libgit2 itself, there's API documentation at https://libgit2.github.com/libgit2[], and a set of guides at https://libgit2.github.com/docs[].
240
-
For the other bindings, check the bundled README and tests; there are often small tutorials and pointers to further reading there.
238
+
当然,完全阐述 Libgit2 的能力已超出本书范围。
239
+
如果你想了解更多关于 Libgit2 的信息,可以浏览它的 API 文档: https://libgit2.github.com/libgit2[], 以及一系列的指南: https://libgit2.github.com/docs[].
0 commit comments