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模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...
随机推荐
- java 补充(final、static)
final 固定的 final 修饰类的时候,只能作为子类继承,不能作为父类. final 定义变量时,必须给成员变量赋值.------ 1.直接赋值 2.构造方法. final 修饰成员方法时 ...
- dfs的两种处理方法
方法一: 对于源点s,初始化vis[s]=1,并且在dfs之后vis[s]=1,为下一次调用做准备 .对于dfs递归中的寻找后继的循环体,入栈出栈语句写在循环内. 模板: //调用 vis[s]=; ...
- 洛谷 P1965 转圈游戏
洛谷 P1965 转圈游戏 传送门 思路 每一轮第 0 号位置上的小伙伴顺时针走到第 m 号位置,第 1 号位置小伙伴走到第 m+1 号位置,--,依此类推,第n − m号位置上的小伙伴走到第 0 号 ...
- python3 mqtt 客户端以及服务端
pip3 install paho-mqtt client #!/usr/bin/env python #coding=utf- import json import sys import os im ...
- [Noip2018]填数游戏
传送门 Description 耳熟能详,就不多说了 Solution 对于一个不会推式子的蒟蒻,如何在考场优雅地通过此题 手玩样例,发现对于 \(n=1\) , \(ans=2^m\) .对于 \( ...
- “/usr/local/lib/libosipparser2.so.7: could not read symbols: Invalid operation” 异常解决
编译c代码报错如下 /usr/bin/ld: /tmp/ccl8nBND.o: undefined reference to symbol 'osip_message_get_body' /usr/b ...
- Linux中文件权限查看和修改
权限定义 linux文件权限分为:r读权限(4).w写权限(2).x执行权限(1) linux权限对象分为:拥有者.组用户.其他用户 权限修改: chown user:group /usr/local ...
- Nginx通过geo模式实现限速白名单和全局负载均衡 - 运维笔记
Nginx的geo模块不仅可以有限速白名单的作用,还可以做全局负载均衡,可以要根据客户端ip访问到不同的server.比如,可以将电信的用户访问定向到电信服务器,网通的用户重 定向到网通服务器”,从而 ...
- Java注解及其原理以及分析spring注解解析源码
注解的定义 注解是那些插入到源代码中,使用其他工具可以对其进行处理的标签. 注解不会改变程序的编译方式:Java编译器对于包含注解和不包含注解的代码会生成相同的虚拟机指令. 在Java中,注解是被当做 ...
- 【Python爬虫案例学习】Python爬取天涯论坛评论
用到的包有requests - BeautSoup 我爬的是天涯论坛的财经论坛:'http://bbs.tianya.cn/list.jsp?item=develop' 它里面的其中的一个帖子的URL ...