侧边栏壁纸
博主头像
Stars-One的杂货小窝博主等级

所有的创作都是有价值的

  • 累计撰写 257 篇文章
  • 累计创建 46 个标签
  • 累计收到 27 条评论

目 录CONTENT

文章目录

Jgit的使用笔记

Stars-one
2022-12-12 / 0 评论 / 0 点赞 / 504 阅读 / 11732 字

原文:Jgit的使用笔记 - Stars-One的杂货小窝

之前整的一个系统,涉及到git代码的推送,是通过cmd命令去推送的,然后最近在产品验收的时候,测试部门随意填了个git仓库,然后导致仓库代码被覆盖了,还好本地留有备份,没出现啥大问题

然后就计划于是就改为使用Jgit库来实现推送代码的功能,本能够验证远程仓库是否有代码,如果有代码,则后台不会去推送

Jgit是eclipse的写的一个集合git操作的java库

本文主要是记录一下如何使用Jgit创建一个本地Git仓库并将代码推送到远程仓库,未涉及的操作可以参考下列罗列的参考链接

使用

1.引入依赖

<dependency><groupId>org.eclipse.jgit</groupId><artifactId>org.eclipse.jgit</artifactId><version>4.4.1.201607150455-r</version></dependency>

由于我是JDK8,所以还是使用的较低的版本

PS:注意高版本由于依赖的JDK版本也是高版本的,低版本的JDK环境可能引用后使用报错,所以各位看情况升级版本

Jgit其中重要的3个类:Git,Repository和Command类

Git对象包含一个Repository对象

而Command主要就是有各种关于Git的操作,如push,pull等

2.获取Git对象

首先,jgit中提供了Git的一个对象,后续的所有的相关add commit push等操作,都是通过此对象来实现的。

有三种方法可以获取Git对象:

1.通过初始化创建

File dirFile = new File("D:\\temp\\mygit");
Git git = Git.init(dirFile)
    .setDirectory(dirFile)
    .call();

如果mygit文件夹已经包含有了.git文件夹,需要使用下面第二个方法,如果是使用此方法会抛出一个异常

所以建议可以判断一下是否有.git文件夹从而执行不同的方法来获取Git对象

这里需要注意的是,Git.init()方法返回的是一个InitCommand对象,之后通过call()方法来执行命令得到Git对象

InitCommand这个从字眼上就可以看出,是用来实现初始化的的一个命令对象

InitCommand实际上是继承于TransportCommand类,后续的像pushpull等操作也是有其对应的command对象供我们使用,这里先暂时简单提一下。

2.打开已有的本地Git仓库

如果当前的本地文件夹已存在有.git的文件夹,可以通过open()来打开

File dirFile = new File("D:\\temp\\mygit")
Git open = Git.open(dirFile);

3.通过clone创建


String gitUserName = "";
String gitUserPwd = "";
CredentialsProvider provider = new UsernamePasswordCredentialsProvider(gitUserName, gitUserPwd);  //生成身份信息
   
Git git = Git.cloneRepository()
    .setBranch("main")
    .setURI(templateProjectUrl)   //设置git远端URL
    .setDirectory(dirFile)  //设置本地仓库位置
    .setCredentialsProvider(provider)   //设置身份验证
    .call();   //启动命令

3.命令操作

添加命令

相当于git add .

AddCommand addCommand = git.add();

//将新文件纳入git管理,不含删除的文件
addCommand.addFilepattern(".").call();

提交命令

git.commit().setMessage("删除文件").call();

设置远程仓库地址

如果获取Git对象是通过初始化命令,则需要给Git对象设置远程仓库地址

RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
String pushUrl = "https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/gittest";
remoteSetUrlCommand.setUri(new URIish(pushUrl));
remoteSetUrlCommand.setName("origin");
remoteSetUrlCommand.call();

创建分支命令

推送需要传递一个本地分支,所以需要创建分支

需要判断下本地是否已有该分支了,有就不需要创建了


/**
 * 获取对应分支
 *
 * @param git
 * @param branchName
 * @return
 * @throws GitAPIException
 */
private Ref getMainRef(Git git, String branchName) throws GitAPIException {
    //判断一下本地是否存在有main分支
    List<Ref> refList = git.branchList().call();
    String brancheNameStr = "refs/heads/" + branchName;
    for (Ref ref : refList) {
        if (ref.getName().equals(brancheNameStr)) {
            return ref;
        }
    }
    return git.branchCreate().setName(branchName).call();
}

推送命令

推送需要本地分支的信息及对应的用户凭证信息

Ref mainRef = getMainRef(git, "main");
//push命令调用call方法后可以得到推送结果
Iterable<PushResult> origin = git.push().add(mainRef).setRemote("origin").setCredentialsProvider(provider).call();
for (PushResult pushResult : origin) {
    Collection<RemoteRefUpdate> remoteUpdates = pushResult.getRemoteUpdates();
    for (RemoteRefUpdate remoteUpdate : remoteUpdates) {
        RemoteRefUpdate.Status status = remoteUpdate.getStatus();
        System.out.println(remoteUpdate.getRemoteName() + ": " + status.name());
    }
}
//记得关闭
git.close();

示例

1.正常流程创建本地Git仓库并推送远程Git仓库

Git命令如下:

git clone https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/gittest2.git
cd gittest2
touch README.md
git add README.md
git commit -m "add README"
git push -u origin main

Jgit代码:

