前言
-
参考 https://www.jianshu.com/p/b3d4e5cee835
-
适用于git私服拉取个人所涉及权限的代码,方便有多个项目权限的人快速拉取自己所有权限的代码。
-
默认生成目录结构与gitlab一致
步骤一:获取权限你的代码权限文件d
从gitlab私服生成所有你有权限的代码信息
请求地址:
http://gitlab.dstcar.com//api/v4/projects?simple=true&per_page=100&page=1
postman方式获取
cookies自己登录gitlab生成
gitlab限制每次只能拉100条数据,超过100条自己分页处理
获取cookies
添加cookies
请求成功后保存json文件到指定地方(步骤二会用到) eg:放到D:\\gitlab_projects.json
步骤二:执行java代码
以下根据个人所需修改对应代码,一般只需要修改下载fileName和srcDir参数
-
备注:有现成springboot项目你直接生成一个单元测试文件执行执行main方法,没有的话自己新建一个项目执行
-
import java.io.File; import java.io.FileInputStream; import java.io.FileReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.Reader; import java.nio.charset.Charset; import org.springframework.util.StreamUtils; import com.alibaba.fastjson.JSON; import com.alibaba.fastjson.JSONArray; import com.alibaba.fastjson.JSONObject; /** * */ public class TestGitlabData { public static void main(String[] args) { //gittlab获取的权限文件 String fileName = "D:\\gitlab_projects.json"; String jsonStr = readJsonFile(fileName); JSONArray projects = JSON.parseArray(jsonStr); //指定需要下载的分支 String branchName = "master"; //尝试切换的分支 String tryCheckOutBranchName = "develop"; //指定下载后输出的地址 String srcDir = "D:\\code"; File execDir = new File(srcDir); if (!execDir.exists()) { boolean result = execDir.mkdirs(); System.out.println("execDir.mkdirs = " + result); } int projectNum = 0; for (int i = 0; i < projects.size(); i++) { JSONObject prj = projects.getJSONObject(i); //项目地址 // String httpUrlToRepo = prj.getString("http_url_to_repo"); String httpUrlToRepo = prj.getString("ssh_url_to_repo"); //项目路径 // String pathWithNamespace = prj.getString("path_with_namespace"); String pathWithNamespace = prj.getString("name_with_namespace").replaceAll(" ", ""); System.out.println("拉取projectNum = " + ++projectNum); gitClone(branchName, httpUrlToRepo, pathWithNamespace, execDir); File tryDir = new File(srcDir + "\\" + pathWithNamespace); if (tryDir.exists()) { gitTryCheckOutBranch(tryCheckOutBranchName, tryDir); } } } private static void gitTryCheckOutBranch(String tryCheckOutBranchName, File execDir) { String command = String.format("git checkout %s ", tryCheckOutBranchName); try { System.out.println("===========start=================="); System.out.println("start exec command : " + command); //执行命令行 Process exec = Runtime.getRuntime().exec(command, null, execDir); exec.waitFor(); String successResult = StreamUtils.copyToString(exec.getInputStream(), Charset.forName("UTF-8")); String errorResult = StreamUtils.copyToString(exec.getErrorStream(), Charset.forName("UTF-8")); System.out.println("successResult: " + successResult); System.out.println("errorResult: " + errorResult); System.out.println("============end================"); } catch (Exception e) { e.printStackTrace(); } } static void gitClone(String branchName, String httpUrlToRepo, String pathWithNamespace, File execDir) { String command = String.format("git clone -b %s %s %s ", branchName, httpUrlToRepo, pathWithNamespace); try { System.out.println("===========start=================="); System.out.println("start exec command : " + command); //执行命令行 Process exec = Runtime.getRuntime().exec(command, null, execDir); exec.waitFor(); String successResult = StreamUtils.copyToString(exec.getInputStream(), Charset.forName("UTF-8")); String errorResult = StreamUtils.copyToString(exec.getErrorStream(), Charset.forName("UTF-8")); System.out.println("successResult: " + successResult); System.out.println("errorResult: " + errorResult); System.out.println("============end================"); } catch (Exception e) { e.printStackTrace(); } } public static String readJsonFile(String fileName) { String jsonStr = ""; try { File jsonFile = new File(fileName); FileReader fileReader = new FileReader(jsonFile); Reader reader = new InputStreamReader(new FileInputStream(jsonFile), "utf-8"); int ch = 0; StringBuffer sb = new StringBuffer(); while ((ch = reader.read()) != -1) { sb.append((char) ch); } fileReader.close(); reader.close(); jsonStr = sb.toString(); return jsonStr; } catch (IOException e) { e.printStackTrace(); return null; } } }
实际效果为下来所有你有权限的代码并且尝试切换develop环境(你想尝试换其他环境也可以自行修改代码参数)
目录结构跟gitlab一致