java实现git操作_基于Jgit 实现java自动化操作git 简单功能

![](/api/file/getImage?fileId=5d84711416199b045000028d)

## 需求

通过java代码实现git的常用的操作

如`clone`、`add`、`commit`、`pull`、`push` 等

最终可以用于例如服务器生产环境中的部分静态文件一件同步

## 代码

```java

import java.io.File;

import java.io.IOException;

import org.apache.commons.lang.ArrayUtils;

import org.apache.commons.lang.StringUtils;

import org.eclipse.jgit.api.AddCommand;

import org.eclipse.jgit.api.CommitCommand;

import org.eclipse.jgit.api.Git;

import org.eclipse.jgit.api.PullResult;

import org.eclipse.jgit.api.PushCommand;

import org.eclipse.jgit.api.errors.GitAPIException;

import org.eclipse.jgit.transport.CredentialsProvider;

import org.eclipse.jgit.transport.PushResult;

import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

/**

* java 操作 git

* 实现java自动化操作git 简单功能实现

*

* 基于Jgit

*

* org.eclipse.jgit

* org.eclipse.jgit

* ${jgit.version}

*

*

* 代码参考

* https://www.jqhtml.com/26305.html

*/

public class GitUtils {

public static UsernamePasswordCredentialsProvider getCredentialsProvider(String gituser, String gitpasswd) {

UsernamePasswordCredentialsProvider credentialsProvider = null;

if (StringUtils.isNotEmpty(gituser) && StringUtils.isNotEmpty(gitpasswd)) {

credentialsProvider = new UsernamePasswordCredentialsProvider(gituser, gitpasswd);

}

return credentialsProvider;

}

public static void doInit(File gitDir) throws GitAPIException {

if (!gitDir.exists()) {

gitDir.mkdirs();

}

Git git = Git.init().setDirectory(gitDir).setBare(false).call();

}

public static void doClone(File gitDir, String gituri, CredentialsProvider credentialsProvider, String branch)

throws GitAPIException {

Git.cloneRepository().setURI(gituri)

.setCredentialsProvider(credentialsProvider)

.setDirectory(gitDir).setCloneAllBranches(false)

.setBranch(branch).call().close();

}

public static PullResult doPull(File gitDir, String url, CredentialsProvider credentialsProvider) throws IOException,

GitAPIException {

Git git = Git.open(gitDir);

return git.pull().setCredentialsProvider(credentialsProvider).call();

}

public static void doAdd(File gitDir, String... files)

throws GitAPIException, IOException {

try (Git git = Git.open(gitDir)) {

AddCommand add = git.add();

if (ArrayUtils.isEmpty(files)) {

add.addFilepattern(".");

} else {

for (String file : files) {

add.addFilepattern(file);

}

}

add.call();

}

}

public static void doCommit(File gitDir, String message) throws GitAPIException, IOException {

Git git = Git.open(gitDir);

CommitCommand commit = git.commit().setAll(false).setMessage(message);

commit.call();

}

public static Iterable doPush(File gitDir, CredentialsProvider credentialsProvider) throws IOException, GitAPIException {

Git git = Git.open(gitDir);

PushCommand push = git.push().setCredentialsProvider(credentialsProvider);

return push.call();

}

}

```

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值