Skip to content

Instantly share code, notes, and snippets.

@davidyang013
Created November 25, 2014 10:53
Show Gist options
  • Save davidyang013/d1f6968f68b921588e88 to your computer and use it in GitHub Desktop.
Save davidyang013/d1f6968f68b921588e88 to your computer and use it in GitHub Desktop.
JGit example
package com.ericsson.iptv.testcase;
import java.io.File;
import java.io.IOException;
import org.eclipse.jgit.api.CreateBranchCommand.SetupUpstreamMode;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.api.errors.JGitInternalException;
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 = "/Users/david/Desktop/jgit";
remotePath = "https://github.com/davidyang013/HLS.git";
localRepo = new FileRepository(localPath);
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();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment