当前位置: 首页 > news >正文

【Git】Java 使用 JGit 创建 Git 代码仓库

JGit 是一个用 Java 实现的 Git 版本控制工具。它是由 EGit 项目发展而来的,EGit 是 Eclipse 基金会的一个开源项目,旨在为 Eclipse IDE 提供 Git 支持。

JGit 提供了一个完整的 Git 实现,包括存储库、索引、对象数据库、命令行客户端和图形界面客户端等。它可以与其他 Git 客户端(如 Git Bash、SourceTree 等)配合使用,也可以作为独立的版本控制系统使用。

JGit 的优点之一是它的速度和性能。由于它是用 Java 实现的,因此可以在各种平台上运行,并且可以利用 Java 的内存管理和垃圾回收机制来提高性能。

本文将介绍使用 JGit 实现推送创建。

环境

  • Git 服务器采用Gitea
  • Gitea 已开启推送创建功能 【Gitea】配置 Push To Create

代码实现

JGit pom 引用

        <!-- https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
<dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>5.1.3.201810200350-r</version>
</dependency>

实现代码


import java.io.File;
import java.io.IOException;
import java.net.URISyntaxException;import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.errors.GitAPIException;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.transport.CredentialsProvider;
import org.eclipse.jgit.transport.URIish;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;import com.google.common.io.Files;/** Description: * Author: xzbd* Finished: 2024年2月18日*/
public class JgitTest {public static void main(String[] args) {String localDir = "D:\\tmp\\git-test\\";String gitUrl = "http://192.168.181.1:3000/root/git-test-by-code.git";String username = "root";String password = "123456";try {Repository repository = new FileRepository(localDir.concat(".git"));Git git = new Git(repository);// 添加一个 Add-New-File.txt 文件,并向其中写入 Write By Code 字符串File addNewFile = new File((localDir.concat("Add-New-File.txt")));Files.write("Write By Code".getBytes(), addNewFile);// 新增的文件添加到存储库,类似于 git add .git.add().addFilepattern(".").call();// 添加提交信息,并提交git.commit().setMessage("code by xzbd").call();// 添加远程仓库信息git.remoteAdd().setName("origin").setUri(new URIish(gitUrl)).call();// 创建认证信息CredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider(username,password);// 推送到远端 master 分支git.push().setRemote("origin").setCredentialsProvider(credentialsProvider).add("master").call();// 关闭 git 命令git.close();} catch (IOException | GitAPIException | URISyntaxException e) {e.printStackTrace();}}
}

执行验证

运行日志

PS D:\test> & 'D:\apps\Java\jdk-11.0.11\bin\java.exe' '@D:\Users\74511\AppData\Local\Temp\cp_93r4hjv1grgk6g6477y7l8wub.argfile' 'cn.xzbd.util.JgitTest' 
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/D:/Users/74511/.m2/repository/ch/qos/logback/logback-classic/1.2.11/logback-classic-1.2.11.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/D:/workspace/builder_backend/libs/sya.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [ch.qos.logback.classic.util.ContextSelectorStaticBinder]
17:16:57.070 [main] DEBUG org.eclipse.jgit.util.FS - readpipe [git, --version],d:\apps\Git\cmd
17:16:57.154 [main] DEBUG org.eclipse.jgit.util.FS - readpipe may return 'git version 2.36.1.windows.1'
17:16:57.154 [main] DEBUG org.eclipse.jgit.util.FS - remaining output:17:16:57.170 [main] DEBUG org.eclipse.jgit.util.FS - readpipe [git, config, --system, --edit],d:\apps\Git\cmd
17:16:57.346 [main] DEBUG org.eclipse.jgit.util.FS - readpipe may return 'D:/apps/Git/etc/gitconfig'
17:16:57.346 [main] DEBUG org.eclipse.jgit.util.FS - remaining output:17:16:58.160 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< # service=git-receive-pack
17:16:58.161 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< 0000
17:16:58.168 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< 0000000000000000000000000000000000000000 capabilities^{}report-status report-status-v2 delete-refs side-band-64k quiet atomic ofs-delta push-options object-format=sha1 agent=git/2.36.2
17:16:58.174 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< 0000
17:16:58.179 [main] DEBUG org.eclipse.jgit.transport.PacketLineOut - git> 0000000000000000000000000000000000000000 a665c306a1cedd957ebb6bd164904f85ab855071 refs/heads/masterreport-status delete-refs ofs-delta side-band-64k agent=JGit/5.1.3.201810200350-r
17:16:58.181 [main] DEBUG org.eclipse.jgit.transport.PacketLineOut - git> 0000
17:17:00.325 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< unpack ok
17:17:00.325 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< ok refs/heads/master
17:17:00.326 [main] DEBUG org.eclipse.jgit.transport.PacketLineIn - git< 0000
PS D:\test>

web结果查看

在这里插入图片描述

相关文章:

