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
In case you want to integrate Git into a service written in Golang, there also is a pure Go library implementation.
5
+
This implementation does not have any native dependencies and thus is not prone to manual memory management errors.
6
+
It is also transparent for the standard Golang performance analysis tooling like CPU, Memory profilers, race detector, etc.
7
+
8
+
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[].
Pluggable storage provides many interesting options.
54
+
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.
55
+
56
+
Another feature is a flexible filesystem abstraction.
57
+
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.
58
+
59
+
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[].
60
+
61
+
[source, go]
62
+
----
63
+
customClient := &http.Client{
64
+
Transport: &http.Transport{ // accept any certificate (might be useful for testing)
A full treatment of go-git's capabilities is outside the scope of this book.
83
+
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[].
Copy file name to clipboardExpand all lines: book/B-embedding-git/sections/jgit.asc
+8-8Lines changed: 8 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,9 +1,9 @@
1
1
=== JGit
2
2
3
-
(((jgit)))(((java)))
3
+
(((jgit)))(((Java)))
4
4
If you want to use Git from within a Java program, there is a fully featured Git library called JGit.
5
5
JGit is a relatively full-featured implementation of Git written natively in Java, and is widely used in the Java community.
6
-
The JGit project is under the Eclipse umbrella, and its home can be found at http://www.eclipse.org/jgit[].
6
+
The JGit project is under the Eclipse umbrella, and its home can be found at https://www.eclipse.org/jgit/[].
7
7
8
8
==== Getting Set Up
9
9
@@ -19,10 +19,10 @@ Probably the easiest is to use Maven – the integration is accomplished by addi
19
19
</dependency>
20
20
----
21
21
22
-
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.
22
+
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.
23
23
Once this step is done, Maven will automatically acquire and use the JGit libraries that you'll need.
24
24
25
-
If you would rather manage the binary dependencies yourself, pre-built JGit binaries are available from http://www.eclipse.org/jgit/download[].
25
+
If you would rather manage the binary dependencies yourself, pre-built JGit binaries are available from https://www.eclipse.org/jgit/download[].
26
26
You can build them into your project by running a command like this:
27
27
28
28
[source,console]
@@ -91,13 +91,13 @@ String name = cfg.getString("user", null, "name");
91
91
There's quite a bit going on here, so let's go through it one section at a time.
92
92
93
93
The first line gets a pointer to the `master` reference.
94
-
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.
94
+
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.
95
95
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()`).
96
-
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.
96
+
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.
97
97
98
98
The second line gets the target of the `master` reference, which is returned as an ObjectId instance.
99
99
ObjectId represents the SHA-1 hash of an object, which might or might not exist in Git's object database.
100
-
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`.
100
+
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`.
101
101
102
102
The next two lines show how to load the raw contents of an object.
103
103
In this example, we call `ObjectLoader.copyTo()` to stream the contents of the object directly to stdout, but ObjectLoader also has methods to read the type and size of an object, as well as return it as a byte array.
@@ -128,7 +128,7 @@ Git git = new Git(repo);
128
128
----
129
129
130
130
The Git class has a nice set of high-level _builder_-style methods that can be used to construct some pretty complex behavior.
131
-
Let's take a look at an example – doing something like `git ls-remote`:
131
+
Let's take a look at an example -- doing something like `git ls-remote`:
The `git_repository` type represents a handle to a repository with a cache in memory.
35
35
This is the simplest method, for when you know the exact path to a repository's working directory or `.git` folder.
36
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.
37
37
38
-
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.
38
+
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.
39
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.
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
41
In this case, `git_object_type(commit)` would return `GIT_OBJ_COMMIT`, so it's safe to cast to a `git_commit` pointer.
42
42
43
43
The next chunk shows how to access the commit's properties.
@@ -66,7 +66,7 @@ tree = commit.tree
66
66
----
67
67
68
68
As you can see, the code is much less cluttered.
69
-
Firstly, Rugged uses exceptions; it can raise things like `ConfigError` or `ObjectError`to signal error conditions.
69
+
Firstly, Rugged uses exceptions; it can raise things like `ConfigError` or `ObjectError` to signal error conditions.
70
70
Secondly, there's no explicit freeing of resources, since Ruby is garbage-collected.
71
71
Let's take a look at a slightly more complicated example: crafting a commit from scratch
<8> The return value is the SHA-1 hash of a new commit object, which you can then use to get a `Commit` object.
107
107
108
108
The Ruby code is nice and clean, but since Libgit2 is doing the heavy lifting, this code will run pretty fast, too.
109
-
If you're not a rubyist, we touch on some other bindings in <<r_libgit2_bindings>>.
110
-
109
+
If you're not a rubyist, we touch on some other bindings in <<_libgit2_bindings>>.
111
110
112
111
==== Advanced Functionality
113
112
114
113
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.
114
+
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
115
Libgit2 allows custom backends for configuration, ref storage, and the object database, among other things.
_(Note that errors are captured, but not handled. We hope your code is better than ours.)_
136
+
_Note that errors are captured, but not handled. We hope your code is better than ours._
138
137
139
-
<1> Initialize an empty object database (ODB) ``frontend,'' which will act as a container for the ``backends'' which are the ones doing the real work.
138
+
<1> Initialize an empty object database (ODB) "`frontend,`" which will act as a container for the "`backends`" which are the ones doing the real work.
140
139
<2> Initialize a custom ODB backend.
141
140
<3> Add the backend to the frontend.
142
141
<4> Open a repository, and set it to use our ODB to look up objects.
@@ -179,15 +178,14 @@ The rest of it is arbitrary; this structure can be as large or small as you need
179
178
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
179
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.
181
180
182
-
[[r_libgit2_bindings]]
181
+
[[_libgit2_bindings]]
183
182
==== Other Bindings
184
183
185
184
Libgit2 has bindings for many languages.
186
185
Here we show a small example using a few of the more complete bindings packages 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
186
The official collection of bindings can be found by browsing the repositories at https://github.com/libgit2[].
188
187
The code we'll write will return the commit message from the commit eventually pointed to by HEAD (sort of like `git log -1`).
189
188
190
-
191
189
===== LibGit2Sharp
192
190
193
191
(((.NET)))(((C#)))(((Mono)))
@@ -196,9 +194,9 @@ The bindings are written in C#, and great care has been taken to wrap the raw Li
196
194
Here's what our example program looks like:
197
195
198
196
[source,csharp]
199
-
-----
197
+
----
200
198
new Repository(@"C:\path\to\repo").Head.Tip.Message;
201
-
-----
199
+
----
202
200
203
201
For desktop Windows applications, there's even a NuGet package that will help you get started quickly.
204
202
@@ -210,19 +208,18 @@ Objective-Git (https://github.com/libgit2/objective-git[]) is the name of the Li
0 commit comments