[Git] Pull part of the code [Original]

1. Reference


2. Realization

Currently working on a release system, because some projects cannot be released in full, but only incrementally, so you need to pull part of the code from git. The following is the implementation of git:

mkdir 仓库名
cd 仓库名
git init 
git remote add origin git地址

// 开启Sparse Checkout模式
git config core.sparsecheckout true 

// 设置需Check Out的文件
echo "文件路径" >> .git/info/sparse-checkout 

// Check Out
git pull origin 分支名(一般是master) 

3. Code example

This logic is implemented using Python3, you can refer to:

import os
import signal
import subprocess
import platform
import json


def get_git_file():
    """
    拉取git的部分代码
    :return:
    """
    try:
        release_info = {
    
    
            'git_path': 'xxxx',
            'git_branch': 'sit', # 分支,一般是master
            'update_files': [
                'src/scbyao.exb',
                'src/cmpeod03.sjl',
                'src/aibd230.btb',
                'src/gdot5810.cpy',
                'src/ibbdpbal.btb',
                'src/sczics.olb',
                'src/bptbank.tbl',
                'src/vflhead.prm',
                'src/[email protected]',
                'src/cmbdinf.btb',
                'src/bptevet.tbl',
                'src/ddztdcwh.olb'
            ],
        }
        git_folder = '/data/git_folder/test'

        # 初始化
        cmd = 'cd %s && git init' % git_folder
        print('git初始化,执行的命令为: %s' % cmd)
        (code, msg) = run_cmd(cmd)
        print('git初始化,执行的结果为: %s' % msg)

        if code:
            raise Exception('git初始化失败,原因是:%s' % msg)

        # 添加仓库
        cmd = 'cd %s && git remote add origin %s' % (git_folder, release_info['git_path'])
        print('添加仓库,执行的命令为: %s' % cmd)
        (code, msg) = run_cmd(cmd)
        print('添加仓库,执行的结果为: %s' % msg)

        if code:
            raise Exception('添加仓库,原因是:%s' % msg)

        # 开启Sparse Checkout模式
        cmd = 'cd %s && git config core.sparsecheckout true' % git_folder
        print('开启Sparse Checkout模式,执行的命令为: %s' % cmd)
        (code, msg) = run_cmd(cmd)
        print('开启Sparse Checkout模式,执行的结果为: %s' % msg)

        if code:
            msg = '开启Sparse Checkout模式失败,原因是:%s' % msg
            raise Exception(msg)

        # 设置需Checkout的文件
        update_files = []
        for file in release_info['update_files']:
            cmd = 'cd %s && echo "%s" >> .git/info/sparse-checkout' % (git_folder, file)
            (code, msg) = run_cmd(cmd)

            if code:
                msg = '设置需Check Out的文件失败,原因是:%s' % msg
                raise Exception(msg)
            update_files.append(file)

        print('需要checkout的文件有:%s' % json.dumps(update_files))

        # 拉代码
        cmd = 'cd %s && git pull origin %s' % (git_folder, release_info['git_branch'])
        print('拉代码,执行的命令为: %s' % cmd)
        (code, msg) = run_cmd(cmd)
        print('拉代码,执行的结果为: %s' % msg)

        return 0, 'success'
    except Exception as e:
        return 1, str(e)


def run_cmd(cmd_string, timeout=600):
    """
    执行命令
    :param cmd_string:  string 字符串
    :param timeout:  int 超时设置
    :return:
    """
    p = subprocess.Popen(cmd_string, stderr=subprocess.STDOUT, stdout=subprocess.PIPE, shell=True, close_fds=True,
                         start_new_session=True)

    format = 'utf-8'
    if platform.system() == "Windows":
        format = 'gbk'

    try:
        (msg, errs) = p.communicate(timeout=timeout)
        ret_code = p.poll()
        if ret_code:
            code = 1
            msg = "[Error]Called Error : " + str(msg.decode(format))
        else:
            code = 0
            msg = str(msg.decode(format))
    except subprocess.TimeoutExpired:
        p.kill()
        p.terminate()
        os.killpg(p.pid, signal.SIGTERM)

        code = 1
        msg = "[ERROR]Timeout Error : Command '" + cmd_string + "' timed out after " + str(timeout) + " seconds"
    except Exception as e:
        code = 1
        msg = "[ERROR]Unknown Error : " + str(e)

    return code, msg

Guess you like

Origin blog.csdn.net/jiandanokok/article/details/106038524