DevOps系列文章 之 Java使用jgit管理git仓库

news/2024/11/24 1:38:57/

最近设计基于gitops新的CICD方案,需要通过java读写git仓库,这里简单记录下。

在jgit中,存在最核心的三个组件:Git类,Repository类。Git类中包含了push commit之类的常见git操作,而Repository则实现了仓库的初始化和基本的管理功能。
Git类的实例都会持有一个Repository实例。

Repository类的初始化

针对一个git仓库,我们一般会有三种方式获得
1.新建一个空仓库

 Git git = Git.init().setDirectory(localPath).call()

2.加载一个已存在的仓库

Repository repository = builder.setGitDir(repoDir).readEnvironment() // scan environment GIT_* variables.findGitDir() // scan up the file system tree.build()

3.从远程仓库下载

Git result = Git.cloneRepository().setURI(REMOTE_URL).setDirectory(localPath).call()

JGit是一款pure java的软件包,可以读写git仓库,下面介绍基本使用。

引入jgit#

maven引入:

        <!-- jgit --><dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>5.8.0.202006091008-r</version></dependency><dependency><groupId>commons-io</groupId><artifactId>commons-io</artifactId><version>2.6</version></dependency>

jgit 有一个Git类,可以用来执行常规的git操作

凭证管理#

通过CredentialsProvider管理凭证,常用的是UsernamePasswordCredentialsProvider

通过下面代码初始化:

public static CredentialsProvider createCredential(String userName, String password) {return new UsernamePasswordCredentialsProvider(userName, password);
}

clone远程仓库#

git 命令:

git clone {repoUrl}

通过Git.cloneRepository 来clone远程仓库,如果需要凭证,则需要指定credentialsProvider

 public static Git fromCloneRepository(String repoUrl, String cloneDir, CredentialsProvider provider) throws GitAPIException {Git git = Git.cloneRepository().setCredentialsProvider(provider).setURI(repoUrl).setDirectory(new File(cloneDir)).call();return git;
}

commit#

git 命令:

git commit -a -m '{msg}'

commit比较简单,对应commit方法, 注意需要先add

public static void commit(Git git, String message, CredentialsProvider provider) throws GitAPIException {git.add().addFilepattern(".").call();git.commit().setMessage(message).call();
}

push#

git 命令:

git push origin branchName

push直接调用push即可, 需要指定credentialsProvider

   public static void push(Git git, CredentialsProvider provider) throws GitAPIException, IOException {push(git,null,provider);}public static void push(Git git, String branch, CredentialsProvider provider) throws GitAPIException, IOException {if (branch == null) {branch = git.getRepository().getBranch();}git.push().setCredentialsProvider(provider).setRemote("origin").setRefSpecs(new RefSpec(branch)).call();}

读取已有仓库#

如果git已经clone了,想直接读取,怎么办?

 public static Repository getRepositoryFromDir(String dir) throws IOException {return new FileRepositoryBuilder().setGitDir(Paths.get(dir, ".git").toFile()).build();}

读取仓库日志#

可以通过RevWalk读取仓库日志。

  • revWalk.parseCommit 可读取一条commit
  • 遍历revWalk,可读取所有日志
 public static List<String> getLogs(Repository repository) throws IOException {return getLogsSinceCommit(repository, null, null);}public static List<String> getLogsSinceCommit(Repository repository, String commit) throws IOException {return getLogsSinceCommit(repository, null, commit);}public static List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException {if (branch == null) {branch = repository.getBranch();}Ref head = repository.findRef("refs/heads/" + branch);List<String> commits = new ArrayList<>();if (head != null) {try (RevWalk revWalk = new RevWalk(repository)) {revWalk.markStart(revWalk.parseCommit(head.getObjectId()));for (RevCommit revCommit : revWalk) {if (revCommit.getId().getName().equals(commit)) {break;}commits.add(revCommit.getFullMessage());System.out.println("\nCommit-Message: " + revCommit.getFullMessage());}revWalk.dispose();}}return commits;}

测试#

我们来先clone仓库,然后修改,最后push

