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 安装virtualenv和virtualenvwrapper的方法
Jan 13 Python
python3.6 +tkinter GUI编程 实现界面化的文本处理工具(推荐)
Dec 20 Python
django 在原有表格添加或删除字段的实例
May 27 Python
Pandas 同元素多列去重的实例
Jul 03 Python
Python实现的读取/更改/写入xml文件操作示例
Aug 30 Python
python 文本单词提取和词频统计的实例
Dec 22 Python
Python3 venv搭建轻量级虚拟环境的步骤(图文)
Aug 09 Python
Django单元测试中Fixtures用法详解
Feb 25 Python
通过案例解析python鸭子类型相关原理
Oct 10 Python
用python读取xlsx文件
Dec 17 Python
【超详细】八大排序算法的各项比较以及各自特点
Mar 31 Python
python 安全地删除列表元素的方法
Mar 16 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中循环语句的用法介绍
2012/01/30 PHP
PHP利用超级全局变量$_POST来接收表单数据的实例
2016/11/05 PHP
php JWT在web端中的使用方法教程
2018/09/06 PHP
详解php中curl返回false的解决办法
2019/03/18 PHP
javascript 定义新对象方法
2010/02/20 Javascript
jQuery中$.ajax()和$.getJson()同步处理详解
2015/08/12 Javascript
Jquery实现仿京东商城省市联动菜单
2015/11/19 Javascript
JQuery插件Marquee.js实现无缝滚动效果
2016/04/26 Javascript
jquery输入数字随机抽奖特效的简单实现代码
2016/06/10 Javascript
Bootstrap轮播插件使用代码
2016/10/11 Javascript
JavaScript实现弹出广告功能
2017/03/30 Javascript
JavaScript+Html5实现按钮复制文字到剪切板功能(手机网页兼容)
2017/03/30 Javascript
解决vue 中 echart 在子组件中只显示一次的问题
2018/08/07 Javascript
JSON的parse()方法介绍
2019/01/31 Javascript
小程序实现上传视频功能
2020/08/18 Javascript
[57:47]Fnatic vs Winstrike 2018国际邀请赛小组赛BO2 第二场 8.18
2018/08/19 DOTA
python使用reportlab画图示例(含中文汉字)
2013/12/03 Python
用Python实现斐波那契(Fibonacci)函数
2016/03/25 Python
Python内置模块turtle绘图详解
2017/12/09 Python
python实现word 2007文档转换为pdf文件
2018/03/15 Python
单利模式及python实现方式详解
2018/03/20 Python
django用户登录和注销的实现方法
2018/07/16 Python
pandas 透视表中文字段排序方法
2018/11/16 Python
Python设计模式之装饰模式实例详解
2019/01/21 Python
python3 实现爬取TOP500的音乐信息并存储到mongoDB数据库中
2019/08/24 Python
python 监测内存和cpu的使用率实例
2019/11/28 Python
CSS3 text shadow字体阴影效果
2016/01/08 HTML / CSS
印尼在线旅游门户网站:NusaTrip
2019/11/01 全球购物
MYSQL支持事务吗
2013/08/09 面试题
迟到检讨书900字
2014/01/14 职场文书
《伯牙绝弦》教学反思
2014/03/02 职场文书
放飞梦想演讲稿
2014/05/05 职场文书
机票销售员态度不好检讨书
2014/09/27 职场文书
期中考试后的感想
2015/08/07 职场文书
终止合同协议书范本
2016/03/22 职场文书
穷人该怎么创业?谨记以下几点
2019/07/11 职场文书