Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix-114 #118

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
7 changes: 6 additions & 1 deletion B-embedding-git.asc
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Even non-developer applications, such as document editors, could potentially ben
また、非開発者向けのアプリケーション(例えばドキュメントエディタなど)であっても、バージョン管理機能から利益を享受できる可能性があります。Gitのモデルは、様々なシナリオに上手く適合します。

//////////////////////////
If you need to integrate Git with your application, you have essentially three choices: spawning a shell and using the Git command-line tool; Libgit2; and JGit.
If you need to integrate Git with your application, you have essentially two options: spawn a shell and call the `git` command-line program, or embed a Git library into your application.
Here we'll cover command-line integration and several of the most popular embeddable Git libraries.
//////////////////////////
Gitをアプリケーションに統合する場合、やり方は大きく分けて3種類あります。1つ目はシェルのプロセスを生成してGitのコマンドラインツールを使う方法、2つ目はLibgit2を使う方法、3つ目はJGitを使う方法です。

Expand All @@ -22,3 +23,7 @@ include::book/B-embedding-git/sections/command-line.asc[]
include::book/B-embedding-git/sections/libgit2.asc[]

include::book/B-embedding-git/sections/jgit.asc[]

include::book/B-embedding-git/sections/go-git.asc[]

include::book/B-embedding-git/sections/dulwich.asc[]
44 changes: 44 additions & 0 deletions book/B-embedding-git/sections/dulwich.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
=== Dulwich

(((Dulwich)))(((Python)))
There is also a pure-Python Git implementation - Dulwich.
The project is hosted under https://www.dulwich.io/
It aims to provide an interface to git repositories (both local and remote) that doesn't call out to git directly but instead uses pure Python.
It has an optional C extensions though, that significantly improve the performance.

Dulwich follows git design and separate two basic levels of API: plumbing and porcelain.

Here is an example of using the lower level API to access the commit message of the last commit:

[source, python]
----
from dulwich.repo import Repo
r = Repo('.')
r.head()
# '57fbe010446356833a6ad1600059d80b1e731e15'

c = r[r.head()]
c
# <Commit 015fc1267258458901a94d228e39f0a378370466>

c.message
# 'Add note about encoding.\n'
----

To print a commit log using high-level porcelain API, one can use:

[source, python]
----
from dulwich import porcelain
porcelain.log('.', max_entries=1)

#commit: 57fbe010446356833a6ad1600059d80b1e731e15
#Author: Jelmer Vernooij <[email protected]>
#Date: Sat Apr 29 2017 23:57:34 +0000
----


==== Further Reading

* The official API documentation is available at https://www.dulwich.io/apidocs/dulwich.html[]
* Official tutorial at https://www.dulwich.io/docs/tutorial[] has many examples of how to do specific tasks with Dulwich
83 changes: 83 additions & 0 deletions book/B-embedding-git/sections/go-git.asc
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
=== go-git

(((go-git)))(((Go)))
In case you want to integrate Git into a service written in Golang, there also is a pure Go library implementation.
This implementation does not have any native dependencies and thus is not prone to manual memory management errors.
It is also transparent for the standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc.

go-git is focused on extensibility, compatibility and supports most of the plumbing APIs, which is documented at https://github.com/go-git/go-git/blob/master/COMPATIBILITY.md[].

Here is a basic example of using Go APIs:

[source, go]
----
import "github.com/go-git/go-git/v5"

r, err := git.PlainClone("/tmp/foo", false, &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
Progress: os.Stdout,
})
----

As soon as you have a `Repository` instance, you can access information and perform mutations on it:

[source, go]
----
// retrieves the branch pointed by HEAD
ref, err := r.Head()

// get the commit object, pointed by ref
commit, err := r.CommitObject(ref.Hash())

// retrieves the commit history
history, err := commit.History()

// iterates over the commits and print each
for _, c := range history {
fmt.Println(c)
}
----

==== Advanced Functionality

go-git has few notable advanced features, one of which is a pluggable storage system, which is similar to Libgit2 backends.
The default implementation is in-memory storage, which is very fast.

[source, go]
----
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{
URL: "https://github.com/go-git/go-git",
})
----