  • 【电路笔记】-LR串联电路
  • 天锐绿盾 | 办公终端文件数据\资料防泄密软件
  • 使用消息中间件实现系统间的异步通信和解耦
  • HttpClient:HTTP GET请求的服务器响应输出
  • 「算法」滑动窗口
  • 入门者拿捏 Java 的必备小秘诀
  • Linux:docker在线仓库(docker hub 阿里云)基础操作
  • VMwareWorkstation17.0虚拟机安装搭建Windows 11虚拟机(完整图文详细步骤教程)
  • Python学习路线图
  • ⭐北邮复试刷题103. 二叉树的锯齿形层序遍历 (力扣每日一题)
  • 模拟发送 Ctrl+Alt+Del 快捷键
  • 【简洁的代码永远不会掩盖设计者的意图】如何写出规范整洁的代码
  • 什么是tomcat?tomcat是干什么用的?
  • [SWPUCTF 2021 新生赛]crypto8
  • MySQL性能调优篇(3)-缓存的优化与清理
  • [分享]iOS开发 - 实现UITableView Plain SectionView和table不停留一起滑动
  • C学习-枚举(九)
  • ES6系列(二)变量的解构赋值
  • Java教程_软件开发基础
  • Just for fun——迅速写完快速排序
  • Mysql数据库的条件查询语句
  • node.js
  • PaddlePaddle-GitHub的正确打开姿势
  • Spring-boot 启动时碰到的错误
  • Wamp集成环境 添加PHP的新版本
  • 初识MongoDB分片
  • 从零开始的无人驾驶 1
  • 第三十一到第三十三天:我是精明的小卖家(一)
  • 为什么要用IPython/Jupyter?
  • Java性能优化之JVM GC(垃圾回收机制)
  • puppet连载22:define用法
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • # Swust 12th acm 邀请赛# [ A ] A+B problem [题解]
  • ${ }的特别功能
  • (1)(1.8) MSP(MultiWii 串行协议)(4.1 版)
  • (12)目标检测_SSD基于pytorch搭建代码
  • (Matalb回归预测)PSO-BP粒子群算法优化BP神经网络的多维回归预测
  • (办公)springboot配置aop处理请求.
  • (顶刊)一个基于分类代理模型的超多目标优化算法
  • (二)PySpark3:SparkSQL编程
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • (转)es进行聚合操作时提示Fielddata is disabled on text fields by default
  • . NET自动找可写目录
  • .NET 4.0中使用内存映射文件实现进程通讯
  • .NET Core实战项目之CMS 第十二章 开发篇-Dapper封装CURD及仓储代码生成器实现
  • .net php 通信,flash与asp/php/asp.net通信的方法
  • /*在DataTable中更新、删除数据*/
  • @Autowired 与@Resource的区别
  • @Bean, @Component, @Configuration简析
  • @CacheInvalidate(name = “xxx“, key = “#results.![a+b]“,multi = true)是什么意思
  • @Mapper作用
  • @vue/cli 3.x+引入jQuery
  • [AS3]URLLoader+URLRequest+JPGEncoder实现BitmapData图片数据保存
  • [CSS]CSS 的背景
  • [CSS3备忘] transform animation 等