最近項目中要做一個回滾功能,目的是如果這次發布出現了問題,立馬回滾到上一次發布的版本,用jgit實現的,具體方法如下:
- public class GitUtil {
- private final static String GIT = ".git";
- /**
- * 將文件列表提交到git倉庫中
- * @param gitRoot git倉庫目錄
- * @param files 需要提交的文件列表
- * @return 返回本次提交的版本號
- * @throws IOException
- */
- public static String commitToGitRepository(String gitRoot, List<String> files) throws Exception {
- if (StringUtils.isNotBlank(gitRoot) && files != null && files.size() > 0) {
- File rootDir = new File(gitRoot);
- //初始化git倉庫
- if (new File(gitRoot + File.separator + GIT).exists() == false) {
- Git.init().setDirectory(rootDir).call();
- }
- //打開git倉庫
- Git git = Git.open(rootDir);
- //判斷是否有被修改過的文件
- List<DiffEntry> diffEntries = git.diff()
- .setPathFilter(PathFilterGroup.createFromStrings(files))
- .setShowNameAndStatusOnly(true).call();
- if (diffEntries == null || diffEntries.size() == 0) {
- throw new Exception("提交的文件內容都沒有被修改,不能提交");
- }
- //被修改過的文件
- List<String> updateFiles=new ArrayList<String>();
- ChangeType changeType;
- for(DiffEntry entry : diffEntries){
- changeType = entry.getChangeType();
- switch (changeType) {
- case ADD:
- updateFiles.add(entry.getNewPath());
- break;
- case COPY:
- updateFiles.add(entry.getNewPath());
- break;
- case DELETE:
- updateFiles.add(entry.getOldPath());
- break;
- case MODIFY:
- updateFiles.add(entry.getOldPath());
- break;
- case RENAME:
- updateFiles.add(entry.getNewPath());
- break;
- }
- }
- //將文件提交到git倉庫中,並返回本次提交的版本號
- AddCommand addCmd = git.add();
- for (String file : updateFiles) {
- addCmd.addFilepattern(file);
- }
- addCmd.call();
- CommitCommand commitCmd = git.commit();
- for (String file : updateFiles) {
- commitCmd.setOnly(file);
- }
- RevCommit revCommit = commitCmd.setCommitter(Constants.USERNAME, Constants.EMAIL)
- .setMessage("publish").call();
- return revCommit.getName();
- }
- return null;
- }
- /**
- * 將git倉庫內容回滾到指定版本的上一個版本
- * @param gitRoot 倉庫目錄
- * @param revision 指定的版本號
- * @return true,回滾成功,否則flase
- * @throws IOException
- */
- public static boolean rollBackPreRevision(String gitRoot, String revision) throws IOException {
- Git git = Git.open(new File(gitRoot));
- Repository repository = git.getRepository();
- RevWalk walk = new RevWalk(repository);
- ObjectId objId = repository.resolve(revision);
- RevCommit revCommit = walk.parseCommit(objId);
- String preVision = revCommit.getParent(0).getName();
- git.reset().setMode(ResetType.HARD).setRef(preVision).call();
- repository.close();
- return true;
- }
- /**
- * 查詢本次提交的日志
- * @param gitRoot git倉庫
- * @param revision 版本號
- * @return
- * @throws Exception
- */
- public static List<DiffEntry> getLog(String gitRoot, String revision) throws Exception {
- Git git = Git.open(new File(gitRoot));
- 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();
- }
- }