zoukankan      html  css  js  c++  java
  • java代码操作git实现仓库代码下载至指定目录

    想要用代码操作gitlab,实现仓库代码下载。

    依赖jgit工具:

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

    全量代码:

    package com.controller;
    
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.lang3.StringUtils;
    import org.eclipse.jgit.api.Git;
    import org.eclipse.jgit.internal.storage.file.FileRepository;
    import org.eclipse.jgit.lib.Repository;
    import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;
    import org.junit.Test;
    
    import java.io.File;
    
    /**
     * @Author Ctrl`
     * @Since 2020/10/16.
     */
    @Slf4j
    public class GitController {
    
        private String localPath;
        private Repository localRepo;
        private Git git;
    
        @Test
        public void tt(){
            download("test","http://***.git","dev");
        }
    
        /**
         * git代码下载
         */
        public String download(String projectName, String gitUrl,String branch) {
            if(StringUtils.isBlank(gitUrl)){
                return "git仓库地址不能为空";
            }
            //认证凭据
            UsernamePasswordCredentialsProvider credentialsProvider = new UsernamePasswordCredentialsProvider("登录git的邮箱","登录密码");
            try {
                //代码指定存储目录
                localPath = "E:\git_repository" + File.separator + projectName;
                System.out.println("============localPath==========" + localPath);
                localRepo = new FileRepository(localPath + "/.git");
                git = new Git(localRepo);
                File localPathFile = new File(localPath);
                if (!localPathFile.exists()) {
                    gitClone(gitUrl, branch, localPath,credentialsProvider);
                } else {
                    gitPull(branch,credentialsProvider);
                }
            } catch (Exception e) {
                log.error(e.getMessage());
                e.printStackTrace();
            }
            return localPath;
        }
    
        /**
         * 如果没有该代码目录,执行git clone
          */
        private void gitClone(String gitUrl, String branch, String localPath,UsernamePasswordCredentialsProvider credentialsProvider) throws Exception {
            Git.cloneRepository().setURI(gitUrl).setBranch(branch)
                        .setDirectory(new File(localPath)).setCredentialsProvider(credentialsProvider).call();
        }
    
        /**
         * 如果有代码,git pull
         */
        private void gitPull(String branch,UsernamePasswordCredentialsProvider credentialsProvider) throws Exception {
            git.pull().setRemoteBranchName(branch).setCredentialsProvider(credentialsProvider).call();
        }
    
    }

    使用ssh时会报错,后续更新。

  • 相关阅读:
    Vpython简单例子
    我在读的书:《ACM图灵奖:19662006(第三版)计算机发展史的缩影》
    可惜啊,没见到姚期智~~
    The Sounds of Music 观后感
    终于在博客园申请开通博客了
    【引用】Python open读写文件实现脚本
    在Python中使用中文
    Discovery:深入理解计算机系统 (Ver.2) 中文版
    [导入]一个都不能少:全面认识IE插件
    [导入]午间心情
  • 原文地址:https://www.cnblogs.com/haohao111/p/13827105.html
Copyright © 2011-2022 走看看