安装
pip3 install gitpython
函数操作git
import os
from git.repo import Repo # Repo是gitpython模块操作git的类,所有的对git的操作,都要通过这个类来完成
def clone():
'''功能:克隆git代码'''
download_path = os.path.join('codes', 'fuck')
# 相当于下面的git代码
# git clone -b master https://gitee.com/zhangchaoyin/gittest.git
# git clone -b v1 https://gitee.com/zhangchaoyin/gittest.git 也可以clone指定版本的代码
Repo.clone_from('https://gitee.com/zhangchaoyin/gittest.git', to_path=download_path, branch='master')
def pull():
'''功能: 拉git代码'''
# 相当于下面的git代码
# git pull origin master
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
repo.git.pull()
def tag_list():
'''功能: 查看所有的tag版本'''
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
tag_list = [item.name for item in repo.tags]
print(tag_list)
def commits_list():
'''功能: 查看所有的commit版本'''
import json
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
commit_log = repo.git.log('--pretty={"commit":"%h","author":"%an","summary":"%s","date":"%cd"}',date='format:%Y-%m-%d %H:%M',max_count=50)
commit_list = [json.loads(item) for item in commit_log.split('\n') ]
print(commit_list)
def branches_list():
'''功能: 查看所有的分支版本'''
pull()
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
branches = repo.remote().refs
branch_list = [item.remote_head for item in branches if item.remote_head != "HEAD"]
def checkout_to_branch(branch='dev'):
'''功能: 切换分支'''
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
# before = repo.git.branch()
# print('当前所在分支:',before)
repo.git.checkout(branch)
# after = repo.git.branch()
# print('当前所在分支:',after)
def checkout_to_commit(commit='ec1d728'):
'''功能: commit回滚'''
# commits_list()
local_path = os.path.join('codes', 'fuck')
repo = Repo(local_path)
repo.git.reset('--hard', commit)
封装到一个类中,以后当做工具。
import os
import json
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'):
"""
:param local_path: 下载下来后存放的目录
:param repo_url: git远程仓库的地址,eg: https://gitee.com/zhangchaoyin/gittest.git
:param branch: 分支名,此处默认为master
"""
self.local_path = local_path
self.repo_url = repo_url
self.repo = None
self.initial(branch)
def initial(self,branch):
"""
初始化git仓库
:param branch: 分支名
:return:
"""
if not os.path.exists(self.local_path):
# 如果没有这个目录就创建目录
os.makedirs(self.local_path)
# 产生类似于: codes/zcy/.git 用于给is_git_dir判断用什么形式克隆
git_local_path = os.path.join(self.local_path, '.git')
if not is_git_dir(git_local_path):
self.repo = Repo.clone_from(self.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')
return [json.loads(item) for item in commit_log.split('\n') ]
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', 'zcy')
repo_object = GitRepository(local_path, 'https://gitee.com/zhangchaoyin/gittest.git')
本文介绍了一个使用GitPython库进行Git仓库操作的实用工具类。该工具类提供了克隆、拉取代码、查看分支和提交历史等功能,并展示了如何切换分支、标签及commit。

1260

被折叠的 条评论
为什么被折叠?



