Jgit实现克隆,切换分支,提交操作

1。maven项目中pom.xml中引入jgit包

  <!-- 引入最新jar包 https://mvnrepository.com/artifact/org.eclipse.jgit/org.eclipse.jgit -->
            <dependency>
                <groupId>org.eclipse.jgit</groupId>
                <artifactId>org.eclipse.jgit</artifactId>
                <version>4.9.0.201710071750-r</version>
            </dependency>

2.代码实现。写GitUtil类实现代码操作,代码如下

package com.XXXX;

import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.api.Status;

import java.io.File;

/**
 * 类描述:git工具类
 * @author lj 2017年10月27日 15:34
 */
public class GitUtil {
    //克隆仓库
    public String cloneRepository(String url, String localPath) {
        try {
            System.out.println("开始下载......");

            Git git = Git.cloneRepository()
                    .setURI(url)
                    .setDirectory(new File(localPath))
                    .setCloneAllBranches(true)
                    .call();

            System.out.println("下载完成......");

            return "success";
        } catch (Exception e) {
            e.printStackTrace();
            return "error";
        }
    }
    //切换分支
    public void checkoutBranch(String localPath, String branchName){
        String projectURL = localPath + "\\.git";

        Git git = null;
        try {
            git = Git.open(new File(projectURL));
            git.checkout().setCreateBranch(true).setName(branchName).call();
            git.pull().call();
            System.out.println("切换分支成功");
        }catch (Exception e){
            e.printStackTrace();
            System.out.println("切换分支失败");
        } finally{
            if (git != null) {
                git.close();
            }
        }   
    }

    //提交代码
    public void commit(String localPath,String pushMessage)  {
        String projectURL = localPath + "\\.git";
        Git git = null;
        try {
            git = Git.open(new File(projectURL));
            git.pull().call();
            Status status = git.status().call();
            if (status.hasUncommittedChanges() == false) {
                System.out.println("无已修改文件");
                return;
            }
            //忽略GitUtil.java文件
//            git.add().addFilepattern("GitUtil.java").call();
            git.add().addFilepattern(".").call();
            git.commit().setMessage(pushMessage).call();
            git.pull().call();
            git.push().call();

//            //查看log信息
//            for(RevCommit revCommit : git.log().call()){
//                System.out.println(revCommit);
//                System.out.println(revCommit.getFullMessage());
//                System.out.println(revCommit.getCommitterIdent().getName() + " " + revCommit.getCommitterIdent().getEmailAddress());
//            }
            System.out.println("提交成功");
        } catch (Exception e){
            e.printStackTrace();
            System.out.println("提交失败");
        } finally{
            if (git != null) {
                git.close();
            }
        }
    }
    public static void main(String[] args) {
        GitUtil gitUtil = new GitUtil();
        //git远程url地址
        String url = "XXXX.git";
//        String localPath = "d:/jgitTest";
        String localPath = System.getProperty("user.dir");
        String branchName = "20171010_branch";
        try {
//            gitUtil.cloneRepository(url,localPath);
//            gitUtil.checkoutBranch(localPath,branchName);
            gitUtil.commit(localPath,"测试提交1");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值