读取所有项目的git地址和分支并写入文件的工具

离职要交接工作,负责的项目较多,项目工程有200多个,每个都是单独的git地址,分支也不尽相同。

领导要求将全部项目的git地址和分支整理出来,一个个点击复制,相当麻烦,因此写了这个小工具,可以全部扫描出来,并写入到指定的gitUrl.txt中。

package com.demo.gittool;

import java.io.*;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;

/**
 * @Date 2021/9/22 15:58
 */
public class Main {
    static String rootPath = "D:\\ideaProjects";
    static File gitUrlFile = new File(rootPath + File.separator + "gitUrls.txt");

    public static void main(String[] args) throws IOException {
        if (gitUrlFile.exists()) {
            gitUrlFile.delete();
        }
        gitUrlFile.createNewFile();
        visitFile(rootPath);
    }

    /**
     * 遍历目录
     *
     * @param rootPath
     * @throws IOException
     */
    public static void visitFile(String rootPath) throws IOException {
        Files.walkFileTree(Paths.get(rootPath), new SimpleFileVisitor<Path>() {
            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                String pathStr = file.toString();
                if (pathStr.endsWith(".git\\config")) {
                    readWriteFile(pathStr);
                }
                return FileVisitResult.CONTINUE;
            }
        });
    }

    /**
     * 读写文件
     *
     * @param s
     */
    private static void readWriteFile(String s) {
        System.out.println("读取文件=" + s);
        try (BufferedReader br = new BufferedReader(new FileReader(s));
             //从head文件读取分支
             BufferedReader brHead = new BufferedReader(new FileReader(new File(s).getParentFile() + File.separator + "HEAD"));
             FileWriter writer = new FileWriter(gitUrlFile, true);
        ) {
            String line = null;
            int count = 0;
            while ((line = br.readLine()) != null) {
                count++;
                if (count > 9) {
                    break;
                }
                //第9行为git地址
                if (count == 9) {
                    final String[] split = line.split("=");
                    if (split.length < 2) {
                        return;
                    }
                    final String gitUrl = split[1];
                    final File file = new File(s).getParentFile().getParentFile();
                    final String[] headSplit = brHead.readLine().split("/");
                    final String branch = headSplit[headSplit.length - 1];
                    String lineToWrite = file.getAbsolutePath() + "\t" + gitUrl + "\t" + branch;
                    System.out.println("写入地址=" + lineToWrite);
                    writer.write(lineToWrite + "\n");
                }
            }

        } catch (Exception e) {
            e.printStackTrace();
        }

    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值