领导任务,过年了,删除已经三个月没有使用的git分支,让项目轻装上阵。手动一个个看,太累了,还是代码来的香
git branch -a
git log -1 --date=short
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<String,String> bran = new HashMap<String,String>(); for (String s : split) { if (s.contains("remotes")) { String cmd2 = "git" + gitDit + " log -1 --date=short " + s; List<String> 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<String> branches = getLastMonth(bran); System.out.println(bran); System.out.println(branches); } private static List<String> getLastMonth(Map<String, String> bran) { List<String> 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 分支
后续用shell命令完成这个功能