spring boot 使用 jgit编写的git客户端

一.引入依赖 

<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();
			}
		}
	}

 

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值