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 相关文章推荐
numpy.ndarray 交换多维数组(矩阵)的行/列方法
Aug 02 Python
Python matplotlib通过plt.scatter画空心圆标记出特定的点方法
Dec 13 Python
Python os.access()用法实例
Feb 18 Python
Python实现读取txt文件中的数据并绘制出图形操作示例
Feb 26 Python
详解python中TCP协议中的粘包问题
Mar 22 Python
Python API 自动化实战详解(纯代码)
Jun 11 Python
python中多个装饰器的调用顺序详解
Jul 16 Python
python 批量修改 labelImg 生成的xml文件的方法
Sep 09 Python
python 队列基本定义与使用方法【初始化、赋值、判断等】
Oct 24 Python
Matlab中plot基本用法的具体使用
Jul 17 Python
python 元组和列表的区别
Dec 30 Python
selenium3.0+python之环境搭建的方法步骤
Feb 01 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通过function_exists检测函数是否存在的方法
2015/03/18 PHP
dvwa+xampp搭建显示乱码的问题及解决方案
2015/08/23 PHP
如何解决PHP使用mysql_query查询超大结果集超内存问题
2016/03/14 PHP
PHP正则表达式笔记与实例详解
2019/05/09 PHP
TP5框架实现的数据库备份功能示例
2020/04/05 PHP
使用户点击后退按钮使效三行代码
2007/07/07 Javascript
js模拟滚动条(横向竖向)
2013/02/22 Javascript
jquery获取div距离窗口和父级dv的距离示例
2013/10/10 Javascript
showModelDialog弹出文件下载窗口的使用示例
2013/11/19 Javascript
jquery左右全屏大尺寸多图滑动效果代码分享
2015/08/28 Javascript
jquery mobile开发常见问题分析
2016/01/21 Javascript
JS判断日期格式是否合法的简单实例
2016/07/11 Javascript
AngularJS指令与控制器之间的交互功能示例
2016/12/14 Javascript
AngulaJS路由 ui-router 传参实例
2017/04/28 Javascript
纯JS实现简单的日历
2017/06/26 Javascript
详解vue.js的事件处理器v-on:click
2017/06/27 Javascript
Webstorm2016使用技巧(SVN插件使用)
2018/10/29 Javascript
vue 详情跳转至列表页实现列表页缓存
2019/03/27 Javascript
js中forEach,for in,for of循环的用法示例小结
2020/03/14 Javascript
js实现炫酷光感效果
2020/09/05 Javascript
python使用正则表达式检测密码强度源码分享
2014/06/11 Python
OpenCV+python手势识别框架和实例讲解
2018/08/03 Python
python3使用flask编写注册post接口的方法
2018/12/28 Python
python将pandas datarame保存为txt文件的实例
2019/02/12 Python
详解Python 调用C# dll库最简方法
2019/06/20 Python
Python实现的爬取豆瓣电影信息功能案例
2019/09/15 Python
Python Process创建进程的2种方法详解
2021/01/25 Python
HTML5 placeholder属性详解
2016/06/22 HTML / CSS
营业员实习自我鉴定
2013/12/07 职场文书
难忘的一课教学反思
2014/04/30 职场文书
争当四好少年演讲稿
2014/09/13 职场文书
村党的群众路线教育实践活动总结材料
2014/10/31 职场文书
幼师中班个人总结
2015/02/12 职场文书
销售业务员岗位职责
2015/02/13 职场文书
《法国号》教学反思
2016/02/22 职场文书
详解MongoDB排序时内存大小限制与创建索引的注意事项
2022/05/06 MongoDB