JGit:找不到教程或简单的例子 [英] JGit: Cannot find a tutorial or simple example
问题描述
我找不到JGit的工作教程。
I cannot find a working tutorial for JGit.
如果任何人有一个很好的链接或一个简单的例子(f.ex. cat-a -file)(如何cat一个文件的答案JGit?不能用JGit V.1.0编译)
It would be very helpful if anyone has a good link or a simple example (f.ex. cat-a-file) that works (The answer from How to "cat" a file in JGit? doesn't compile with JGit V.1.0).
不幸的是,官方的 JGit文档对我来说不是很有用。但是我认为使用JGit的开发人员必须很多。
Unfortunately the official JGit documentation for beginners is not very useful to me. But I think there must be a lot developers who are using JGit?
推荐答案
事实上,使用API。
Indeed, there is very little around in terms of documented usage of the API.
为了其他读者的意图,我特此为最常见的操作提供简单的测试:
For the sake of other readers, I hereby provide a simple test for the most common operations:
- 创建存储库
- 克隆它
- 添加文件
- 推送
- 跟踪
origin / master
into主人
(如果你克隆了一个裸的回购,这是必要的) - 拉(在这种情况下,无论如何,无论如何)
<将
- Create the repository
- Clone it
- Add a file
- Commit
- Push
- Track
origin/master
intomaster
(this is necessary if you clone a bare repo) - Pull (uselessly, in this case, but whatever)
特别要注意,添加文件需要模式,而不是路径。此外,由于克隆中存在 master
,跟踪需要 .setForce(true)
。
In particular, note that adding files requires a pattern, not a path. Also, tracking requires .setForce(true)
due to the existence of master
on the clone.
请考虑这个例子是简单和自包含的。
Please consider that this example is meant to be simple and self-contained.
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.errors.*;
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.junit.Before;
import org.junit.Test;
public class TestJGit {
private String localPath, remotePath;
private Repository localRepo;
private Git git;
@Before
public void init() throws IOException {
localPath = "/home/me/repos/mytest";
remotePath = "git@github.com:me/mytestrepo.git";
localRepo = new FileRepository(localPath + "/.git");
git = new Git(localRepo);
}
@Test
public void testCreate() throws IOException {
Repository newRepo = new FileRepository(localPath + ".git");
newRepo.create();
}
@Test
public void testClone() throws IOException, GitAPIException {
Git.cloneRepository().setURI(remotePath)
.setDirectory(new File(localPath)).call();
}
@Test
public void testAdd() throws IOException, GitAPIException {
File myfile = new File(localPath + "/myfile");
myfile.createNewFile();
git.add().addFilepattern("myfile").call();
}
@Test
public void testCommit() throws IOException, GitAPIException,
JGitInternalException {
git.commit().setMessage("Added myfile").call();
}
@Test
public void testPush() throws IOException, JGitInternalException,
GitAPIException {
git.push().call();
}
@Test
public void testTrackMaster() throws IOException, JGitInternalException,
GitAPIException {
git.branchCreate().setName("master")
.setUpstreamMode(SetupUpstreamMode.SET_UPSTREAM)
.setStartPoint("origin/master").setForce(true).call();
}
@Test
public void testPull() throws IOException, GitAPIException {
git.pull().call();
}
}
这篇关于JGit:找不到教程或简单的例子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!