GitPython模块
GitPython模块
安装:
pip3 install gitpython
- Gitpython 操作
import os
from git.repo import Repo
import json
def 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_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)
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()
GitPython模块的更多相关文章
- GitPython git python 的开发库
工程地址: https://pypi.python.org/pypi/GitPython/需要安装先安装: gitdb https://pypi.python.org/pypi/gitdb GitPy ...
- salt常用命令、模块、执行
一.salt常用命令 salt 该命令执行salt的执行模块,通常在master端运行,也是我们最常用到的命令 salt [options] '<target>' <function ...
- npm 私有模块的管理使用
你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...
- node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理
一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...
- ES6模块import细节
写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...
- Python标准模块--ContextManager
1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...
- Python标准模块--Unicode
1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...
- Python标准模块--Iterators和Generators
1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...
- 自己实现一个javascript事件模块
nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- three.js 添加三维坐标系
//显示三维坐标系 ); scene.add(axis);
- 8-STM32物联网开发WIFI(ESP8266)+GPRS(Air202)系统方案安全篇(Apache 配置SSL,HTTPS连接)
https://www.cnblogs.com/yangfengwu/p/10947423.html 和当时配置MQTT差不多,去下载证书文件 https://www.cnblogs.com/ya ...
- 网络协议 19 - RPC协议综述
这几年微服务很火,想必各位博友或多或少的都接触过.微服务概念中, 各服务间的相互调用是不可或缺的一环.你知道微服务之间是通过什么方式相互调用的吗? 你可能说,这还不简单,用 socket 呗. ...
- Redis4.0.9最新版本安装时出现的问题
Redis Redis(REmote DIctionary Server,远程数据字典服务器)是开源的内存数据库,常用作缓存或者消息队列. Redis的特点: Redis存在于内存,使用硬盘作为持久化 ...
- nginx 日志之 access_log分割
如果任由访问日志写下去,日志文件会变得越来越大,甚至是写满磁盘. 所以,我们需要想办法把日志做切割,比如每天生成一个新的日志,旧的日志按规定时间删除即可. 实现日志切割可以通过写shell脚本或者系统 ...
- QT QWidget 关闭的流程
当QWidget被点击右上角“X”关闭时: 1.调用虚函数closeEvent 2.调用QWidget的析构函数
- shell脚本-练习1
- 图文详解如何使用VMWare创建一套虚拟机“集群”
开篇废话 在学习各种高大上的大数据产品也好,模拟部署我们的程序到PRD环境也好,总离不开需要一个机器集群.然而通常我们都没有那么多银子去购买多台云服务器,更没钱购买物理机.所以对于技术研究来说,最经济 ...
- Certification information不能过大
/* If certification information is too big this event can't be transmitted as it would cause failure ...
- .net core 使用本地包
环境:.net core 2.2 类库:.Net Standard 2.0 旧版本: 发布你自己的Nuget包 如果不想把包发布到nuget上面供别人引用,然后你有没有服务器,我们只能使用本地包 ...