JGit克隆代码,提交代码,推送新分支

 /**
     * 克隆代码
     * @param localPath 拉取到本地的地址
     * @param remotePath 远程仓库地址
     * @param branch 拉去的分支
     * @throws IOException
     * @throws GitAPIException
     */
    public static void cloneCode(String localPath,String remotePath,String branch) throws IOException, GitAPIException {
        //获取路径中的本地文件夹
        String localPathName = localPath +  JGitCommon.getFileName(remotePath);
        File file = new File(localPathName);
        if(!file.exists()){
            file.mkdir();
        }
        //设置远程服务器上的用户名和密码
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider(_configReader.getVal("UserName"),_configReader.getVal("PassWord"));

        //克隆代码库命令
        CloneCommand cloneCommand = Git.cloneRepository();

        Git git= cloneCommand.setURI(remotePath) //设置远程URI
                .setBranch(branch) //设置clone下来的分支
                .setDirectory(new File(localPathName)) //设置下载存放路径
                .setCredentialsProvider(usernamePasswordCredentialsProvider) //设置权限验证
                .call();
        System.out.println("代码拉去完毕");

        //System.out.print(git.tag());
    }
  /**
     * 提交修改后的文件
     * @param localPath 本地修改的代码地址
     * @param remotePath 需要提交到的远程地址
     * @param message 提交信息
     * @throws IOException
     * @throws GitAPIException
     */
    public static void gitAddYamlCommit(String localPath,String remotePath,String message) throws IOException, GitAPIException {
        //找到本地的git路径,一般在隐藏.git文件夹下
        Git git = new Git(new FileRepository(localPath +  JGitCommon.getFileName(remotePath)+"/.git"));
        //提交代码
        git.add().addFilepattern(".").call();
        git.commit().setAll(true).setMessage(message).call();

        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                UsernamePasswordCredentialsProvider(_configReader.getVal("UserName"),_configReader.getVal("PassWord"));
        //git仓库地址
        git.push().setRemote("origin").setCredentialsProvider(usernamePasswordCredentialsProvider).call();
        System.out.println(remotePath+"===提交成功");
    }
  /**
     * 推送新分支到远程
     * @param localPath  本地git地址
     * @param remotePath  远程git地址
     * @param newbranch  新分支名称
     * @return
     */
    public static void newBranch(String localPath,String remotePath,String newbranch){
        try {
//            账号密码认证
            UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider =new
                    UsernamePasswordCredentialsProvider(_configReader.getVal("UserName"),_configReader.getVal("PassWord"));
            //找到本地的git路径,一般在隐藏.git文件夹下
            Git git = new Git(new FileRepository(localPath +  JGitCommon.getFileName(remotePath)+"/.git"));
            //检查新建的分支是否已经存在,如果存在则将已存在的分支强制删除并新建一个分支
            List<Ref> refs = git.branchList().call();
            for (Ref ref : refs) {
                if (ref.getName().equals(newbranch)) {
                    System.out.println("Removing branch before");
                    git.branchDelete()
                            .setBranchNames(newbranch)
                            .setForce(true)
                            .call();
                    break;
                }
            }
            //新建分支
            Ref ref = git.branchCreate().setName(newbranch).call();
            //推送到远程
            git.push().add(ref).setCredentialsProvider(usernamePasswordCredentialsProvider).call();
            System.out.println(newbranch+"分支推送完毕");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

无奈的码农

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值