首页 技术 正文
技术 2022年11月9日
0 收藏 408 点赞 4,085 浏览 2917 个字

GitPython模块

安装:

pip3 install gitpython
  • Gitpython 操作
import os
from git.repo import Repo
import jsondef clone():
"""
克隆代码
:return:
"""
download_path = os.path.join('code', 'codetest')
Repo.clone_from('https://github.com/novice-gamer/codetest.git', to_path=download_path, branch='master')def pull():
"""
pull最新代码
:return:
"""
local_path = os.path.join('code', 'codetest')
repo = Repo(local_path)
repo.git.pull()def get_branch():
"""
获取所有分支
:return:
"""
local_path = os.path.join('code', 'codetest')
repo = Repo(local_path)
branches = repo.remote().refs
for item in branches:
print(item.remote_head)def get_tags():
"""
获取所有版本
:return:
"""
local_path = os.path.join('code', 'codetest')
repo = Repo(local_path)
for tag in repo.tags:
print(tag.name)def get_commits():
"""
获取所有commit
:return:
"""
local_path = os.path.join('code', 'codetest')
repo = Repo(local_path) # 格式化输出
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 = [json.loads(item) for item in log_list]
for commitlog in real_log_list:
print(commitlog)def checkout():
"""
切换分支和回滚
:return:
"""
local_path = os.path.join('code', 'codetest')
repo = Repo(local_path)
before = repo.git.branch() # 查看分支
print(before)
repo.git.reset('--hard', '854ead2e82dc73b634cbd5afcf1414f5b30e94a8') # 回滚

Gitpython 操作类

import os
from git.repo import Repo
from git.repo.fun import is_git_dirclass 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)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开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,071
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,549
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,397
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,174
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,809
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,889