package com.devops.autocicdstore.git;import org.apache.commons.io.FileUtils;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.transport.CredentialsProvider;import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;import static com.devops.autocicdstore.git.GetRepositoryMgr.*;/*** @Author 虎哥* @Description //TODO* |要带着问题去学习,多猜想多验证|**/
public class TestMain {public static void main(String[] args) throws GitAPIException, IOException {String yaml = "dependencies:\n" +"- name: springboot-rest-demo\n" +"  version: 0.0.5\n" +"  repository: http://hub.hubHOST.com/chartrepo/ainote\n" +"  alias: demo\n" +"- name: exposecontroller\n" +"  version: 2.3.82\n" +"  repository: http://chartmuseum.jenkins-x.io\n" +"  alias: cleanup\n";CredentialsProvider provider = createCredential("USR_NAME", "PASSWORD");String cloneDir = "/tmp/test";Git git = fromCloneRepository("https://gitee.com/JavaBigDataStudy/auto-cicd-store.git", cloneDir, provider);// 修改文件FileUtils.writeStringToFile(Paths.get(cloneDir, "env", "requirements.yaml").toFile(), yaml, "utf-8");// 提交commit(git, "deploy(app): deploy  springboot-rest-demo:0.0.5 to env test", provider);// push 到远程仓库push(git, "master", provider);git.clean().call();git.close();FileUtils.deleteDirectory(new File(cloneDir));}
}

package com.devops.autocicdstore.git;import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.lib.Ref;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.revwalk.RevCommit;
import org.eclipse.jgit.revwalk.RevWalk;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;import java.io.File;
import java.io.IOException;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;/*** @Author 虎哥* @Description //TODO* |要带着问题去学习,多猜想多验证|**/
public class GetRepositoryMgr {public static CredentialsProvider createCredential(String userName, String password) {return new UsernamePasswordCredentialsProvider(userName, password);}public static void main(String[] args) throws GitAPIException, IOException {
//        String localPath = "D:\\SoftwareDev\\Code\\auto-cicd-store\\git-res";
//        String remotePath = "https://gitee.com/JavaBigDataStudy/auto-cicd-store.git";
//        CredentialsProvider provider = createCredential("XXXX","XXX");
//        //1
//
//        Git git = fromCloneRepository(remotePath,localPath,provider);
//        //2
//        commit(git,"test",provider);
//        //3
//        push(git,"dev",provider);
//        //4
//      //  getLogs()lRepository repository = getRepositoryFromDir("D:\\\\SoftwareDev\\\\Code\\\\auto-cicd-store\\\\git-res");List<String> logs = getLogs(repository);System.out.println(logs);}public static Git fromCloneRepository(String repoUrl, String cloneDir, CredentialsProvider provider) throws GitAPIException {Git git = Git.cloneRepository().setCredentialsProvider(provider).setURI(repoUrl).setDirectory(new File(cloneDir)).call();return git;}public static Repository getRepositoryFromDir(String dir) throws IOException {return new FileRepositoryBuilder().setGitDir(Paths.get(dir, ".git").toFile()).build();}public static void commit(Git git, String message, CredentialsProvider provider) throws GitAPIException {git.add().addFilepattern(".").call();git.commit().setMessage(message).call();}public static void push(Git git, CredentialsProvider provider) throws GitAPIException, IOException {push(git,null,provider);}public static void push(Git git, String branch, CredentialsProvider provider) throws GitAPIException, IOException {if (branch == null) {branch = git.getRepository().getBranch();}git.push().setCredentialsProvider(provider).setRemote("origin").setRefSpecs(new RefSpec(branch)).call();}public static List<String> getLogs(Repository repository) throws IOException {return getLogsSinceCommit(repository, null, null);}public static List<String> getLogsSinceCommit(Repository repository, String commit) throws IOException {return getLogsSinceCommit(repository, null, commit);}public static List<String> getLogsSinceCommit(Repository repository, String branch, String commit) throws IOException {if (branch == null) {branch = repository.getBranch();}Ref head = repository.findRef("refs/heads/" + branch);List<String> commits = new ArrayList<>();if (head != null) {try (RevWalk revWalk = new RevWalk(repository)) {revWalk.markStart(revWalk.parseCommit(head.getObjectId()));for (RevCommit revCommit : revWalk) {if (revCommit.getId().getName().equals(commit)) {break;}commits.add(revCommit.getFullMessage());System.out.println("\nCommit-Message: " + revCommit.getFullMessage());}revWalk.dispose();}}return commits;}}

读取已有仓库的日志:

