一.引入依赖
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.1.3.201810200350-r</version>
<optional>true</optional>
</dependency>
二 .
import java.io.File;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.HttpConfig;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
public class GitUtil {
//private static Log log = LogFactory.getLog(GitUtil.class);
private GitUtil() {
}
public static Git getGit(String uri, CredentialsProvider credentialsProvider, String localDir) throws Exception {
Git git = null;
if (new File(localDir).exists() ) {
git = Git.open(new File(localDir));
} else {
git = Git.cloneRepository().setCredentialsProvider(credentialsProvider).setURI(uri)
.setDirectory(new File(localDir)).call();
}
//设置一下post内存,否则可能会报错Error writing request body to server
git.getRepository().getConfig().setInt(HttpConfig.HTTP, null, HttpConfig.POST_BUFFER_KEY, 512*1024*1024);
return git;
}
public static CredentialsProvider getCredentialsProvider(String username, String password) {
return new UsernamePasswordCredentialsProvider(username, password);
}
public static Repository getRepository(Git git) {
return git.getRepository();
}
public static void pull(Git git, CredentialsProvider credentialsProvider) throws Exception {
git.pull().setRemote("origin").setCredentialsProvider(credentialsProvider).call();
}
public static void push(Git git, CredentialsProvider credentialsProvider, String filepattern, String message)
throws Exception {
git.add().addFilepattern(filepattern).call();
git.add().setUpdate(true);
git.commit().setMessage(message).call();
git.push().setCredentialsProvider(credentialsProvider).call();
}
public static void main(String[] args) throws Exception {
String uri = "http://127.0.0.1:3000/XXX/git_test.git";
String username = "XXX";
String password = "123456";
CredentialsProvider credentialsProvider = getCredentialsProvider(username, password);
String localDir = "D:/tmp/git_test";
Git git = getGit(uri, credentialsProvider, localDir);
pull(git, credentialsProvider);
push(git, credentialsProvider, ".", "提交文件");
}
}
三
一、切换分支遇到的问题
1.org.eclipse.jgit.api.errors.NoHeadException: Cannot checkout from unborn branch
2.pull权限不够
git.pull().call
问题:
org.eclipse.jgit.api.errors.TransportException: http://igit.xxxx.com/xxx/platform/xxxx: Authentication is required but no CredentialsProvider has been registered
解决办法:设置用户名和密码
增加方法:setCredentialsProvider(usernamePasswordCredentialsProvider)
即:git.pull().setCredentialsProvider(usernamePasswordCredentialsProvider).call();
3.修改密码后重新clone代码权限不够(window7操作系统)
原因:git客户端之前操作的原因
解决:重置git的配置文件
命令:git config --system --unset credential.helper
重置后,重新clone 拉取,正常
代码:
private Git git=null;
public void CommitCode(String proName,String proPath) throws IOException, NoFilepatternException, GitAPIException {
try{
Repository existingRepo = new FileRepositoryBuilder().setGitDir(new File(proPath+"\\.git")).build();
git = new Git(existingRepo);
//true if no differences exist between the working-tree, the index, and the current HEAD, false if differences do exist
if(git.status().call().isClean()==false){
git.add().addFilepattern(".").call();
SimpleDateFormat ymd = new SimpleDateFormat("yyyyMMdd");
SimpleDateFormat hm = new SimpleDateFormat("HHmm");
git.commit().setMessage(proName+"_"+ymd.format(new Date())+"_"+hm.format(new Date())).call();
git.push().call();
MyLog.logger.info("------succeed add,commit,push files . to repository at " + existingRepo.getDirectory());
}else{ //clean
MyLog.logger.info("\n-------code is clean------");
}
}finally{
if(git !=null){
git.close();
}
}
}