zoukankan      html  css  js  c++  java
  • JGit的常用功能(提交、回滚,日志查询)

    最近项目中要做一个回滚功能,目的是如果这次发布出现了问题,立马回滚到上一次发布的版本,用jgit实现的,具体方法如下:

    Java代码  收藏代码
    1. public class GitUtil {  
    2.   
    3.     private final static String GIT = ".git";  
    4.   
    5.     /** 
    6.      * 将文件列表提交到git仓库中 
    7.      * @param gitRoot git仓库目录 
    8.      * @param files 需要提交的文件列表 
    9.      * @return 返回本次提交的版本号 
    10.      * @throws IOException  
    11.      */  
    12.     public static String commitToGitRepository(String gitRoot, List<String> files) throws Exception {  
    13.         if (StringUtils.isNotBlank(gitRoot) && files != null && files.size() > 0) {  
    14.   
    15.             File rootDir = new File(gitRoot);  
    16.   
    17.             //初始化git仓库  
    18.             if (new File(gitRoot + File.separator + GIT).exists() == false) {  
    19.                 Git.init().setDirectory(rootDir).call();  
    20.             }  
    21.   
    22.             //打开git仓库  
    23.             Git git = Git.open(rootDir);  
    24.             //判断是否有被修改过的文件  
    25.             List<DiffEntry> diffEntries = git.diff()  
    26.                 .setPathFilter(PathFilterGroup.createFromStrings(files))  
    27.                 .setShowNameAndStatusOnly(true).call();  
    28.             if (diffEntries == null || diffEntries.size() == 0) {  
    29.                 throw new Exception("提交的文件内容都没有被修改,不能提交");  
    30.             }  
    31.             //被修改过的文件  
    32.             List<String> updateFiles=new ArrayList<String>();  
    33.             ChangeType changeType;  
    34.             for(DiffEntry entry : diffEntries){  
    35.                 changeType = entry.getChangeType();  
    36.                 switch (changeType) {  
    37.                     case ADD:  
    38.                         updateFiles.add(entry.getNewPath());  
    39.                         break;  
    40.                     case COPY:  
    41.                         updateFiles.add(entry.getNewPath());  
    42.                         break;  
    43.                     case DELETE:  
    44.                         updateFiles.add(entry.getOldPath());  
    45.                         break;  
    46.                     case MODIFY:  
    47.                         updateFiles.add(entry.getOldPath());  
    48.                         break;  
    49.                     case RENAME:  
    50.                         updateFiles.add(entry.getNewPath());  
    51.                         break;  
    52.                     }  
    53.             }  
    54.             //将文件提交到git仓库中,并返回本次提交的版本号  
    55.             AddCommand addCmd = git.add();  
    56.             for (String file : updateFiles) {  
    57.                 addCmd.addFilepattern(file);  
    58.             }  
    59.             addCmd.call();  
    60.   
    61.             CommitCommand commitCmd = git.commit();  
    62.             for (String file : updateFiles) {  
    63.                 commitCmd.setOnly(file);  
    64.             }  
    65.             RevCommit revCommit = commitCmd.setCommitter(Constants.USERNAME, Constants.EMAIL)  
    66.                 .setMessage("publish").call();  
    67.             return revCommit.getName();  
    68.         }  
    69.         return null;  
    70.     }  
    71.   
    72.     /** 
    73.      * 将git仓库内容回滚到指定版本的上一个版本 
    74.      * @param gitRoot 仓库目录 
    75.      * @param revision 指定的版本号 
    76.      * @return true,回滚成功,否则flase 
    77.      * @throws IOException 
    78.      */  
    79.     public static boolean rollBackPreRevision(String gitRoot, String revision) throws IOException {  
    80.   
    81.         Git git = Git.open(new File(gitRoot));  
    82.   
    83.         Repository repository = git.getRepository();  
    84.   
    85.         RevWalk walk = new RevWalk(repository);  
    86.         ObjectId objId = repository.resolve(revision);  
    87.         RevCommit revCommit = walk.parseCommit(objId);  
    88.         String preVision = revCommit.getParent(0).getName();  
    89.         git.reset().setMode(ResetType.HARD).setRef(preVision).call();  
    90.         repository.close();  
    91.         return true;  
    92.     }  
    93.   
    94.     /** 
    95.      * 查询本次提交的日志 
    96.      * @param gitRoot git仓库 
    97.      * @param revision  版本号 
    98.      * @return  
    99.      * @throws Exception 
    100.      */  
    101.     public static List<DiffEntry> getLog(String gitRoot, String revision) throws Exception {  
    102.         Git git = Git.open(new File(gitRoot));  
    103.         Repository repository = git.getRepository();  
    104.   
    105.         ObjectId objId = repository.resolve(revision);  
    106.         Iterable<RevCommit> allCommitsLater = git.log().add(objId).call();  
    107.         Iterator<RevCommit> iter = allCommitsLater.iterator();  
    108.         RevCommit commit = iter.next();  
    109.         TreeWalk tw = new TreeWalk(repository);  
    110.         tw.addTree(commit.getTree());  
    111.   
    112.         commit = iter.next();  
    113.         if (commit != null)  
    114.             tw.addTree(commit.getTree());  
    115.         else  
    116.             return null;  
    117.   
    118.         tw.setRecursive(true);  
    119.         RenameDetector rd = new RenameDetector(repository);  
    120.         rd.addAll(DiffEntry.scan(tw));  
    121.   
    122.         return rd.compute();  
    123.     }  
    124. }  
  • 相关阅读:
    协议与接口相关
    jmeter 使用(1)
    jmeter 压力测试
    shell脚本的规则
    charles的原理及使用
    Linux环境部署和项目构建
    面向对象
    python 基础练习题
    jmeter 使用(2)
    Ext.apply
  • 原文地址:https://www.cnblogs.com/exmyth/p/13371764.html
Copyright © 2011-2022 走看看