In fact, there is very little API documented use.
For other readers, I hereby give a simple test for the most common operations:
- Create repository
- Clone it
- to add a file
- Fix
- Click
- Track
origin/master in master (this is necessary if you are cloning a bare repo) - Pull (useless, in this case, but whatever)
In particular, note that adding a file requires a template , not a path. Additionally, tracking requires .setForce(true) due to the existence of master on the clone.
Please note that this example should be simple and self-sufficient.
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(); } }
Luca Geretti Apr 25 '12 at 7:19 2012-04-25 07:19
source share