用Python操作git命令

摘要:
importosfromgit.repoimportRepofgit.repo.funportis_ git_ DirclassGitRepository(对象):“”“git仓库管理”“”def__init__(self,local_path,repo_url,branch='master'):self.local_ path=local_路径self.repo _ url=repo
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("
")
        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)


if __name__ == '__main__':
    local_path = os.path.join('codes', 'luffycity')
    repo = GitRepository(local_path, 'https://gitee.com/wupeiqi/fuck.git')
    branch_list = repo.branches()
    print(branch_list)
    repo.change_to_branch('dev')
    repo.pull()

免责声明:文章转载自《用Python操作git命令》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇SAP各模组简称Unity简单的通过鼠标点击移动下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

python继承的练习

继承的父类是上一篇python类和实例里的类 1、冰淇淋小店:冰淇淋小店是一种特殊的餐馆。编写一个名为IceCreamStand的类,让它继承Restaurant类。这两个版本的Restaurant类都可以,挑选你更喜欢的那个即可。添加一个名为flavors的属性,用于存储一个由各种口味的冰淇淋组成的列表。编写一个显示这些冰淇淋的方法。创建一个IceCre...

Jenkins+pipeline+参数构建+人工干预确定

  Jenkins+pipeline+参数构建+人工干预 实现了以下功能 1. 可以选择环境,单选;可以选择需要发布的项目,多选 2.发布过程可视化 3. 可以人工干预是否继续发布。 初始化配置需要很久,比如拉镜像这些事情,我可以提前操作。配置做好之后,等到下班时间,再进行发布操作。有时候会遇到,我初始化配置做好之后,测试通知还有变动。我可以人工干预,...

python轻量级orm框架 peewee常用功能速查

peewee常用功能速查 peewee 简介 Peewee是一种简单而小的ORM。它有很少的(但富有表现力的)概念,使它易于学习和直观的使用。 常见orm数据库框架 Django ORM peewee SQLAlchemy Django ORM 优点:易用,学习曲线短和Django紧密集合,用Django时使用约定俗成的方法去操作数据库缺点:Quer...

Python3 -- 查看python安装路径以及pip安装的包列表及路径

1. 查看python路径终端输入命令: whereis python 假设你的电脑上同时安装了python2和python3,可以使用使用以下命令分别查看其安装路径: whereis python2 whereis python3 2.查看使用pip安装的软件包默认python3 pip list python2查看pip安装的软件包名称及版本 pyth...

Git 【管理篇】

Git 介绍 Git 是什么?傻瓜内容跟踪器(The stupid content tracker)!Git 创始人、Linux 之父 Linus Torvalds 就是这样介绍 Git 的。 Git 是用于 Linux 内核开发的版本控制工具。与常用的版本控制工具 CVS, Subversion 等不同,它采用了分布式版本库的方式,服务器端软件支持不再...

windows+django3.1+ASGI+nginx部署

# 了解CGI CGI(通用网关接口, Common Gateway Interface/CGI),定义客户端与Web服务器的交流方式的一个程序。 #  什么是WSGI PythonWeb服务器网关接口(Python Web Server Gateway Interface,缩写为WSGI)是Python应用程序或框架和Web服务器之间的一种接口,已经被广...