Ответ 1
В самом деле, в плане документированного использования API очень мало.
Для других читателей я настоящим даю простой тест для наиболее распространенных операций:
- Создать репозиторий
- Clone it
- Добавить файл
- Фиксировать
- Нажмите
- Отслеживать
origin/master
вmaster
(это необходимо, если вы клонируете голый репо) - Потяните (бесполезно, в этом случае, но что угодно)
В частности, обратите внимание, что для добавления файлов требуется шаблон, а не путь. Кроме того, для отслеживания требуется .setForce(true)
из-за существования master
на клоне.
Пожалуйста, учтите, что этот пример должен быть простым и самодостаточным.
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 = "[email protected]: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();
}
}