public void testPush() throws GitAPIException, URISyntaxException {
    //Git账号和密码
    String gitUserName = "usernmae";
    String gitUserPwd = "123456";

    CredentialsProvider provider = new UsernamePasswordCredentialsProvider(gitUserName, gitUserPwd);  //生成身份信息
    //指定Git仓库克隆的本地文件夹
    File dirFile = new File("D:\\temp\\gittest3");

    //git远程仓库地址
    String pushUrl = "https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/te1111.git";

    Git newGit = Git.cloneRepository()
            .setDirectory(dirFile)
            .setURI(pushUrl)
            .setCredentialsProvider(provider)
            .call();
    //模拟在文件夹中添加代码文件
    FileUtil.writeString("a text", FileUtil.file(dirFile, "my.txt"), Charsets.UTF_8);

    AddCommand addCommand = newGit.add();

    //将新文件纳入git管理,不含删除的文件
    addCommand.addFilepattern(".").call();

    //将已删除的文件纳入git管理,不含新文件
    //addCommand.addFilepattern(".").setUpdate(true).call();

    newGit.commit().setMessage("提交操作").call();

    Ref mainRef = getMainRef(newGit, "main");

    //输出结果
    Iterable<PushResult> origin = newGit.push().add(mainRef).setCredentialsProvider(provider).setRemote("origin").call();
    for (PushResult pushResult : origin) {
        Collection<RemoteRefUpdate> remoteUpdates = pushResult.getRemoteUpdates();
        for (RemoteRefUpdate remoteUpdate : remoteUpdates) {
            RemoteRefUpdate.Status status = remoteUpdate.getStatus();
            System.out.println(remoteUpdate.getRemoteName() + ": " + status.name());
        }
    }

    newGit.close();     //关闭
}

/**
 * 判断本地仓库是否存在有某分支,如果没有则创建
 */
private Ref getMainRef(Git git, String branchName) throws GitAPIException {
    
    List<Ref> refList = git.branchList().call();
    String brancheNameStr = "refs/heads/" + branchName;
    for (Ref ref : refList) {
        if (ref.getName().equals(brancheNameStr)) {
            return ref;
        }
    }
    return git.branchCreate().setName(branchName).call();
}

2.已有文件夹推送远程Git仓库

命令:

推送现有文件夹

cd existing_folder
git remote add origin https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/gittest.git
git add .
git commit -m "Initial commit"
git push -u origin main

推送现有的 Git 仓库

cd existing_repo
git remote add origin https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/gittest.git
git push -u origin main

Jgit代码:

public void testInitAndPush() throws GitAPIException, URISyntaxException, IOException {
    //Git账号和密码
    String gitUserName = "usernmae";
    String gitUserPwd = "123456";
    //git远程仓库地址
    String pushUrl = "https://git.linewellcloud.com/TJYR/TJ000003/SZYDYYYF/product/swan/test/gittest.git";
    CredentialsProvider provider = new UsernamePasswordCredentialsProvider(gitUserName, gitUserPwd);  //生成身份信息

    //本地已存在的文件夹
    File dirFile = new File("D:\\temp\\gittest3");

    //这里我是加了个判断条件
    boolean flag = false;
    File[] files = dirFile.listFiles();
    for (File file : files) {
        if (file.getName().equals(".git")) {
            flag = true;
            break;
        }
    }
    Git git;
    if (!flag) {
        //如果文件夹还没有创建git仓库,则调用创建git本地仓库(推送现有文件夹)
        git = Git.init().setDirectory(dirFile).call();

    }else{
        //如果已有git本地仓库,则直接打开(推送现有的 Git 仓库)
        git = Git.open(dirFile);
    }

    //设置远程仓库的地址
    RemoteSetUrlCommand remoteSetUrlCommand = git.remoteSetUrl();
    remoteSetUrlCommand.setUri(new URIish(pushUrl));
    remoteSetUrlCommand.setName("origin");
    remoteSetUrlCommand.call();

    git.add().addFilepattern(".").call();
    git.commit().setMessage("初次提交").call();

    //判断一下本地是否存在有main分支
    Ref mainRef = getMainRef(git, "main");

    //输出结果
    Iterable<PushResult> origin = git.push().add(mainRef).setCredentialsProvider(provider).setRemote("origin").call();
    for (PushResult pushResult : origin) {
        Collection<RemoteRefUpdate> remoteUpdates = pushResult.getRemoteUpdates();
        for (RemoteRefUpdate remoteUpdate : remoteUpdates) {
            RemoteRefUpdate.Status status = remoteUpdate.getStatus();
            System.out.println(remoteUpdate.getRemoteName() + ": " + status.name());
        }
    }
    
    git.close();
}

/**
 * 判断本地仓库是否存在有某分支,如果没有则创建
 */
private Ref getMainRef(Git git, String branchName) throws GitAPIException {
    
    List<Ref> refList = git.branchList().call();
    String brancheNameStr = "refs/heads/" + branchName;
    for (Ref ref : refList) {
        if (ref.getName().equals(brancheNameStr)) {
            return ref;
        }
    }
    return git.branchCreate().setName(branchName).call();
}

补充-判断远程仓库是否为空

public boolean isRemoteGitEmpty(String remoteUrl) throws GitAPIException {
    String gitUserName = "wanxingxing";
    String gitUserPwd = "13556710asd";

    CredentialsProvider provider = new UsernamePasswordCredentialsProvider(gitUserName, gitUserPwd);  //生成身份信息

    Collection<Ref> call = Git.lsRemoteRepository().setRemote(remoteUrl).setCredentialsProvider(provider).call();
    if (call.isEmpty()) {
        return true;
    } else {
        for (Ref ref : call) {
            System.out.println(ref.getName());
        }
        return false;
    }
}

参考

0

评论区