JGit操做

package com.huawei.buildcenter.cicdpipeline.service.gitprocess;

import org.apache.dubbo.common.logger.Logger;
import org.apache.dubbo.common.logger.LoggerFactory;
import org.eclipse.jgit.api.CloneCommand;
import org.eclipse.jgit.api.Git;
import org.eclipse.jgit.internal.storage.file.FileRepository;
import org.eclipse.jgit.lib.Repository;
import org.eclipse.jgit.storage.file.FileRepositoryBuilder;
import org.eclipse.jgit.transport.RefSpec;
import org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider;

import java.io.File;

/**
 * The GitProcess
 *
 */
public class GitProcess {

    private static final Logger LOGGER = LoggerFactory.getLogger(GitProcess.class);

    protected void gitInit(String localRepoPath) {
        Repository newlyCreatedRepo = null;
        try {
            newlyCreatedRepo = FileRepositoryBuilder.create(new File(localRepoPath + "/.git"));
            newlyCreatedRepo.create();
            LOGGER.debug("Git init success");
        } catch (Exception e) {
            LOGGER.error("Git init fail.", e);
        } finally {
            if (newlyCreatedRepo != null) {
                newlyCreatedRepo.close();
            }
        }
    }

    /**
     * 克隆遠程倉庫
     *
     * @param remoteRepoPath:遠端倉庫url
     * @param branch:分支
     * @param userName:用戶名
     * @param passWord:密碼
     */
    protected void gitClone(String remoteRepoPath, String localRepoPath, String branch, String userName,
        String passWord) {
        //設置遠程服務器上的用戶名和密碼
        UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider
            = new UsernamePasswordCredentialsProvider(userName, passWord);
        //克隆代碼庫命令
        CloneCommand cloneCommand = Git.cloneRepository();
        Git git = null;
        try {
            git = cloneCommand.setURI(remoteRepoPath) //設置遠程URI
                .setBranch(branch) //設置clone下來的分支
                .setDirectory(new File(localRepoPath)) //設置下載存放路徑
                .setCredentialsProvider(usernamePasswordCredentialsProvider) //設置權限驗證
                .call();
            LOGGER.debug("Git clone success");
        } catch (Exception e) {
            LOGGER.error("Git clone fail.", e);
        } finally {
            if (git != null) {
                git.close();
            }
        }
    }

    /**
     * 添加文件
     *
     * @param addFilePath:添加文件路徑
     * @param localRepoPath:分支
     * @return boolean:結果
     */
    protected boolean gitAdd(String addFilePath, String localRepoPath) {
        boolean addFileFlag = true;
        Git git = null;
        try {
            git = Git.open(new File(localRepoPath + "/.git"));
            //add file to git
            git.add().addFilepattern(addFilePath).call();
        } catch (Exception e) {
            addFileFlag = false;
        } finally {
            if (git != null) {
                git.close();
            }
        }
        return addFileFlag;
    }

    /**
     * 本地代碼提交
     *
     * @param msg:提交信息
     * @param localRepoPath:本地代碼倉位置
     */
    protected void gitCommit(String msg, String localRepoPath) {
        Git git = null;
        try {
            git = new Git(new FileRepository(localRepoPath + "/.git"));
            //所有提交
            git.commit().setAll(true).setMessage(msg).call();
            LOGGER.debug("Git commit success");
        } catch (Exception e) {
            LOGGER.error("Git commit fail.", e);
        } finally {
            if (git != null) {
                git.close();
            }
        }
    }

    /**
     * 本地代碼提交
     *
     * @param branch:提交信息
     * @param localRepoPath:提交信息
     * @return boolean:提交結果
     */
    protected boolean gitPull(String branch, String localRepoPath) {
        boolean pullFlag = true;
        try (Git git = Git.open(new File(localRepoPath + "/.git"))) {
            git.pull().setRemoteBranchName(branch).call();
        } catch (Exception e) {
            pullFlag = false;
        }
        return pullFlag;
    }

    /**
     * push本地代碼到遠程倉庫
     *
     * @param branch:分支
     * @param userName:用戶名
     * @param passWord:密碼
     */
    protected void gitPush(String remoteRepoPath, String localRepoPath, String branch, String userName,
        String passWord) {
        Git git = null;
        try {
            git = new Git(new FileRepository(localRepoPath + "/.git"));
            UsernamePasswordCredentialsProvider usernamePasswordCredentialsProvider
                = new UsernamePasswordCredentialsProvider(userName, passWord);
            git.push()
                .setRemote(remoteRepoPath)
                .setRefSpecs(new RefSpec(branch))
                .setCredentialsProvider(usernamePasswordCredentialsProvider)
                .call();
            LOGGER.debug("Git push success");
        } catch (Exception e) {
            LOGGER.error("Git push fail.", e);
        } finally {
            if (git != null) {
                git.close();
            }
        }
    }
}
相關文章
相關標籤/搜索