gitpython 操作 git 倉庫


能夠讓你通過 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多個,上傳者壓力太大)

如何解決這種問題???

比特流技術

將所有人都變成既可以是上傳者也可以是下載者

聯想你下載小片片的時候有些速度快游戲速度慢,速度快可能是因為你室友的電腦中就有,你是從你室友的電腦中下載的,速度慢是因為你的周圍都沒有該資源的提供者


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM