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
There are a number of ways to connect your project with JGit and start writing code against it.
11
-
Probably the easiest is to use Maven – the integration is accomplished by adding the following snipped to the `<dependencies>` tag in your pom.xml file:
@@ -19,115 +19,115 @@ 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.
23
-
Once this step is done, Maven will automatically acquire and use the JGit libraries that you'll need.
JGit has two basic levels of API: plumbing and porcelain.
37
-
The terminology for these comes from Git itself, and JGit is divided into roughly the same kinds of areas: porcelain APIs are a friendly front-end for common user-level actions (the sorts of things a normal user would use the Git command-line tool for), while the plumbing APIs are for interacting with low-level repository objects directly.
36
+
JGit 的 API 有两种基本的层次:底层命令和高层命令。
37
+
这个两个术语都来自 Git ,并且 JGit 也被按照相同的方式粗略地划分:高层 API 是一个面向普通用户级别功能的友好的前端(一系列普通用户使用 Git 命令行工具时可能用到的东西),底层 API 则直接作用于低级的仓库对象。
38
38
39
-
The starting point for most JGit sessions is the `Repository` class, and the first thing you'll want to do is create an instance of it.
40
-
For a filesystem-based repository (yes, JGit allows for other storage models), this is accomplished using `FileRepositoryBuilder`:
Repository existingRepo = new FileRepositoryBuilder()
50
50
.setGitDir(new File("my_repo/.git"))
51
51
.build();
52
52
----
53
53
54
-
The builder has a fluent API for providing all the things it needs to find a Git repository, whether or not your program knows exactly where it's located.
55
-
It can use environment variables (`.readEnvironment()`), start from a place in the working directory and search (`.setWorkTree(…).findGitDir()`), or just open a known `.git` directory as above.
54
+
无论你的程序是否知道仓库的确切位置,builder 中的那个流畅的 API 都可以提供给它寻找仓库所需所有信息。
String name = cfg.getString("user", null, "name");
88
88
----
89
89
90
-
There's quite a bit going on here, so let's go through it one section at a time.
90
+
这里完成了一大堆事情,所以我们还是一次理解一段的好。
91
91
92
-
The first line gets a pointer to the `master` reference.
93
-
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
-
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()`).
95
-
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.
The second line gets the target of the `master` reference, which is returned as an ObjectId instance.
98
-
ObjectId represents the SHA-1 hash of an object, which might or might not exist in Git's object database.
99
-
The third line is similar, but shows how JGit handles the rev-parse syntax (for more on this, see <<_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 next two lines show how to load the raw contents of an object.
102
-
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.
103
-
For large objects (where `.isLarge()` returns `true`), you can call `.openStream()` to get an InputStream-like object that can read the raw object data without pulling it all into memory at once.
The last example shows how to fetch the `user.name` value from the Git configuration files.
111
-
This Config instance uses the repository we opened earlier for local configuration, but will automatically detect the global and system configuration files and read values from them as well.
This is only a small sampling of the full plumbing API; there are many more methods and classes available.
114
-
Also not shown here is the way JGit handles errors, which is through the use of exceptions.
115
-
JGit APIs sometimes throw standard Java exceptions (such as `IOException`), but there are a host of JGit-specific exception types that are provided as well (such as `NoRemoteRepositoryException`, `CorruptObjectException`, and `NoMergeBaseException`).
117
+
==== 高层命令
116
118
117
-
==== Porcelain
118
-
119
-
The plumbing APIs are rather complete, but it can be cumbersome to string them together to achieve common goals, like adding a file to the index, or making a new commit.
120
-
JGit provides a higher-level set of APIs to help out with this, and the entry point to these APIs is the `Git` class:
119
+
底层 API 更加完善,但是有时将它们串起来以实现普通的目的非常困难,例如将一个文件添加到索引,或者创建一个新的提交。
120
+
为了解决这个问题, JGit 提供了一系列高层 API ,使用这些 API 的入口点就是 `Git` 类:
121
121
122
122
[source,java]
123
123
----
124
124
Repository repo;
125
-
// construct repo...
125
+
// 构建仓库。。。
126
126
Git git = new Git(repo);
127
127
----
128
128
129
-
The Git class has a nice set of high-level _builder_-style methods that can be used to construct some pretty complex behavior.
130
-
Let's take a look at an example – doing something like `git ls-remote`:
129
+
Git 类有一系列非常好的_构建器_风格的高层方法,它可以用来构造一些复杂的行为。
130
+
我们来看一个例子——做一件类似 `git ls-remote` 的事。
131
131
132
132
[source,java]
133
133
----
@@ -143,18 +143,18 @@ for (Ref ref : remoteRefs) {
143
143
}
144
144
----
145
145
146
-
This is a common pattern with the Git class; the methods return a command object that lets you chain method calls to set parameters, which are executed when you call `.call()`.
147
-
In this case, we're asking the `origin` remote for tags, but not heads.
148
-
Also notice the use of a `CredentialsProvider` object for authentication.
Many other commands are available through the Git class, including but not limited to `add`, `blame`, `commit`, `clean`, `push`, `rebase`, `revert`, and `reset`.
0 commit comments