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脚本实现网卡流量监控
Feb 14 Python
Python中max函数用法实例分析
Jul 17 Python
Python入门之三角函数tan()函数实例详解
Nov 08 Python
Python2.7基于笛卡尔积算法实现N个数组的排列组合运算示例
Nov 23 Python
python实现闹钟定时播放音乐功能
Jan 25 Python
浅谈python的深浅拷贝以及fromkeys的用法
Mar 08 Python
python的几种矩阵相乘的公式详解
Jul 10 Python
Python_查看sqlite3表结构,查询语句的示例代码
Jul 17 Python
如何基于Python实现电子邮件的发送
Dec 16 Python
python实现查找所有程序的安装信息
Feb 18 Python
Python定时从Mysql提取数据存入Redis的实现
May 03 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下使用curl模拟用户登陆的代码
2010/09/10 PHP
PHP使用imagick读取PDF生成png缩略图的两种方法
2014/03/20 PHP
PHP管理依赖(dependency)关系工具 Composer 安装与使用
2014/08/18 PHP
一个完整的PHP类包含的七种语法说明
2015/06/04 PHP
PHP QRCODE生成彩色二维码的方法
2016/05/19 PHP
php实现解析xml并生成sql语句的方法
2018/02/03 PHP
laravel框架 laravel-admin上传图片到oss的方法
2019/10/13 PHP
Sample script that deletes a SQL Server database
2007/06/16 Javascript
Egret引擎开发指南之发布项目
2014/09/03 Javascript
浅谈jQuery中的事件
2015/03/23 Javascript
JS+CSS实现简单的二级下拉导航菜单效果
2015/09/21 Javascript
解决jquery无法找到其他父级子集问题的方法
2016/05/10 Javascript
JavaScript中的ajax功能的概念和示例详解
2016/10/17 Javascript
js继承实现方法详解
2016/12/16 Javascript
JS敏感词过滤代码
2016/12/23 Javascript
详解Angular2 关于*ngFor 嵌套循环
2017/05/22 Javascript
2种在vue项目中使用百度地图的简单方法
2018/09/28 Javascript
js canvas画布实现高斯模糊效果
2018/11/27 Javascript
适合前端Vue开发童鞋的跨平台Weex的使用详解
2019/10/16 Javascript
nodejs如何在package.json中设置多条启动命令
2020/03/16 NodeJs
python 获取et和excel的版本号
2009/04/09 Python
python使用mysqldb连接数据库操作方法示例详解
2013/12/03 Python
Python 中开发pattern的string模板(template) 实例详解
2017/04/01 Python
Python数据可视化编程通过Matplotlib创建散点图代码示例
2017/12/09 Python
在Python 中同一个类两个函数间变量的调用方法
2019/01/31 Python
Flask框架学习笔记之模板操作实例详解
2019/08/15 Python
wxPython实现带颜色的进度条
2019/11/19 Python
tensorflow mnist 数据加载实现并画图效果
2020/02/05 Python
Python抓包并解析json爬虫的完整实例代码
2020/11/03 Python
HTML5之消息通知的使用(Web Notification)
2018/10/30 HTML / CSS
美国奢侈品在线团购网站:Gilt City
2017/11/16 全球购物
巴西美妆购物网站:Kutiz Beauté
2019/03/13 全球购物
啤酒节策划方案
2014/05/28 职场文书
2016年大学生实习单位评语
2015/12/01 职场文书
干部外出学习心得体会
2016/01/18 职场文书
vue3语法糖内的defineProps及defineEmits
2022/04/14 Vue.js