Skip to content

Commit 1b82cea

Browse files
committed
Merge branch 'Geno1024-B-3_jgit'
2 parents b8b6822 + 9767456 commit 1b82cea

File tree

2 files changed

+66
-66
lines changed

2 files changed

+66
-66
lines changed
Lines changed: 65 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
=== JGit
22

33
(((jgit)))(((java)))
4-
If you want to use Git from within a Java program, there is a fully featured Git library called JGit.
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[].
4+
如果你想在一个 Java 程序中使用 Git ,有一个功能齐全的 Git 库,那就是 JGit
5+
JGit 是一个用 Java 写成的功能相对健全的 Git 的实现,它在 Java 社区中被广泛使用。
6+
JGit 工程由 Eclipse 维护,它的主页在 http://www.eclipse.org/jgit[]
77

8-
==== Getting Set Up
8+
==== 起步
99

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

1313
[source,xml]
1414
----
@@ -19,115 +19,115 @@ Probably the easiest is to use Maven – the integration is accomplished by addi
1919
</dependency>
2020
----
2121

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.
22+
在你读到这段文字时 `version` 很可能已经更新了,所以请浏览 http://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit[] 以获取最新的仓库信息。
23+
当这一步完成之后, Maven 就会自动获取并使用你所需要的 JGit 库。
2424

25-
If you would rather manage the binary dependencies yourself, pre-built JGit binaries are available from http://www.eclipse.org/jgit/download[].
26-
You can build them into your project by running a command like this:
25+
如果你想自己管理二进制的依赖包,那么你可以从 http://www.eclipse.org/jgit/download[] 获得预构建的 JGit 二进制文件。
26+
你可以像下面这样执行一个命令来将它们构建进你的工程。
2727

2828
[source,shell]
2929
----
3030
javac -cp .:org.eclipse.jgit-3.5.0.201409260305-r.jar App.java
3131
java -cp .:org.eclipse.jgit-3.5.0.201409260305-r.jar App
3232
----
3333

34-
==== Plumbing
34+
==== 底层命令
3535

36-
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 则直接作用于低级的仓库对象。
3838

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`:
39+
大多数 JGit 会话会以 `Repository` 类作为起点,你首先要做的事就是创建一个它的实例。
40+
对于一个基于文件系统的仓库来说(嗯, JGit 允许其它的存储模型),用 `FileRepositoryBuilder` 完成它。
4141

4242
[source,java]
4343
----
44-
// Create a new repository; the path must exist
44+
// 创建一个新仓库,路径必须存在
4545
Repository newlyCreatedRepo = FileRepositoryBuilder.create(
4646
new File("/tmp/new_repo/.git"));
4747
48-
// Open an existing repository
48+
// 打开一个存在的仓库
4949
Repository existingRepo = new FileRepositoryBuilder()
5050
.setGitDir(new File("my_repo/.git"))
5151
.build();
5252
----
5353

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 都可以提供给它寻找仓库所需所有信息。
55+
它可以使用环境变量 (`.readEnvironment()`) ,从工作目录的某处开始并搜索 (`.setWorkTree(…).findGitDir()`) , 或者仅仅只是像上面那样打开一个已知的 `.git` 目录。
5656

57-
Once you have a `Repository` instance, you can do all sorts of things with it.
58-
Here's a quick sampling:
57+
当你拥有一个 `Repository` 实例后,你就能对它做各种各样的事。
58+
下面是一个速览:
5959

6060
[source,java]
6161
----
62-
// Get a reference
62+
// 获取引用
6363
Ref master = repo.getRef("master");
6464
65-
// Get the object the reference points to
65+
// 获取该引用所指向的对象
6666
ObjectId masterTip = master.getObjectId();
6767
6868
// Rev-parse
6969
ObjectId obj = repo.resolve("HEAD^{tree}");
7070
71-
// Load raw object contents
71+
// 装载对象原始内容
7272
ObjectLoader loader = r.open(masterTip);
7373
loader.copyTo(System.out);
7474
75-
// Create a branch
75+
// 创建分支
7676
RefUpdate createBranch1 = r.updateRef("refs/heads/branch1");
7777
createBranch1.setNewObjectId(masterTip);
7878
createBranch1.update();
7979
80-
// Delete a branch
80+
// 删除分支
8181
RefUpdate deleteBranch1 = r.updateRef("refs/heads/branch1");
8282
deleteBranch1.setForceUpdate(true);
8383
deleteBranch1.delete();
8484
85-
// Config
85+
// 配置
8686
Config cfg = r.getConfig();
8787
String name = cfg.getString("user", null, "name");
8888
----
8989

90-
There's quite a bit going on here, so let's go through it one section at a time.
90+
这里完成了一大堆事情,所以我们还是一次理解一段的好。
9191

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.
92+
第一行获取一个指向 `master` 引用的指针。
93+
JGit 自动抓取位于 `refs/heads/master` 的 _真正的_ master 引用,并返回一个允许你获取该引用的信息的对象。
94+
你可以获取它的名字 (`.getName()`) ,或者一个直接引用的目标对象 (`.getObjectId()`) ,或者一个指向该引用的符号指针 (`.getTarget()`) 。
95+
引用对象也经常被用来表示标签的引用和对象,所以你可以询问某个标签是否被 ``削除'' 了,或者说它指向一个标签对象的(也许很长的)字符串的最终目标。
9696

97-
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`.
97+
第二行获得以 `master` 引用的目标,它返回一个 ObjectId 实例。
98+
不管是否存在于一个 Git 对象的数据库,ObjectId 都会代表一个对象的 SHA-1 哈希。
99+
第三行与此相似,但是它展示了 JGit 如何处理 rev-parse 语法(要了解更多,请看 <<_branch_references>> ),你可以传入任何 Git 了解的对象说明符,然后 JGit 会返回该对象的一个有效的 ObjectId ,或者 `null` 。
100+
101+
接下来两行展示了如何装载一个对象的原始内容。
102+
在这个例子中,我们调用 `ObjectLoader.copyTo()` 直接向标准输出流输出对象的内容,除此之外 ObjectLoader 还带有读取对象的类型和长度并将它以字节数组返回的方法。
103+
对于一个( `.isLarge()` 返回 `true` 的)大的对象,你可以调用 `.openStream()` 来获得一个类似 InputStream 的对象,它可以在没有一次性将所有数据拉到内存的前提下读取对象的原始数据。
100104

