JGit

JGit

参考:https://yonge812.iteye.com/blog/1687480

概念:

就是用java代码实现git的命令行操作

JGit API:

https://download.eclipse.org/jgit/site/5.2.1.201812262042-r/apidocs/index.html

打开git仓库 

Git git=Git.open(new File(“FilePath”))

获取检出命令对象

CheckoutCommand  cc=git.checkout()

给检出命令对象设置指定的提交版本号或分支名

cc.setName(“版本号或者分支名”)

执行检出命令

cc.call();

 

TreeWalk

该类可以根据需要在尽可能多的树中执行n路差异。

RevWalk

遍历提交图并按顺序生成匹配的提交

 

应用:

需求:利用java程序和每次提交代码生成的commitid来获取该次提交的所有文件

公司的要求在每次提交工单,并且测试通过之后,要把该次提交生成的文件放到一个对应的工单文件夹并上传到指定仓库

本项目采用maven架构,出于内网环境的考虑,也可以改为普通java项目

需要用到的jar包(pom.xml):

		<dependency>
			<groupId>org.eclipse.jgit</groupId>
			<artifactId>org.eclipse.jgit</artifactId>
			<version>4.8.0.201706111038-r</version>
		</dependency>

常量类

设置参数

package com.wowowo.constant;

public class JGitConstant {
	/**
	 * 文件路径复制过来之后把\替换成/!!!!!
	 * 如果想要获取以前提交的文件,把flag设为true,并填写你当前版本的commitid,如果在提交之后直接获取修改的文件,flag设为false
	 * 
	 */
	// 仓库所在url
	public static final String gitRoot = "";
	// 该次提交的commitid,长id
	public static final String revision = "";
	// 要保存到的代码文件夹所在位置
	public static final String destPath = "";

	// --------------------------------------------------------------------------------
	// 如果想要获取以前提交的文件,把flag设为true
	public static final Boolean flag = false;
	// currentid填你当前版本的commitid
	public static final String currentid = "";
	// --------------------------------------------------------------------------------

}

getLog()

根据版本号版和其前一个版本的差异获取DiffEntry  list

代码解析:

根据commitid获取所有的revcommit 迭代器,里面存放历次提交信息

Iterator和Iterable接口的区别

https://www.cnblogs.com/keyi/p/5821285.html

拿出2个revocommit的revwalk

新建treewalk对象

把2个revwalk放入treewalk对象中

DiffEntiry.scan 方法传入treewalk参数,获取差异list

	public static List<DiffEntry> getLog() throws Exception {
		//load();
		git = Git.open(new File(gitRoot));
		// 我走了,本地版本切换该commitid的版本
		if (flag) {
			git.checkout().setName(revision).call();
		}
		Repository repository = git.getRepository();
		ObjectId objId = repository.resolve(revision);
		Iterable<RevCommit> allCommitsLater = git.log().add(objId).call();
		Iterator<RevCommit> iter = allCommitsLater.iterator();
		RevCommit commit = iter.next();
		TreeWalk tw = new TreeWalk(repository);
		tw.addTree(commit.getTree());
		commit = iter.next();
		if (commit != null)
			tw.addTree(commit.getTree());
		else
			return null;
		tw.setRecursive(true);
		RenameDetector rd = new RenameDetector(repository);
		rd.addAll(DiffEntry.scan(tw));

		return rd.compute();
	}

获取提交的文件名,拷贝文件

	public static void getCommitPath(List<DiffEntry> list) {
		try {
			for (DiffEntry diffEntry : list) {
				// 跳过删除的文件
				if (diffEntry.getOldPath().equals("/dev/null")) {
					System.out.println("删除了" + diffEntry.getNewPath());
					continue;
				} else if (diffEntry.getNewPath().equals("/dev/null")) {
					System.out.println("新建的文件:" + diffEntry.getOldPath());
				} else {
					System.out.println("修改的文件:" + diffEntry.getOldPath());
				}
				String srcFile = gitRoot + "/" + diffEntry.getOldPath();
				String destDir = destPath + "/"
						+ diffEntry.getOldPath().substring(0, diffEntry.getOldPath().lastIndexOf("/"));
				String destFile = destPath + "/" + diffEntry.getOldPath();
				fileDir = new File(destDir);
				fileDir.mkdirs();
				file = new File(destFile);
				System.out.println(destFile);
				file.createNewFile();
				CopyFiles.copy(new File(srcFile), new File(destFile));
				// System.out.println("要拷贝的源文件地址"+gitRoot +
				// diffEntry.getOldPath());
				// System.out.println("复制到的目标文件地址"+destPath +
				// diffEntry.getOldPath());
				System.out.println("**************************************************************************");
			}
			// 我又回来啦!,拷贝完毕,回到当前版本
			if (flag) {
				git.checkout().setName(currentid).call();
			}

		} catch (Exception e) {
			e.printStackTrace();
		}
	}