使用JGit获取指定分支的文件内容
/** * 获取指定分支、指定文件的内容 * @param gitRoot git仓库目录 * @param branchName 分支名称 * @param fileName 文件名称 * @return * @throws Exception */ public static String getContentWithFile(String gitRoot, final String branchName, String fileName) throws Exception { final Git git = Git.open(new File(gitRoot)); Repository repository = git.getRepository(); repository = git.getRepository(); RevWalk walk = new RevWalk(repository); Ref ref = repository.getRef(branchName); if (ref == null) { //获取远程分支 ref = repository.getRef(REF_REMOTES + branchName); } //异步pull ExecutorService executor = Executors.newCachedThreadPool(); FutureTask<Boolean> task = new FutureTask<Boolean>(new Callable<Boolean>() { @Override public Boolean call() throws Exception { /*//创建分支 CreateBranchCommand createBranchCmd = git.branchCreate(); createBranchCmd.setStartPoint(REF_REMOTES + branchName).setName(branchName).call();*/ return git.pull().call().isSuccessful(); } }); executor.execute(task); ObjectId objId = ref.getObjectId(); RevCommit revCommit = walk.parseCommit(objId); RevTree revTree = revCommit.getTree(); TreeWalk treeWalk = TreeWalk.forPath(repository, fileName, revTree); //文件名错误 if (treeWalk == null) return null; ObjectId blobId = treeWalk.getObjectId(0); ObjectLoader loader = repository.open(blobId); byte[] bytes = loader.getBytes(); if (bytes != null) return new String(bytes); return null; }
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25