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模块的更多相关文章

  1. GitPython git python 的开发库

    工程地址: https://pypi.python.org/pypi/GitPython/需要安装先安装: gitdb https://pypi.python.org/pypi/gitdb GitPy ...

  2. salt常用命令、模块、执行

    一.salt常用命令 salt 该命令执行salt的执行模块,通常在master端运行,也是我们最常用到的命令 salt [options] '<target>' <function ...

  3. npm 私有模块的管理使用

    你可以使用 NPM 命令行工具来管理你在 NPM 仓库的私有模块代码,这使得在项目中使用公共模块变的更加方便. 开始前的工作 你需要一个 2.7.0 以上版本的 npm ,并且需要有一个可以登陆 np ...

  4. node.js学习(三)简单的node程序&&模块简单使用&&commonJS规范&&深入理解模块原理

    一.一个简单的node程序 1.新建一个txt文件 2.修改后缀 修改之后会弹出这个,点击"是" 3.运行test.js 源文件 使用node.js运行之后的. 如果该路径下没有该 ...

  5. ES6模块import细节

    写在前面,目前浏览器对ES6的import支持还不是很好,需要用bable转译. ES6引入外部模块分两种情况: 1.导入外部的变量或函数等: import {firstName, lastName, ...

  6. Python标准模块--ContextManager

    1 模块简介 在数年前,Python 2.5 加入了一个非常特殊的关键字,就是with.with语句允许开发者创建上下文管理器.什么是上下文管理器?上下文管理器就是允许你可以自动地开始和结束一些事情. ...

  7. Python标准模块--Unicode

    1 模块简介 Python 3中最大的变化之一就是删除了Unicode类型.在Python 2中,有str类型和unicode类型,例如, Python 2.7.6 (default, Oct 26 ...

  8. Python标准模块--Iterators和Generators

    1 模块简介 当你开始使用Python编程时,你或许已经使用了iterators(迭代器)和generators(生成器),你当时可能并没有意识到.在本篇博文中,我们将会学习迭代器和生成器是什么.当然 ...

  9. 自己实现一个javascript事件模块

    nodejs中的事件模块 nodejs中有一个events模块,用来给别的函数对象提供绑定事件.触发事件的能力.这个别的函数的对象,我把它叫做事件宿主对象(非权威叫法),其原理是把宿主函数的原型链指向 ...

随机推荐

  1. BST | 1043 BST树与镜像BST树的判断

    较为简单.小于大于的都走一遍就可以AC了 #include <stdio.h> #include <memory.h> #include <math.h> #inc ...

  2. Nuxt.js中scss公用文件(不使用官方插件style-resources)

    项目多多少少应该都遇到有公用文件这种情况,比如说偶尔某一天产品来找你,能不能明天把网站的这个颜色给我改下?第二天再来给我换回来? 如果再css2.x时代,不使用css预处理技术,这一改只能“查找替换” ...

  3. 洛谷P5020 货币系统

    题目 题意简化一下就是找题目给定的n个数最多能消掉多少个,我们用个tong[i]来记录i这个数值能不能用小于等于i的货币组合起来,等于1意味着他只能由自己本身的货币组成,等于2说明他可以被其他货币组成 ...

  4. Linux 内核启动信息的打印 --- dev_driver_string函数/dev_name函数

    内核启动时,常会打印出一些信息:开头是 "驱动模块的名字: + 具体的信息" 如:在运行的linux系统设备上,插入鼠标,就会打印出鼠标的相关信息; [ 402.134068] i ...

  5. Svn CleanUp failed解决方案

    在项目目录下找到wc.db文件,使用sqlite工具打开,清空main下的WC_LOCK和 WORK_QUEUE表即可.

  6. python模块、异常

    1. python 模块 模块是一个包含所有你定义的函数和变量的文件,其后缀名是.py.模块可以被别的程序引入,以使用该模块中的函数等功能.这也是使用 python 标准库的方法.(有点像java的c ...

  7. 用结构体解析Pascal字符串

    来源:https://www.cnblogs.com/qiuyuwutong/p/8708844.html 1.什么是柔性数组? 柔性数组既数组大小待定的数组, C语言中结构体的最后一个元素可以是大小 ...

  8. Java学习:面向对象三大特征:封装、继承、多态之封装性

    面向对象三大特征:封装.继承.多态. 封装性在Java当中的体现: 方法就是一种封装 关键字private也是一种封装 封装就是将一些细节信息隐藏起来,对于外界不可见. 问题描述:定义Person的年 ...

  9. Python 中拼音库 PyPinyin 的用法【华为云技术分享】

    [摘要] 最近碰到了一个问题,项目中很多文件都是接手过来的中文命名的一些素材,结果在部署的时候文件名全都乱码了,导致项目无法正常运行. 后来请教了一位大佬怎么解决文件名乱码的问题,他说这个需要正面解决 ...

  10. 谷歌浏览器安装Elasticsearch-head 插件

    下载该插件,地址:https://github.com/liufengji/es-head/blob/master/elasticsearch-head.crx 下载后的文件名是:elasticsea ...