天天看點

JGitJGit

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”))

JGitJGit

擷取檢出指令對象

CheckoutCommand  cc=git.checkout()

JGitJGit

給檢出指令對象設定指定的送出版本号或分支名

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

JGitJGit

執行檢出指令

cc.call();

JGitJGit

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();
		}
	}