Python的高级Git库 Gittle


Posted in Python onSeptember 22, 2014

Gittle是一个高级纯python git 库。构建在dulwich之上,提供了大部分的低层机制。

Install it

pip install gittle

Examples :

Clone a repository

from gittle import Gittle
 
repo_path = '/tmp/gittle_bare'
repo_url = 'git://github.com/FriendCode/gittle.git'
 
repo = Gittle.clone(repo_url, repo_path)

With authentication (see Authentication section for more information) :

auth = GittleAuth(pkey=key)
Gittle.clone(repo_url, repo_path, auth=auth)

Or clone bare repository (no working directory) :

repo = Gittle.clone(repo_url, repo_path, bare=True)

Init repository from a path

repo = Gittle.init(path)

Get repository information

# Get list of objects
repo.commits
 
# Get list of branches
repo.branches
 
# Get list of modified files (in current working directory)
repo.modified_files
 
# Get diff between latest commits
repo.diff('HEAD', 'HEAD~1')

Commit

# Stage single file
repo.stage('file.txt')
 
# Stage multiple files
repo.stage(['other1.txt', 'other2.txt'])
 
# Do the commit
repo.commit(name="Samy Pesse", email="samy@friendco.de", message="This is a commit")

Pull

repo = Gittle(repo_path, origin_uri=repo_url)
 
# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# Do pull
repo.pull()

Push

repo = Gittle(repo_path, origin_uri=repo_url)
 
# Authentication with RSA private key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# Do push
repo.push()

Authentication for remote operations

# With a key
key_file = open('/Users/Me/keys/rsa/private_rsa')
repo.auth(pkey=key_file)
 
# With username and password
repo.auth(username="your_name", password="your_password")

Branch

# Create branch off master
repo.create_branch('dev', 'master')
 
# Checkout the branch
repo.switch_branch('dev')
 
# Create an empty branch (like 'git checkout --orphan')
repo.create_orphan_branch('NewBranchName')
 
# Print a list of branches
print(repo.branches)
 
# Remove a branch
repo.remove_branch('dev')
 
# Print a list of branches
print(repo.branches)

Get file version

versions = repo.get_file_versions('gittle/gittle.py')
print("Found %d versions out of a total of %d commits" % (len(versions), repo.commit_count()))

Get list of modified files (in current working directory)

repo.modified_files

Count number of commits

repo.commit_count

Get information for commits

List commits :

# Get 20 first commits repo.commit_info(start=0, end=20)

With a given commit :

commit = "a2105a0d528bf770021de874baf72ce36f6c3ccc"

Diff with another commit :

old_commit = repo.get_previous_commit(commit, n=1)
print repo.diff(commit, old_commit)

Explore commit files using :

commit = "a2105a0d528bf770021de874baf72ce36f6c3ccc"
 
# Files tree
print repo.commit_tree(commit)
 
# List files in a subpath
print repo.commit_ls(commit, "testdir")
 
# Read a file
print repo.commit_file(commit, "testdir/test.txt")

Create a GIT server

from gittle import GitServer
 
# Read only
GitServer('/', 'localhost').serve_forever()
 
# Read/Write
GitServer('/', 'localhost', perm='rw').serve_forever()
Python 相关文章推荐
一个超级简单的python web程序
Sep 11 Python
python采用getopt解析命令行输入参数实例
Sep 30 Python
python pandas中DataFrame类型数据操作函数的方法
Apr 08 Python
Python使用pip安装pySerial串口通讯模块
Apr 20 Python
对python中执行DOS命令的3种方法总结
May 12 Python
由Python编写的MySQL管理工具代码实例
Apr 09 Python
Python高级特性 切片 迭代解析
Aug 23 Python
在Django中实现添加user到group并查看
Nov 18 Python
python使用OpenCV模块实现图像的融合示例代码
Apr 10 Python
解决Jupyter notebook更换主题工具栏被隐藏及添加目录生成插件问题
Apr 20 Python
python3.x中安装web.py步骤方法
Jun 23 Python
python数据分析之单因素分析线性拟合及地理编码
Jun 25 Python
Python实现抓取网页并且解析的实例
Sep 20 #Python
跟老齐学Python之字典,你还记得吗?
Sep 20 #Python
跟老齐学Python之再深点,更懂list
Sep 20 #Python
跟老齐学Python之画圈还不简单吗?
Sep 20 #Python
跟老齐学Python之list和str比较
Sep 20 #Python
Python显示进度条的方法
Sep 20 #Python
python中对list去重的多种方法
Sep 18 #Python
You might like
php之Memcache学习笔记
2013/06/17 PHP
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
2013/07/01 PHP
分享php多功能图片处理类
2016/05/15 PHP
PHP扩展框架之Yaf框架的安装与使用
2016/05/18 PHP
php生成二维码不保存服务器还有下载功能的实现代码
2018/08/09 PHP
textContent在Firefox下与innerText等效的属性
2007/05/12 Javascript
jquery 插件开发方法小结
2009/10/23 Javascript
js如何调用qq互联api实现第三方登录
2014/03/28 Javascript
Windows系统下Node.js的简单入门教程
2015/06/23 Javascript
JavaScript生成SQL查询表单的方法
2015/08/13 Javascript
jquery实现鼠标点击后展开列表内容的导航栏效果
2015/09/14 Javascript
AngularJS中实现用户访问的身份认证和表单验证功能
2016/04/21 Javascript
Function.prototype.apply()与Function.prototype.call()小结
2016/04/27 Javascript
NodeJs通过async/await处理异步的方法
2017/10/09 NodeJs
Node做中转服务器转发接口
2017/10/18 Javascript
Angular4.x通过路由守卫进行路由重定向实现根据条件跳转到相应的页面(推荐)
2018/05/10 Javascript
微信小程序搭建(mpvue+mpvue-weui+fly.js)的详细步骤
2018/09/18 Javascript
VUE解决微信签名及SPA微信invalid signature问题(完美处理)
2019/03/29 Javascript
JavaScript中callee和caller的区别与用法实例分析
2019/06/28 Javascript
Vue.js递归组件实现组织架构树和选人功能案例分析
2019/07/03 Javascript
Element Dialog对话框的使用示例
2020/07/26 Javascript
详解Python程序与服务器连接的WSGI接口
2015/04/29 Python
Python for循环与getitem的关系详解
2020/01/02 Python
Python matplotlib绘制图形实例(包括点,曲线,注释和箭头)
2020/04/17 Python
Python实现一个简单的递归下降分析器
2020/08/01 Python
详解Python中Pyyaml模块的使用
2020/10/08 Python
css3 伪类选择器快速复习小结
2019/09/10 HTML / CSS
字中字效果的实现【html5实例】
2016/05/03 HTML / CSS
VICHY薇姿英国官网:全球专业敏感肌护肤领先品牌
2017/07/04 全球购物
英国的领先快速时尚零售商:In The Style
2019/03/25 全球购物
监理资料员岗位职责
2014/01/03 职场文书
咖啡馆创业计划书
2014/01/26 职场文书
新文化运动的基本口号
2014/06/21 职场文书
乡镇法制宣传日活动总结
2015/05/05 职场文书
优秀班主任工作总结2015
2015/05/25 职场文书
MySql统计函数COUNT的具体使用详解
2022/08/14 MySQL