引
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结果查看