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中的XML库4Suite Server的介绍
Apr 14 Python
python开启debug模式的方法
Jun 27 Python
Python获取好友地区分布及好友性别分布情况代码详解
Jul 10 Python
详解Python对JSON中的特殊类型进行Encoder
Jul 15 Python
python 读取数据库并绘图的实例
Dec 03 Python
pytorch实现用CNN和LSTM对文本进行分类方式
Jan 08 Python
python时间日期操作方法实例小结
Feb 06 Python
python GUI库图形界面开发之PyQt5窗口类QMainWindow详细使用方法
Feb 26 Python
python字典的值可以修改吗
Jun 29 Python
python中pathlib模块的基本用法与总结
Aug 17 Python
python如何实现图片压缩
Sep 11 Python
pycharm使用技巧之自动调整代码格式总结
Nov 04 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连接mysql数据库代码
2009/03/10 PHP
10个超级有用的PHP代码片段果断收藏
2015/09/23 PHP
Zend Framework教程之Zend_Db_Table_Row用法实例分析
2016/03/21 PHP
PHP实现对数组分页处理实例详解
2017/02/07 PHP
PHP实现模拟http请求的方法分析
2017/12/20 PHP
Javascript与vbscript数据共享
2007/01/09 Javascript
字符串的replace方法应用浅析
2011/12/06 Javascript
Javascript 垃圾收集机制介绍理解
2013/05/14 Javascript
Jquery 跨域访问 Lightswitch OData Service的方法
2013/09/11 Javascript
javascript简单实现表格行间隔显示颜色并高亮显示
2013/11/29 Javascript
js使用ajax读博客rss示例
2014/05/06 Javascript
JavaScript DOM 对象深入了解
2016/07/20 Javascript
js文件中直接alert()中文出来的是乱码的解决方法
2016/11/01 Javascript
AngularJS指令用法详解
2016/11/02 Javascript
浅谈EasyUI常用控件的禁用方法
2016/11/09 Javascript
老生常谈jacascript DOM节点获取
2017/04/17 Javascript
jquery层次选择器的介绍
2019/01/18 jQuery
Vue-cli3简单使用(图文步骤)
2019/04/30 Javascript
详解vue-cli项目开发/生产环境代理实现跨域请求
2019/07/23 Javascript
多个vue子路由文件自动化合并的方法
2019/09/03 Javascript
在Linux系统上通过uWSGI配置Nginx+Python环境的教程
2015/12/25 Python
Django URL传递参数的方法总结
2016/08/28 Python
itchat和matplotlib的结合使用爬取微信信息的实例
2017/08/25 Python
python实现发送邮件功能代码
2017/12/14 Python
python3爬虫之设计签名小程序
2018/06/19 Python
对pandas数据判断是否为NaN值的方法详解
2018/11/06 Python
对python中Librosa的mfcc步骤详解
2019/01/09 Python
HTML5探秘:用requestAnimationFrame优化Web动画
2018/06/03 HTML / CSS
新年联欢会主持词
2014/03/27 职场文书
工商管理本科生求职信
2014/07/13 职场文书
支部书记四风问题自我剖析材料
2014/09/29 职场文书
寝室长工作失责检讨书
2014/10/06 职场文书
部队2014年终工作总结
2014/11/27 职场文书
保证金退回承诺函格式
2015/01/21 职场文书
八达岭长城导游词
2015/01/30 职场文书
学习焦裕禄观后感
2015/06/09 职场文书