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. 4-OpenResty 配置 https 访问

    首先是下载证书  https://www.cnblogs.com/yangfengwu/p/11809757.html 因为咱用的 Nginx 所以 修改这个 server { listen ssl; ...

  2. [300iq Contest 1-D]Dates

    传送门 Description 每个妹子匹配一个时间区间,每个时间最多选择\(a_i\)个妹子,每个妹子有一个快乐值,最大化总快乐值 Solution 贪心,从大往小取,能取则取 判断是否可以有完美匹 ...

  3. 【python学习案例】python判断自身是否正在运行

    需要引入psutil包: 实现思路: 1)用os.getpid()获取当前程序运行PID,将PID存入文件中 2)用psutil模块获取当前系统所有正在运行的pid 3)读取之前存入的PID,判断该P ...

  4. 【07月01日】A股滚动市净率PB历史新低排名

    2010年01月01日 到 2019年07月01日 之间,滚动市净率历史新低排名. 上市三年以上的公司,2019年07月01日市净率在30以下的公司. 来源:A股滚动市净率(PB)历史新低排名. 1 ...

  5. hanlp添加词典不起作用

    不起作用的原因很多,这里列举几个 这里的hanlp我虽然用的maven建立的但是要添加自定义词典,所以没有用maven引入的方式,而是下载了data+hanlp.jar文件          data ...

  6. 【LeetCode】在排序数组中查找元素的第一个和最后一个位置【三次二分】

    给定一个按照升序排列的整数数组 nums,和一个目标值 target.找出给定目标值在数组中的开始位置和结束位置. 你的算法时间复杂度必须是 O(log n) 级别. 如果数组中不存在目标值,返回 [ ...

  7. JSR223 PostProcessor VS BeanShell PostProcessor in JMeter

    I would recommend using JSR223 PostProcessor About performance: In JMeter's official user manual, Ab ...

  8. npm install 报错 error Unexpected end of JSON input while parsing near '...sShrinkwrap":false,"d' 解决办法

    npm install 报错 : error Unexpected end of JSON input while parsing near '...sShrinkwrap":false,& ...

  9. GoF的23种设计模式之结构型模式的特点和分类

    结构型模式描述如何将类或对象按某种布局组成更大的结构.它分为类结构型模式和对象结构型模式,前者采用继承机制来组织接口和类,后者釆用组合或聚合来组合对象. 由于组合关系或聚合关系比继承关系耦合度低,满足 ...

  10. Cglib 与 JDK动态代理

    作者:xiaolyuh 时间:2019/09/20 09:58 AOP 代理的两种实现: jdk是代理接口,私有方法必然不会存在在接口里,所以就不会被拦截到: cglib是子类,private的方法照 ...