python基于ssh实现git仓库的pull以及push

本文最后更新于:2024年2月16日星期五下午3点57分

python基于ssh实现git仓库的pull以及push

GitPython==3.1.27

代码

from datetime import datetime
from git import Repo
import re
class gitrepo:
    def __init__(self, git_repo="[email protected]:FoldCle/Foldcle.com.git",git_to='./Foldcle'):
        """赋予name和repo属性

        Args:
            git_repo (str, optional): _description_. Defaults to '[email protected]:FoldCle/Foldcle.com.git'.
            git_to (str, optional): _description_. Defaults to './Foldcle'.
        """
        pattern='.*/(.*?).git'
        self.name=re.findall(pattern,git_repo)[0] # virusps
        print(f'正在检测仓库[{self.name}]更新...')
        try:
            repo = Repo.clone_from(url=git_repo, to_path=git_to)
            print(f'✔ [{self.name}]已下载到本地')
        except:
            repo = Repo(git_to)
            origin = repo.remote(name='origin')
            try:
                origin.pull()
                print(f"✔ [{self.name}]更新完成"+ '\n')
            except:
                print(f"[{self.name}]需人工检查文件版本"+ '\n')
                input('程序已暂停')
        self.repo=repo
    
   
    def update_git(self,add_list=['*'],commit_str=datetime.now().strftime('%Y-%m-%d %H:%M:%S')):
        repo=self.repo
        print(f'[{self.name}]上传更新中...')
        try:
            for i in add_list:
                repo.git.add(i)
        except:
            print(f'[{self.name}] git add Error!')
        try:
            repo.git.commit('-m', commit_str)
            repo.git.push()
            print(f'[{self.name}]上传成功!'+'\n')
        except:
            print('出错了:文件并未修改'+'\n')
            pass

小弟不才,烦请各位前辈批评指正


python基于ssh实现git仓库的pull以及push
https://asyu.in/gongju/py-git_repo/
作者
Yu
发布于
2022年7月3日
更新于
2024年2月16日 15时
许可协议