代码如下:
import java.io.File;
import java.util.ArrayList;
import java.util.List;
import org.eclipse.jgit.api.CreateBranchCommand;
import org.eclipse.jgit.api.Git;
public class GitTest {
public static Git gitResult = null;
public static void main(String[] args) {
try {
File file = new File("/Users/laimuc/Documents/work/helloworld");
File gitFile = new File("/Users/laimuc/Documents/work/helloworld/.git");
if (gitFile.exists()) {
gitResult = Git.open(file);
gitResult.checkout().setCreateBranch(false).setName("daily").setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
gitResult.pull().call();
} else {
List<String> branchs = new ArrayList<String>();
branchs.add("refs/heads/daily");
gitResult = Git.cloneRepository().setURI("git@gitlab.xxxxx.com:group/helloworld.git").setBranchesToClone(branchs).setDirectory(file).call();
gitResult.checkout().setCreateBranch(true).setName("daily").setUpstreamMode(CreateBranchCommand.SetupUpstreamMode.TRACK).call();
gitResult.pull().call();
}
gitResult.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}