 Repository repository = getRepositoryFromDir("GIT_DIR");List<String> logs = getLogs(repository);System.out.println(logs);RevCommit head = getLastCommit(repository);System.out.println(head.getFullMessage());


http://www.ppmy.cn/news/789774.html

相关文章

数据结构--单链表

目录 1.单链表的定义&#xff1a; 单链表基本操作的实现&#xff1a; 2.单链表的初始化&#xff08;构造带头结点的空表&#xff09; 2.将头结点的指针域置空 3.链表是否为空&#xff1a; 4.单链表的销毁&#xff1a; 5.单链表的清空: 6.求单链表的表长&#xff1a; 7…

最长公共子序列LCA

最长连续公共子序列 题目链接:3692. 最长连续公共子序列 - AcWing题库 /*解法:定义dp[i1][j1]为以a[i]结尾的字符串和b[j]结尾的字符串的最长连续公共子序列那么对于a[i] b[j]的时候 dp[i1][j1] dp[i][j]1否则为0 其他就是细枝末节 */ #include<iostream> #include<…

关于ios低版本在app store下载软件时由于版本低导致不能下载的解决办法

最近有人提出问题&#xff0c;说IOS7怎么在APP store中下载软件&#xff0c;好多软件都提示需要ios8及以上才可以下载&#xff0c;而App Store里下载又不会提供给你旧版本&#xff0c;难倒ios7就必须升级才能下载吗&#xff1f;对此本人在网上查了好多资料也做了好多测试&#…

苹果微信更新不了最新版本_微信最新版建议更新!还有一个功能彩蛋!

潮阳圈同城&#xff0c;招聘求职/房屋租售 &#x1f448;戳这里&#xff01; “有毒&#xff01;” “微信更新又又又又又来了&#xff01;” 这更新频率也太快了吧... 前段时间 安卓用户更新到最新版本微信 已经可以修改微信号 而苹果用户当时还不可以 粉丝一直追问苹果什么时…

苹果android怎么升级,微信系统大升级,苹果安卓手机如何升级更新为最新版微信8.0?...

原标题&#xff1a;微信系统大升级&#xff0c;苹果安卓手机如何升级更新为最新版微信8.0&#xff1f; 昨天&#xff0c;微信发布了最新版微信8.0。与以往不同的是&#xff0c;作为一款已风靡十年的社交产品&#xff0c;微信此次版本更新带来许多全新玩法和小创意。 目前苹果手…

苹果android怎么升级,微信系统升级!苹果安卓手机如何升级更新为最新版微信8.0?...

昨天&#xff0c;微信发布了最新版微信8.0。与以往不同的是&#xff0c;作为一款已风靡十年的社交产品&#xff0c;微信此次版本更新带来许多全新玩法和小创意。 目前苹果手机用户可以点击“关于微信”进行自动升级&#xff0c;安卓系统暂时还未推送最新安装包。 安卓手机 如何…

苹果微信更新不了最新版本_微信最新版来了,安卓苹果都更新,3大功能非常亮眼...

大家好我是人见人爱花见花开的奶思君。微信又双叒叕更新了&#xff0c;相信各位宝宝都非常的关心&#xff0c;这次ios和安卓的都更新了。到底更新了啥呢&#xff1f;速来围观&#xff01; iOS 7.0.18 正式版 首先来说说iOS 7.0.18 正式版&#xff0c;更新内容里面显示&#xff…

iOS微信8.0.31更新,带来7个功能

近日&#xff0c;iOS微信更新了8.0.31正式版。经过几天的深入体验&#xff0c;我发现了7个新特性。如果你还没有升级&#xff0c;我们就往下看吧。 功能一&#xff1a;优化编辑按钮 微信发图片&#xff0c;发送前可以编辑&#xff0c;底部的编辑按钮变了。新版画笔工具变成了钢…