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 相关文章推荐
用smtplib和email封装python发送邮件模块类分享
Feb 17 Python
Django中间件工作流程及写法实例代码
Feb 06 Python
Python使用Selenium模块实现模拟浏览器抓取淘宝商品美食信息功能示例
Jul 18 Python
Python实现常见的回文字符串算法
Nov 14 Python
python机器学习包mlxtend的安装和配置详解
Aug 21 Python
Python之Numpy的超实用基础详细教程
Oct 23 Python
python 图像的离散傅立叶变换实例
Jan 02 Python
Python如何使用字符打印照片
Jan 03 Python
selenium+python配置chrome浏览器的选项的实现
Mar 18 Python
如何利用python web框架做文件流下载的实现示例
Jun 02 Python
详解Python 最短匹配模式
Jul 29 Python
python OpenCV学习笔记
Mar 31 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
星际争霸 Starcraft 发展史
2020/03/14 星际争霸
phpmyadmin 常用选项设置详解版
2010/03/07 PHP
php中根据某年第几天计算出日期年月日的代码
2011/02/24 PHP
PHP substr 截取字符串出现乱码问题解决方法[utf8与gb2312]
2011/12/16 PHP
php采用ajax数据提交post与post常见方法总结
2014/11/10 PHP
JavaScript中的16进制字符(改进)
2011/11/21 Javascript
JS时间选择器 兼容IE6,7,8,9
2012/06/26 Javascript
javascript动态的改变IFrame的高度实现自动伸展
2013/10/12 Javascript
刷新页面的几种方法小结(JS,ASP.NET)
2014/01/07 Javascript
setTimeout自动触发一个js的方法
2014/01/15 Javascript
jQuery实现的向下图文信息滚动效果
2015/05/03 Javascript
js实现遍历含有input的table实例
2015/12/07 Javascript
JavaScript 动态三角函数实例详解
2017/01/08 Javascript
vue2.0结合DataTable插件实现表格动态刷新的方法详解
2017/03/17 Javascript
详解JavaScript中return的用法
2017/05/08 Javascript
微信小程序实现的贪吃蛇游戏【附源码下载】
2018/01/03 Javascript
jquery判断滚动条距离顶部的距离方法
2018/09/05 jQuery
Vue中的组件及路由使用实例代码详解
2019/05/22 Javascript
Python random模块(获取随机数)常用方法和使用例子
2014/05/13 Python
Python 类的继承实例详解
2017/03/25 Python
Python3生成手写体数字方法
2018/01/30 Python
Python实现曲线拟合操作示例【基于numpy,scipy,matplotlib库】
2018/07/12 Python
Python 虚拟空间的使用代码详解
2019/06/10 Python
简述Html5 IphoneX 适配方法
2018/02/08 HTML / CSS
美国著名的婴儿学步鞋老品牌:Robeez
2016/08/20 全球购物
Tretorn美国官网:瑞典外套和鞋类品牌,抵御风雨
2018/07/19 全球购物
中国制造网:Made-in-China.com
2019/10/25 全球购物
水污染治理专业毕业生推荐信
2013/11/14 职场文书
办公室内勤工作职责
2013/12/11 职场文书
执行力心得体会
2013/12/31 职场文书
给实习单位的感谢信
2014/02/01 职场文书
《桃林那间小木屋》教学反思
2014/05/01 职场文书
乡镇创先争优活动总结
2014/08/28 职场文书
教师自我剖析材料(四风问题)
2014/09/30 职场文书
CocosCreator入门教程之网络通信
2021/04/16 Javascript
python和Appium的移动端多设备自动化测试框架
2022/04/26 Python