Pluggable storage provides many interesting options.
For instance, https://github.com/go-git/go-git/tree/master/_examples/storage[] allows you to store references, objects, and configuration in an Aerospike database.

Another feature is a flexible filesystem abstraction.
Using https://pkg.go.dev/github.com/go-git/go-billy/v5?tab=doc#Filesystem[] it is easy to store all the files in different way i.e by packing all of them to a single archive on disk or by keeping them all in-memory.

Another advanced use-case includes a fine-tunable HTTP client, such as the one found at https://github.com/go-git/go-git/blob/master/_examples/custom_http/main.go[].

[source, go]
----
customClient := &http.Client{
Transport: &http.Transport{ // accept any certificate (might be useful for testing)
TLSClientConfig: &tls.Config{InsecureSkipVerify: true},
},
Timeout: 15 * time.Second, // 15 second timeout
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return http.ErrUseLastResponse // don't follow redirect
},
}

// Override http(s) default protocol to use our custom client
client.InstallProtocol("https", githttp.NewClient(customClient))

// Clone repository using the new client if the protocol is https://
r, err := git.Clone(memory.NewStorage(), nil, &git.CloneOptions{URL: url})
----

==== Further Reading

A full treatment of go-git's capabilities is outside the scope of this book.
If you want more information on go-git, there's API documentation at https://pkg.go.dev/github.com/go-git/go-git/v5[], and a set of usage examples at https://github.com/go-git/go-git/tree/master/_examples[].
17 changes: 8 additions & 9 deletions book/B-embedding-git/sections/jgit.asc
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
=== JGit

(((jgit)))(((java)))
(((jgit)))(((Java)))
//////////////////////////
If you want to use Git from within a Java program, there is a fully featured Git library called JGit.
JGit is a relatively full-featured implementation of Git written natively in Java, and is widely used in the Java community.
The JGit project is under the Eclipse umbrella, and its home can be found at http://www.eclipse.org/jgit[].
The JGit project is under the Eclipse umbrella, and its home can be found at https://www.eclipse.org/jgit/[].
//////////////////////////
JavaのプログラムからGitを使いたい場合、十分な機能を備えたGitのライブラリであるJGitが利用できます。
JGitは、ネイティブJavaによるGitの実装です。Gitのほぼ全機能を備えており、Javaコミュニティで広く使われています。
Expand Down Expand Up @@ -32,14 +32,14 @@ JGitをあなたのプロジェクトへ追加して、コードを書き始め
----

//////////////////////////
The `version` will most likely have advanced by the time you read this; check http://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit[] for updated repository information.
The `version` will most likely have advanced by the time you read this; check https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit[] for updated repository information.
Once this step is done, Maven will automatically acquire and use the JGit libraries that you'll need.
//////////////////////////
皆さんがこれを読んでいる時には、おそらく `version` の番号はもっと進んでいるでしょうから、 http://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit[] で最新のリポジトリの情報を確認してください。
このステップが完了したら、以降は必要なJGitライブラリの取得と使用をMavenが自動的に行ってくれます。

//////////////////////////
If you would rather manage the binary dependencies yourself, pre-built JGit binaries are available from http://www.eclipse.org/jgit/download[].
If you would rather manage the binary dependencies yourself, pre-built JGit binaries are available from https://www.eclipse.org/jgit/download[].
You can build them into your project by running a command like this:
//////////////////////////
バイナリの依存関係を自前で管理したい場合は、ビルド済みのJGitのバイナリが http://www.eclipse.org/jgit/download[] から取得できます。
Expand Down Expand Up @@ -173,7 +173,7 @@ There's quite a bit going on here, so let's go through it one section at a time.

