JGIT基本CURD操作,获取提交记录,文件内容比对,获取暂存区文件,指定文件等操作
package utils;
import org.eclipse.jgit.api.*;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.diff.DiffFormatter;
import org.eclipse.jgit.diff.RenameDetector;
import org.eclipse.jgit.lib.*;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevTree;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.eclipse.jgit.treewalk.AbstractTreeIterator;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import org.eclipse.jgit.treewalk.TreeWalk;
import org.eclipse.jgit.treewalk.filter.PathFilterGroup;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*@author Mr.Ye
*@ClassName GitUtils
*@Date 2022/3/21
*@describe http://wiki.eclipse.org/JGit/User_Guide (eclipse-JGIT社区)
*@describe https://github.com/centic9/jgit-cookbook (开源api)
*@describe https://www.136.la/tech/show-875175.html (刚开始可以看这个,参考GitUtils.test()方法)
* @describe 使用工具类以后需要主动调用GIT.close()方法,关闭git
*/
public class GitUtils {
private static final DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
private static final Logger logger = LoggerFactory.getLogger(GitUtils.class);
private final static String REF_HEADS = "refs/heads/";
/** 本地远程仓库--用于文件比较 默认路径不对外展示*/
private static String localRemotePath = "/local-remote/";
/**
* 初始化url
* 默认远程仓库的项目名称为最后一个路径
*/
public static HashMap<String, String> initUrl(String remotePath,String localPath){
String path =remotePath.split("\\.git")[0];
String[] split= path.split("/");
String ss = split[split.length-1];
//本地地址
localPath = localPath+ss;
//本地远程地址 用于比较
localRemotePath = localRemotePath+ss;
HashMap<String, String> map = new HashMap<>();
map.put("localPath",localPath);
map.put("remotePath",remotePath);
map.put("localRemotePath",localRemotePath);
return map;
}
/**
* 克隆远程仓库
* @param localPath 本地地址 -(本地地址不存在,使用这个方法)
* @param remotePath 远程仓库
* @param provider 秘钥
* @param masterBranch 分支
* @return
*/
public static Git cloneRepository(String localPath, String remotePath,
UsernamePasswordCredentialsProvider provider,
String masterBranch) throws GitAPIException {
Git git = Git.cloneRepository()
.setCredentialsProvider(provider)
.setURI(remotePath)
.setRemote(masterBranch)
.setDirectory(new File(localPath))
.call();
return git;
}
/**
* 本地地址存在(使用这个方法)
* @return
*/
public static Git initGit(String localPath) {
File file = new File(localPath);
logger.info("本地文件路径"+localPath);
Git git = null;
/*路径是否存在*/
if(file.exists()) {
try {
git = Git.open(new File(localPath));
} catch (IOException e) {
e.printStackTrace();
git.close();
}
}
return git;
}
/**
* 初始化 将本地代码关联远程仓库,并推送数据至远程仓库(每个仓库只需要使用一次)
* @return
*/
public static Git onlyOnceGitInit(String localPath,String remotePath,
UsernamePasswordCredentialsProvider provider,String banceName)
throws GitAPIException, URISyntaxException {
/*初始化*/
Git git = Git.init().setDirectory(new File(localPath)).call();
/*关联远程仓库*/
git.remoteAdd().setName(banceName).setUri(new URIish(remotePath)).call();
/*本地暂存*/
git.add().addFilepattern(".").call();
/*本地提交*/
git.commit().setAll(true).setMessage("Initialize commit").call();
/*拉取最新代码*/
git.pull().setRemote(banceName).call();
/*推送到远程仓库*/
git.push().setRemote(banceName).setCredentialsProvider(provider).call();
return git;
}
/**
* 全部推送,返回提交的id
* --url 为空则默认全部推送
* --不为空则可以选择地址推送
* @throws GitAPIException
* @param git git
* @param message 提交信息
*/
public static String push(Git git,String message,List<String> url,UsernamePasswordCredentialsProvider provider,String banceName)
throws GitAPIException{
/*路径为空则暂存所有文件*/
AddCommand addCmd = git.add();
if (url==null ||url.size()<=0){
addCmd.addFilepattern(".");
}else {
for (String s : url) {
addCmd.addFilepattern(s);
}
}
addCmd.call();
/*提交到本