使用jgit 操作仓库:初始化,克隆远程仓库,添加缓存,本地代码提交,代码同步到远程仓库
使用场景,将生成的文件自动提交到gitlab.
添加jgit依赖
<!--jGit--> <dependency> <groupId>org.eclipse.jgit</groupId> <artifactId>org.eclipse.jgit</artifactId> <version>4.4.1.201607150455-r</version> </dependency>
定义仓库路径
public String remotePath = "http://user@10.1.2.1:8080/project.git";//远程库路径 public String localPath = "D:\\project\\";//下载已有仓库到本地路径
仓库初始化
/**
* 仓库初始化
*/
public static void gitInit() throws IOException{
Repository newlyCreatedRepo = FileRepositoryBuilder.create(new File(localPath+"/.git"));
if(newlyCreatedRepo!=null){
newlyCreatedRepo.create();
}
System.err.println("git init success");
}
克隆远程仓库
/**
* 克隆远程仓库
* @param remotePath:"url"
* @param branch:master
* @param userName:"userName"
* @param passWord:"password"
* @throws GitAPIException
*/
public static void gitClone(String remotePath,String branch,String userName,String passWord) throws GitAPIException {
//设置远程服务器上的用户名和密码
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
UsernamePasswordCredentialsProvider(userName,passWord);
//克隆代码库命令
CloneCommand cloneCommand = Git.cloneRepository();
Git git= cloneCommand.setURI(remotePath) //设置远程URI
.setBranch(branch) //设置clone下来的分支
.setDirectory(new File(localPath)) //设置下载存放路径
.setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
.call();
System.err.println("git clone success");
}
添加到缓存区
/**
* 添加到缓存区:对于删除的文件jgit添加失败
* @throws IOException
* @throws GitAPIException
*/
public static void gitAdd() throws IOException, GitAPIException {
Git git = new Git(new FileRepository(localPath + "/.git"));
git.add().addFilepattern(".").call();
System.err.println("git add success");
}
本地代码提交
/**
* 本地代码提交
* @param msg:提交信息
* @throws IOException
* @throws GitAPIException
*/
public static void gitCommit(String msg) throws IOException, GitAPIException {
Git git = new Git(new FileRepository(localPath + "/.git"));
//全部提交
git.commit().setAll(true).setMessage(msg).call();
System.err.println("git commit success");
}
本地代码同步到远程仓库
/**
* push本地代码到远程仓库
* @param branch:master
* @param userName:"userName"
* @param passWord:"password"
* @throws IOException
* @throws GitAPIException
*/
public static void gitPush(String branch,String userName,String passWord) throws IOException, GitAPIException {
UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
UsernamePasswordCredentialsProvider(userName,passWord);
//git仓库地址
Git git = new Git(new FileRepository(localPath+"/.git"));
//检查新建的分支是否已经存在,如果存在则将已存在的分支强制删除并新建一个分支
List<Ref> refs = git.branchList().call();
for (Ref ref : refs) {
if (ref.getName().equals(branch)) {
System.out.println("Removing branch before");
git.branchDelete().setBranchNames(branch).setForce(true)
.call();
break;
}
}
//新建分支
Ref ref = git.branchCreate().setName(branch).call();
//推送到远程
git.push().add(ref).setCredentialsProvider(usernamePasswordCredentialsProvider).call();
/*git.push().setRemote("origin/"+branch)
.setCredentialsProvider(usernamePasswordCredentialsProvider).call();*/
System.err.println("git push success");
}
删除服务器文件
/**
* 递归删除本地服务器文件
* @param files
*/
public static void filesDelete(File[] files){
if(files!=null&&files.length>0){
for (File file:files) {
//递归删除文件
File[] childFiles = file.listFiles();
filesDelete(childFiles);
file.delete();
System.err.println(file.getName()+" delete success");
}
}
}
主方法调用
public static void main(String[] args){
String remotePath = "url";
String branch = "branch";
String userName = "userName";
String passWord = "password";
String msg = "";
try {
filesDelete(new File(localPath).listFiles());
//gitInit();
gitClone(remotePath,branch,userName,passWord);
//添加要提交的新文件
/*gitAdd();
gitCommit(msg);
filesDelete(new File(localPath).listFiles());*/
gitPush("jgittest",userName,passWord);
} catch (GitAPIException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
参考文献:
https://www.cnblogs.com/songshu120/p/6180747.html
https://blog.csdn.net/gloomy_114/article/details/53129740

浙公网安备 33010602011771号