101-
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.
105+
接下来几行展现了如何创建一个新的分支。
106+
我们创建一个 RefUpdate 实例,配置一些参数,然后调用 `.update()` 来确认这个更改。
107+
删除相同分支的代码就在这行下面。
108+
记住必须先 `.setForceUpdate(true)` 才能让它工作,否则调用 `.delete()` 只会返回 `REJECTED` ,然后什么都没有发生。
104109

105-
The next few lines show what it takes to create a new branch.
106-
We create a RefUpdate instance, configure some parameters, and call `.update()` to trigger the change.
107-
Directly following this is the code to delete that same branch.
108-
Note that `.setForceUpdate(true)` is required for this to work; otherwise the `.delete()` call will return `REJECTED`, and nothing will happen.
110+
最后一个例子展示了如何从 Git 配置文件中获取 `user.name` 的值。
111+
这个 Config 实例使用我们先前打开的仓库做本地配置,但是它也会自动地检测并读取全局和系统的配置文件。
109112

110-
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.
113+
这只是底层 API 的冰山一角,另外还有许多可以使用的方法和类。
114+
还有一个没有放在这里说明的,就是 JGit 是用异常机制来处理错误的。
115+
JGit API 有时使用标准的 Java 异常(例如 `IOException` ),但是它也提供了大量 JGit 自己定义的异常类型(例如 `NoRemoteRepositoryException`、 `CorruptObjectException` 和 `NoMergeBaseException`)。
112116

113-
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+
==== 高层命令
116118

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` 类:
121121

122122
[source,java]
123123
----
124124
Repository repo;
125-
// construct repo...
125+
// 构建仓库。。。
126126
Git git = new Git(repo);
127127
----
128128

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` 的事。
131131

132132
[source,java]
133133
----
@@ -143,18 +143,18 @@ for (Ref ref : remoteRefs) {
143143
}
144144
----
145145

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.
146+
这是一个 Git 类的公共样式,这个方法返回一个可以让你串连若干方法调用来设置参数的命令对象,当你调用 `.call()` 时它们就会被执行。
147+
在这情况下,我们只是请求了 `origin` 远程的标签,而不是头部。
148+
还要注意用于验证的 `CredentialsProvider` 对象的使用。
149149

150-
Many other commands are available through the Git class, including but not limited to `add`, `blame`, `commit`, `clean`, `push`, `rebase`, `revert`, and `reset`.
150+
Git 类中还可以使用许多其它的命令,包括但不限于 `add``blame``commit``clean``push``rebase``revert` `reset`
151151

152-
==== Further Reading
152+
==== 拓展阅读
153153

154-
This is only a small sampling of JGit's full capabilities.
155-
If you're interested and want to learn more, here's where to look for information and inspiration:
154+
这只是 JGit 的全部能力的冰山一角。
155+
如果你对这有兴趣并且想深入学习,在下面可以找到一些信息和灵感。
156156

157-
* The official JGit API documentation is available online at http://download.eclipse.org/jgit/docs/latest/apidocs[].
158-
These are standard Javadoc, so your favorite JVM IDE will be able to install them locally, as well.
159-
* The JGit Cookbook at https://github.com/centic9/jgit-cookbook[] has many examples of how to do specific tasks with JGit.
160-
* There are several good resources pointed out at http://stackoverflow.com/questions/6861881[].
157+
* JGit API 在线官方文档: http://download.eclipse.org/jgit/docs/latest/apidocs[]
158+
这是基本的 Javadoc ,所以你也可以在你最喜欢的 JVM IDE 上将它们安装它们到本地。
159+
* JGit Cookbook https://github.com/centic9/jgit-cookbook[] 拥有许多如何利用 JGit 实现特定任务的例子。
160+
* http://stackoverflow.com/questions/6861881[] 指出了几个好的资源。

status.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
"B-embedding-git": {
120120
"1-embedding-git.asc": 100,
121121
"sections/command-line.asc": 100,
122-
"sections/jgit.asc": 0,
122+
"sections/jgit.asc": 100,
123123
"sections/libgit2.asc": 100
124124
},
125125
"C-git-commands": {

0 commit comments

Comments
 (0)