Jgit获取最近一次commit的文件和文件列表
pom<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM
·
pom
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.xs</groupId>
<artifactId>git_package</artifactId>
<version>1.0</version>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<encoding>UTF-8</encoding>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.3.8</version>
</dependency>
<dependency>
<groupId>org.eclipse.jgit</groupId>
<artifactId>org.eclipse.jgit</artifactId>
<version>5.1.3.201810200350-r</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<version>2.3.1.RELEASE</version>
<configuration>
<mainClass>
com.xs.git.JGitTest
</mainClass>
</configuration>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
package com.xs.git;
import cn.hutool.core.io.FileUtil;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.diff.DiffEntry;
import org.eclipse.jgit.lib.ObjectId;
import org.eclipse.jgit.lib.ObjectReader;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.treewalk.CanonicalTreeParser;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* @Author xs
* @Date 2020/7/15 14:19
* @Description 用JGit获取提交记录
* 1.将程序放在git项目下
*/
public class JGitTest {
public static void main(String[] args) throws IOException, GitAPIException {
System.out.println("com.xs.git_package Start!");
System.out.println();
try (Repository repository = openJGitCookbookRepository()) {
//^表示前几个父提交 ^^ 前两次和^前一次的对比
ObjectId oldHead = repository.resolve("HEAD^^^{tree}");
ObjectId head = repository.resolve("HEAD^{tree}");
//获取classpath路径 /C:/HRP/HRP/out/production/HRP1/
String classPath = JGitTest.class.getResource("/").getPath().substring(1);
String basePath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
System.out.println("线程获取:"+basePath);
//获取项目路径 //C:\HRP\HRP
File directory = new File("");
String projectPath = directory.getCanonicalPath() + "\\";
String sourcePath = "";
File toFile = new File("D:/web-config/package");
File txtFile = new File("D:/web-config/package/package.txt");
//不存在则创建目录
if (!toFile.exists()) {
toFile.mkdirs();
}
if (!txtFile.exists()) {
toFile.mkdir();
}
ArrayList arrayList = new ArrayList();
// prepare the two iterators to compute the diff between
try (ObjectReader reader = repository.newObjectReader()) {
CanonicalTreeParser oldTreeIter = new CanonicalTreeParser();
oldTreeIter.reset(reader, oldHead);
CanonicalTreeParser newTreeIter = new CanonicalTreeParser();
newTreeIter.reset(reader, head);
// finally get the list of changed files
try (Git git = new Git(repository)) {
List<DiffEntry> diffs = git.diff()
.setNewTree(newTreeIter)
.setOldTree(oldTreeIter)
.call();
for (DiffEntry entry : diffs) {
//增加和修改的文件进行打包,删除的过滤掉
if ("DELETE".equals(entry.getChangeType().name())) {
return;
}
//todo 文件名相同的复制出现问题
String filePath = entry.getNewPath();
//如果文件后缀为"java"的进行找class复制在指定路径下
if (filePath.endsWith("java")) {
filePath = filePath.substring(0, filePath.length() - 4) + "class";
filePath = filePath.substring(4, filePath.length());
arrayList.add(filePath);
sourcePath = classPath + filePath;
File sourceFile = new File(sourcePath);
FileUtil.copy(sourceFile, toFile, true);
}
//其他js,jsp复制到
if (filePath.endsWith("js")
|| filePath.endsWith("jsp")
|| filePath.endsWith("SQL")
|| filePath.endsWith("sql")
|| filePath.endsWith("cpt")
|| filePath.endsWith("frm")
) {
File sourceFile = new File(projectPath + filePath);
arrayList.add(filePath);
FileUtil.copy(sourceFile, toFile, false);
}
System.out.println(entry);
}
} catch (Exception e) {
}
// 写进打包的文本文件中
FileUtil.writeLines(arrayList, txtFile, "UTF-8");
}
}
System.out.println();
System.out.println("打包文件输出为:D:\\web-config\\package !");
System.out.println("com.xs.git_package Done!");
}
public static Repository openJGitCookbookRepository() throws IOException {
FileRepositoryBuilder builder = new FileRepositoryBuilder();
return builder
.readEnvironment() // scan environment GIT_* variables
.findGitDir() // scan up the file system tree
.build();
}
}
更多推荐
已为社区贡献1条内容
所有评论(0)