怪物奇妙物语

宇宙无敌超级美少男的怪物奇妙物语

首页 新随笔 联系 管理

gitpython将每次的commit导出项目(可以导出到指定文件夹)

import git
import subprocess
import os
from git.repo import Repo
from git.repo.fun import is_git_dir


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):
            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)

    def export_archive(self,fp,file_path):
        """
        将commit导出作为一个项目文件
        """
        self.repo.git.archive(fp, output=file_path)
    
   

if __name__ == '__main__':
    # /Users/song/Code/threejs_哔站_汐月风尘/three-learner
    # 使用git的项目文件夹
    local_path = '/Users/song/Code/threejs_哔站_汐月风尘/three-learner' 
    # 输出文件夹
    out_path = os.path.join(local_path, 'out')
    # git项目的远程地址
    repo_path = ''
    # 初始化GitRepository
    repo = GitRepository(local_path,repo_path)
    # 获取所有的提交记录
    commit_list = repo.commits()
    for commit in commit_list:

        time = commit['date'].replace(' ','_').replace(':','')
        summary = commit['summary'].replace(':','_')
        output_file = os.path.join(out_path,time+"_"+summary+'.zip')
        commit_sha = commit['commit']

        # 使用git archive命令,将每次的提交导出项目
        subprocess.call(['git', 'archive', '--format=zip',  '-o', output_file, commit_sha], cwd=local_path)

gitpython导出(只能导出到当前文件夹)

import os
from git.repo import Repo
from git.repo.fun import is_git_dir


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):
            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)

    def export_archive(self,fp,file_path):
        """
        将commit导出作为一个项目文件
        """
        self.repo.git.archive(fp, output=file_path)
    
   

if __name__ == '__main__':
    # /Users/song/Code/threejs_哔站_汐月风尘/three-learner
    local_path = '/Users/song/Code/threejs_哔站_汐月风尘/three-learner' 
    out_path = os.path.join(local_path, 'out')
    repo_path = ''
    repo = GitRepository(local_path,repo_path)

    commit_list = repo.commits()
    for commit in commit_list:

        time = commit['date'].replace(' ','_').replace(':','')
        summary = commit['summary'].replace(':','_')
        commit_exp = repo.repo.commit(commit['commit'])
        file_name = time+"_"+summary+'.zip'

        with open(file_name, 'wb') as fp:
            # 使用gitpython也是可以导出项目,但是只能导出到当前目录,不能导出到指定目录
            repo.repo.archive(fp,treeish=commit_exp,format='zip')
posted on 2023-04-11 22:38  超级无敌美少男战士  阅读(40)  评论(0编辑  收藏  举报