栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

git 查询3个月未使用的分支

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

git 查询3个月未使用的分支

文章目录

背景思路codeTODO

背景

领导任务,过年了,删除已经三个月没有使用的git分支,让项目轻装上阵。手动一个个看,太累了,还是代码来的香

思路
    查询所有的远程分支
 git branch -a
    遍历查询每个分支最近一次的提交
git  log -1 --date=short
    将分支名称-最后提交时间放入Map,然后过滤符合的分支名称
code
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IORuntimeException;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.RuntimeUtil;
import cn.hutool.core.util.StrUtil;

import java.io.IOException;
import java.io.InputStream;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class GitTool {
    public static void main(String[] args) {
        
        String projectDir = "D:\workProject\javabasedemo\JavaBeseStudy";
        String gitDir = " --git-dir="+projectDir+"\.git";
        String workTree = " --work-tree="+projectDir;
        String gitDit = gitDir + workTree;
        String cmd = "git" + gitDir + workTree + " branch -a";
        String str = RuntimeUtil.execForStr(cmd);
        String[] split = str.split("n");
        Map bran = new HashMap();
        for (String s : split) {
            if (s.contains("remotes")) {
                String cmd2 = "git" + gitDit + " log -1 --date=short " + s;
                List strings = RuntimeUtil.execForLines(cmd2);
                if(strings.size() > 2){
                    String s1 = strings.get(2);
                    String group0 = ReUtil.getGroup0("[0-9]{4}-[0-9]{2}-[0-9]{2}", s1);
                    bran.put(s,group0);
                }
            }
        }
        
        List branches = getLastMonth(bran);
        System.out.println(bran);
        System.out.println(branches);
    }

    private static List getLastMonth(Map bran) {
        List list = new ArrayList();
        bran.forEach((k,v)->{
            LocalDate localDate = LocalDate.parse(v).plusMonths(3);
            if (localDate.isBefore(LocalDate.now())) {
              list.add(k);
            }
        });
        return list;
    }
}

最后根据打印出来结果,然后进行delete 分支

TODO

后续用shell命令完成这个功能

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/724249.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号