//////////////////////////
The first line gets a pointer to the `master` reference.
JGit automatically grabs the _actual_ master ref, which lives at `refs/heads/master`, and returns an object that lets you fetch information about the reference.
JGit automatically grabs the _actual_ `master` ref, which lives at `refs/heads/master`, and returns an object that lets you fetch information about the reference.
You can get the name (`.getName()`), and either the target object of a direct reference (`.getObjectId()`) or the reference pointed to by a symbolic ref (`.getTarget()`).
Ref objects are also used to represent tag refs and objects, so you can ask if the tag is ``peeled,'' meaning that it points to the final target of a (potentially long) string of tag objects.
//////////////////////////
Expand All @@ -185,7 +185,7 @@ JGitは `refs/heads/master` にある _実際の_ master参照を自動的に取
//////////////////////////
The second line gets the target of the `master` reference, which is returned as an ObjectId instance.
ObjectId represents the SHA-1 hash of an object, which might or might not exist in Git's object database.
The third line is similar, but shows how JGit handles the rev-parse syntax (for more on this, see <<ch07-git-tools#r_branch_references>>); you can pass any object specifier that Git understands, and JGit will return either a valid ObjectId for that object, or `null`.
The third line is similar, but shows how JGit handles the rev-parse syntax (for more on this, see <<ch07-git-tools#_branch_references>>); you can pass any object specifier that Git understands, and JGit will return either a valid ObjectId for that object, or `null`.
//////////////////////////
2行目では、 `master` 参照の指す先を取得して、ObjectIdインスタンスの形で返します。
ObjectIdはGitのオブジェクトデータベース中にある(または、データベース中にない)オブジェクトのSHA-1ハッシュを表しています。
Expand Down Expand Up @@ -252,7 +252,7 @@ Git git = new Git(repo);

//////////////////////////
The Git class has a nice set of high-level _builder_-style methods that can be used to construct some pretty complex behavior.
Let's take a look at an example doing something like `git ls-remote`:
Let's take a look at an example -- doing something like `git ls-remote`:
//////////////////////////
Gitクラスは、洗練された高レベルの _builder_ スタイルのメソッドを備えています。これは、非常に複雑な処理を組み立てる際に利用できます。
それでは例を見てみましょう。ここでは `git ls-remote` のような処理を行っています。
Expand Down Expand Up @@ -298,10 +298,9 @@ If you're interested and want to learn more, here's where to look for informatio
興味が湧いた、もっと知りたいということなら、情報は次の場所から探せます。

//////////////////////////
* The official JGit API documentation is available online at http://download.eclipse.org/jgit/docs/latest/apidocs[].
* The official JGit API documentation can be found at https://www.eclipse.org/jgit/documentation[].
These are standard Javadoc, so your favorite JVM IDE will be able to install them locally, as well.
* The JGit Cookbook at https://github.com/centic9/jgit-cookbook[] has many examples of how to do specific tasks with JGit.
* There are several good resources pointed out at http://stackoverflow.com/questions/6861881[].
//////////////////////////
* オフィシャルなJGit APIドキュメントは http://download.eclipse.org/jgit/docs/latest/apidocs[] で参照できます。
標準的なJavadocなので、ローカルにインストールして、好きなJVM IDEから参照することもできます。
Expand Down
26 changes: 13 additions & 13 deletions book/B-embedding-git/sections/libgit2.asc
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
//////////////////////////
Another option at your disposal is to use Libgit2.
Libgit2 is a dependency-free implementation of Git, with a focus on having a nice API for use within other programs.
You can find it at http://libgit2.github.com[].
You can find it at https://libgit2.org[].
//////////////////////////
あなたが取れる2つ目のオプションは、Libgit2を使用することです。
Libgit2は、他のプログラムへの依存性のないGitの実装であり、プログラムから使いやすいAPIを提供することにフォーカスしています。
Expand All @@ -22,7 +22,7 @@ Here's a whirlwind tour:

[source,c]
//////////////////////////
-----
----
// Open a repository
git_repository *repo;
int error = git_repository_open(&repo, "/path/to/repository");
Expand All @@ -41,7 +41,7 @@ const git_oid *tree_id = git_commit_tree_id(commit);
// Cleanup
git_commit_free(commit);
git_repository_free(repo);
-----
----
//////////////////////////
-----
// リポジトリを開く
Expand Down Expand Up @@ -76,7 +76,7 @@ There's also the `git_repository_open_ext` which includes options for searching,
他の方法としては、 `git_repository_open_ext` を使って検索オプション付きで開く方法、 `git_clone` とその仲間を使ってリモートリポジトリのローカルなクローンを作る方法、 `git_repository_init` を使って全く新規にリポジトリを作る方法があります。

//////////////////////////
The second chunk of code uses rev-parse syntax (see <<ch07-git-tools#r_branch_references>> for more on this) to get the commit that HEAD eventually points to.
The second chunk of code uses rev-parse syntax (see <<ch07-git-tools#_branch_references>> for more on this) to get the commit that HEAD eventually points to.
The type returned is a `git_object` pointer, which represents something that exists in the Git object database for a repository.
`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.
In this case, `git_object_type(commit)` would return `GIT_OBJ_COMMIT`, so it's safe to cast to a `git_commit` pointer.
Expand Down Expand Up @@ -132,7 +132,7 @@ tree = commit.tree

//////////////////////////
As you can see, the code is much less cluttered.
Firstly, Rugged uses exceptions; it can raise things like `ConfigError` or `ObjectError` to signal error conditions.
Firstly, Rugged uses exceptions; it can raise things like `ConfigError` or `ObjectError` to signal error conditions.
Secondly, there's no explicit freeing of resources, since Ruby is garbage-collected.
Let's take a look at a slightly more complicated example: crafting a commit from scratch
//////////////////////////
Expand Down Expand Up @@ -189,7 +189,7 @@ commit = repo.lookup(commit_id) # <8>

//////////////////////////
The Ruby code is nice and clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
If you're not a rubyist, we touch on some other bindings in <<r_libgit2_bindings>>.
If you're not a rubyist, we touch on some other bindings in <<_libgit2_bindings>>.
//////////////////////////
このRubyのコードは単純明快です。また、重い処理はLibgit2が行っているので、非常に高速に実行できます。
Rubyist でない方のために、 <<r_libgit2_bindings>> では他のバインディングにも触れています。
Expand Down Expand Up @@ -230,7 +230,7 @@ error = git_odb_add_backend(odb, my_backend, 1); // <3>

git_repository *repo;
error = git_repository_open(&repo, "some-path");
error = git_repository_set_odb(odb); // <4>
error = git_repository_set_odb(repo, odb); // <4>
----

//////////////////////////
Expand Down Expand Up @@ -300,7 +300,7 @@ Take a look at the `include/git2/sys/odb_backend.h` file in the Libgit2 source f
この初期化関数では、構造体にメモリを割り当て、カスタムコンテキストを設定し、それがサポートしている `parent` 構造体のメンバーへデータを設定しています。
その他の呼び出しのシグネチャについては、Libgit2のソースの `include/git2/sys/odb_backend.h` ファイルを見てみてください。ユースケースがはっきりしていれば、シグネチャのうちどれをサポートすればよいかを判断するのに役立つでしょう。

[[r_libgit2_bindings]]
[[_libgit2_bindings]]
//////////////////////////
==== Other Bindings
//////////////////////////
Expand Down Expand Up @@ -331,9 +331,9 @@ Here's what our example program looks like:
サンプルプログラムは次のようになります。

[source,csharp]
-----
----
new Repository(@"C:\path\to\repo").Head.Tip.Message;
-----
----

//////////////////////////
For desktop Windows applications, there's even a NuGet package that will help you get started quickly.
Expand All @@ -353,11 +353,11 @@ Objective-Git (https://github.com/libgit2/objective-git[]) は、そういった
サンプルプログラムは次のようになります。

[source,objc]
-----
----
GTRepository *repo =
[[GTRepository alloc] initWithURL:[NSURL fileURLWithPath: @"/path/to/repo"] error:NULL];
NSString *msg = [[[repo headReferenceWithError:NULL] resolvedTarget] message];
-----
----

//////////////////////////
Objective-git is fully interoperable with Swift, so don't fear if you've left Objective-C behind.
Expand All @@ -369,7 +369,7 @@ Objective-git は Swift に対しても完全な相互運用性があるので

(((Python)))
//////////////////////////
The bindings for Libgit2 in Python are called Pygit2, and can be found at http://www.pygit2.org/[].
The bindings for Libgit2 in Python are called Pygit2, and can be found at https://www.pygit2.org[].
Our example program:
//////////////////////////
Libgit2 の Python 向けバインディングは Pygit2 という名前で、 http://www.pygit2.org/[] から取得できます。
Expand Down
Loading