能够让你通过 python 代码操作 git 仓库

安装

pip3 install gitpython

基本使用

import os
from git.repo import Repo

# 下载远程仓库的代码可以怎么搞   clone  pull
# 先定义代码的存放位置
download_path = os.path.join('santa','NB')
Repo.clone_from('https://github.com/DominicJi/TeachTest.git',to_path=download_path,branch='master')

更多操作

# ############## 2. pull最新代码 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('santa', 'NB')
repo = Repo(local_path)
repo.git.pull()


# ############## 3. 获取所有分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('santa', 'NB')
repo = Repo(local_path)
 
branches = repo.remote().refs
for item in branches:
    print(item.remote_head)
    
    
# ############## 4. 获取所有版本 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('santa', 'NB')
repo = Repo(local_path)
 
for tag in repo.tags:
    print(tag.name)

    
# ############## 5. 获取所有commit ##############
import os
from git.repo import Repo
 
local_path = os.path.join('santa', 'NB')
repo = Repo(local_path)
 
# 将所有提交记录结果格式成json格式字符串 方便后续反序列化操作
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}', max_count=50,
                          date='format:%Y-%m-%d %H:%M')
log_list = commit_log.split("\n")
real_log_list = [eval(item) for item in log_list]
print(real_log_list)
 
    
# ############## 6. 切换分支 ##############
import os
from git.repo import Repo
 
local_path = os.path.join('santa', 'NB')
repo = Repo(local_path)
 
before = repo.git.branch()
print(before)
repo.git.checkout('master')
after = repo.git.branch()
print(after)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8')
 
    
# ############## 7. 打包代码 ##############
with open(os.path.join('santa', 'NB.tar'), 'wb') as fp:
    repo.archive(fp)

对模块的诸多功能进行一个封装

class GitRepository(object):
    """
    git仓库管理
    """
    def __init__(self, local_path, repo_url, branch='master'):
        self.local_path = local_path
        self.repo_url = repo_url
        self.repo = None
        self.initial(repo_url, branch)

    def initial(self, repo_url, branch):
        """
        初始化git仓库
        :param repo_url:
        :param branch:
        :return:
        """
        if not os.path.exists(self.local_path):
            # mkdir不能创建多级目录     makedirs可以创建多级目录
            os.makedirs(self.local_path)

        git_local_path = os.path.join(self.local_path, '.git')
        if not is_git_dir(git_local_path):
            self.repo = Repo.clone_from(repo_url, to_path=self.local_path, branch=branch)
        else:
            self.repo = Repo(self.local_path)

    def pull(self):
        """
        从线上拉最新代码
        :return:
        """
        self.repo.git.pull()

    def branches(self):
        """
        获取所有分支
        :return:
        """
        branches = self.repo.remote().refs
        return [item.remote_head for item in branches if item.remote_head not in ['HEAD', ]]

    def commits(self):
        """
        获取所有提交记录
        :return:
        """
        commit_log = self.repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',
                                       max_count=50,
                                       date='format:%Y-%m-%d %H:%M')
        log_list = commit_log.split("\n")
        return [eval(item) for item in log_list]

    def tags(self):
        """
        获取所有tag
        :return:
        """
        return [tag.name for tag in self.repo.tags]

    def change_to_branch(self, branch):
        """
        切换分值
        :param branch:
        :return:
        """
        self.repo.git.checkout(branch)

    def change_to_commit(self, branch, commit):
        """
        切换commit
        :param branch:
        :param commit:
        :return:
        """
        self.change_to_branch(branch=branch)
        self.repo.git.reset('--hard', commit)

    def change_to_tag(self, tag):
        """
        切换tag
        :param tag:
        :return:
        """
        self.repo.git.checkout(tag)

代码发布概述图

ps:当服务器特别多的时候,从同一个地方下载数据回出现压力过大的情况(上传者只有一个,下载者有N多个,上传者压力太大)

如何解决这种问题???

比特流技术

将所有人都变成既可以是上传者也可以是下载者

联想你下载小片片的时候有些速度快游戏速度慢,速度快可能是因为你室友的电脑中就有,你是从你室友的电脑中下载的,速度慢是因为你的周围都没有该资源的提供者

 posted on 2020-04-15 15:17  Rannie`  阅读(2227)  评论(0编辑  收藏  举报
去除动画
找回动画