https://blog.csdn.net/qq_37203082/article/details/120327084
Jgit 工具类 (代码检出、删除分支(本地、远程)、新建分支、切换分支、代码提交)_jgit删除远程分支_CJ点的博客-CSDN博客
<!--JAVA操作GIT-->
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>3.7.0.201502260915-r</version>
</dependency>
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.ListBranchCommand;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
import java.util.List;
/**
* desciption Java-git工具类
* @author cjw
* @Date 2021/9/13 17:35
*/
public class JgitUtil {
private static final Logger log = LoggerFactory.getLogger(JgitUtil.class);
/**
* desciption 检出仓库代码
* @param url git仓库连接
* @param localPath 代码文件夹
* @param gitName git账号
* @param gitPassword git密码
* @return
* @Date 2021/9/13 17:43
*/
public static Boolean cloneRepository(String url, String localPath,String gitName,String gitPassword) {
Git git =null;
try {
log.info("JgitUtil.cloneRepository;开始检出Master代码;git路径:{},检出路径:{}",url,localPath);
File file=new File(localPath);
if(file.isDirectory()){
log.info("JgitUtil.cloneRepository;该路径:{},已存在文件夹,删除原文件,进行覆盖",localPath);
deleteFile(file);
}
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitName, gitPassword);
git = Git.cloneRepository()
.setURI(url)
.setDirectory(new File(localPath))
.setCloneAllBranches(true)
.setCredentialsProvider(credentialsProvider)
.call();
return true;
} catch (Exception e) {
log.error("JgitUtil.cloneRepository;错误;检出Master代码异常;检出路径:{},异常信息:{}",url,e);
return false;
} finally{
if (git != null) {
git.close();
}
}
}
/**
* desciption 切换分支
* @param localPath
* @param branchName
* @param gitName git账号
* @param gitPassword git密码
* @Date 2021/9/13 17:48
*/
public static Boolean checkoutBranch(String localPath, String branchName,String gitName,String gitPassword){
log.info("JgitUtil.checkoutBranch;切换分支;{}",branchName);
String projectURL = localPath + "\\.git";
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitName, gitPassword);
Git git = null;
try {
git = Git.open(new File(projectURL));
git.checkout().setCreateBranch(false).setName(branchName).call();
git.pull().setCredentialsProvider(credentialsProvider).call();
return true;
}catch (Exception e){
log.error("JgitUtil.checkoutBranch;错误;切换分支失败;异常信息:{}",e);
return false;
} finally{
if (git != null) {
git.close();
}
}
}
/**
* desciption 提交代码
* @param localPath 代码目录
* @param pushMessage 提交信息
* @param gitName git账号
* @param gitPassword git密码
* @return
* @Date 2021/9/13 17:52
*/
public static Boolean gitCommitAndPush(String localPath,String pushMessage,String gitName,String gitPassword) {
log.info("JgitUtil.gitCommitAndPush;提交代码;信息:{}",pushMessage);
String projectURL = localPath + "\\.git";
Git git = null;
try {
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitName, gitPassword);
git = Git.open(new File(projectURL));
git.pull().setCredentialsProvider(credentialsProvider).call();
git.add().addFilepattern(".").call();
git.commit().setMessage(pushMessage).call();
git.pull().setCredentialsProvider(credentialsProvider).call();
git.push().setCredentialsProvider(credentialsProvider).call();
return true;
} catch (Exception e){
log.error("JgitUtil.gitCommitAndPush;错误;提交代码;信息:{};异常信息:{}",pushMessage,e);
return false;
} finally{
if (git != null) {
git.close();
}
}
}
/**
* desciption 创建新的分支
* @param localPath 代码目录
* @param baranchName
* @param gitName git账号
* @param gitPassword git密码
* @return
* @Date 2021/9/13 17:58
*/
public static Boolean newBranch(String localPath,String baranchName,String gitName,String gitPassword) {
log.info("JgitUtil.newBranch;创建新的分支;名称{}",baranchName);
Git git = null;
try {
String projectURL = localPath + "\\.git";
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(gitName, gitPassword);
git = Git.open(new File(projectURL));
//检查新建的分支是否已经存在,如果存在则将已存在的分支强制删除并新建一个分支
List<Ref> refs = git.branchList().setListMode(ListBranchCommand.ListMode.ALL).call();
for (Ref ref : refs) {
String bName = ref.getName().substring(ref.getName().lastIndexOf("/") + 1);
if (ref.getName().substring(ref.getName().lastIndexOf("/") + 1).equals(baranchName)) {
log.info("JgitUtil.newBranch;分支已存在;进行删除操作,再新建;{}",baranchName);
//删除本地分支
git.branchDelete().setBranchNames(bName).setForce(true)
.call();
//删除远程分支
//这里的用法和网上不同,可能因为版本的原因导致
RefSpec refSpec3 = new RefSpec()
.setSource(null)
.setDestination("refs/heads/"+bName);
git.push().setRefSpecs(refSpec3).setRemote("origin").setCredentialsProvider(credentialsProvider).call();
break;
}
}
// //新建分支
Ref ref = git.branchCreate().setName(baranchName).call();
git.push().add(ref).setCredentialsProvider(credentialsProvider).call();
return true;
} catch (Exception ex) {
log.error("JgitUtil.newBranch;错误;创建新的分支,{}",ex);
return false;
}finally{
if (git != null) {
git.close();
}
}
}
/**
* 先根遍历序递归删除文件夹
* @param dirFile 要被删除的文件或者目录
* @return 删除成功返回true, 否则返回false
*/
public static boolean deleteFile(File dirFile) {
// 如果dir对应的文件不存在,则退出
if (!dirFile.exists()) {
return false;
}
if (dirFile.isFile()) {
return dirFile.delete();
} else {
for (File file : dirFile.listFiles()) {
deleteFile(file);
}
}
return dirFile.delete();
}
public static void main(String[] args) {
JgitUtil gitUtil = new JgitUtil();
//git远程url地址
// String url = "XXXX.git";
String url = "http://11.11.11.11:9799/cjw/auto.git";
String branchName = "buriedpoint-demo3";
String localPath = "D:\\data"+File.separator+branchName;
String gitName="xxx";
String gitPassword="xxxxxx";
try {
//拉取master代码
// JgitUtil.cloneRepository(url,localPath,gitName, gitPassword);
//创建分支
// JgitUtil.newBranch(localPath,branchName,gitName, gitPassword);
// //切换分支
// JgitUtil.checkoutBranch(localPath,branchName,gitName, gitPassword);
// //代码提交
// JgitUtil.gitCommitAndPush(localPath,"测试提交1",gitName, gitPassword);
} catch (Exception e) {
e.printStackTrace();
}
}
}