亲测成功!!!
实现效果:主要针对项目业务中,对代码上传方式Git/SVN的更新操作。做到当GIit仓库中出现版本提交时,将差异文件保存到本地。
之前接触的都是手动idea进行差异文件对比,还真没有通过代码的方式来读取过,经过实践成功读取差异文件,分享出来,大家一起学习,欢迎提出建议问题!!!
后序会更新 SVN 获取不同版本之间的增量文件(差异文件)
目录
1. 首先将代码克隆到本地/opt/code目录下(我这里放到的是/opt/code);
一、依赖:
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.13.1.202206130422-r</version>
</dependency>
二、业务逻辑
1. 首先将代码克隆到本地/opt/code目录下(我这里放到的是/opt/code);
2. 获取两次Git版本的id;
3. 比较不同版本之间的差异文件;
三、整体代码
@Test
public void test() {
// 初始化数据
String localPath = "/opt/code"; //本地存放代码地址
String url = "https://127.0.0.1:9999/git/code";//git远程地址
String branchName = "master";//git远程地址分支
String username = "";//git远程地址账户
String password = "";//git远程地址密码
// 1.首先将代码克隆到本地/opt/code目录下
Git git = null;
try {
//验证Git登录
CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username, password);
//将代码clone到本地
git = Git.cloneRepository().setURI(url).setDirectory(new File(localPath))
.setBranch(branchName)
.setCredentialsProvider(credentialsProvider).call();
// 2.获取两次版本的id
//记录第一次拉取的版本
ObjectId firstHeadId = getLatestCommitId(localPath);
//更新本地代码(Git仓库最新的代码)
git = Git.open(new File(localPath));
git.checkout().setCreateBranch(false).setName(branchName).call();
git.pull().setCredentialsProvider(credentialsProvider).call();
//记录第二次拉取的版本
ObjectId secondHeadId = getLatestCommitId(localPath);
//3. 比较不同版本之间的差异文件
// 比较两次代码的差异
List<DiffEntry> diffEntries = compareCommits(localPath, firstHeadId, secondHeadId);
if (!diffEntries.isEmpty()) {
for (DiffEntry diffEntry : diffEntries) {
String diffFilePath = diffEntry.getOldPath();
System.out.println("差异文件:" + diffFilePath);
//后续逻辑根据具体需求,自行添加
}
}
} catch (GitAPIException | IOException e) {
e.printStackTrace();
} finally {
if (git != null) {
git.close();
}
}
}
四、引用方法
/**
* 比较两个提交之间的差异
*/
public static List<DiffEntry> compareCommits(String repositoryPath, ObjectId oldCommitId, ObjectId newCommitId) throws IOException {
Repository repository = new RepositoryBuilder().setGitDir(new File(repositoryPath + "/.git")).build();
try (Git git = new Git(repository);
RevWalk revWalk = new RevWalk(repository)) {
RevCommit oldCommit = revWalk.parseCommit(oldCommitId);
RevCommit newCommit = revWalk.parseCommit(newCommitId);
// 准备比较的树对象
CanonicalTreeParser oldTreeParser = new CanonicalTreeParser();
oldTreeParser.reset(repository.newObjectReader(), oldCommit.getTree().getId());
CanonicalTreeParser newTreeParser = new CanonicalTreeParser();
newTreeParser.reset(repository.newObjectReader(), newCommit.getTree().getId());
// 比较两个树之间的差异
try (DiffFormatter diffFormatter = new DiffFormatter(DisabledOutputStream.INSTANCE)) {
diffFormatter.setRepository(repository);
return diffFormatter.scan(oldTreeParser, newTreeParser);
}
}
}
/**
* 获取代码的最新提交ID
*/
public static ObjectId getLatestCommitId(String repositoryPath) throws IOException {
Repository repository = new RepositoryBuilder().setGitDir(new File(repositoryPath + "/.git")).build();
try (Git git = new Git(repository)) {
Iterable<RevCommit> commits = null;
try {
commits = git.log().setMaxCount(1).call();
} catch (GitAPIException e) {
e.printStackTrace();
}
return commits.iterator().next().getId();
}
}
五,外部链接
下面是通过idea手动查看git不同版本之间的差异文件对比方式,
--------------------------------------------------------------转载-------------------------------------------------------------
git 代码不同版本的对比(IDEA)
原文链接:https://blog.youkuaiyun.com/qq_30624649/()article/details/115125872
Git 查看不同版本之间的差异和代码的改动
原文链接:https://www.itheima.com/news/20220623/163629.html