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中 Lambda表达式全面解析
Nov 28 Python
详解python 模拟豆瓣登录(豆瓣6.0)
Apr 18 Python
Python pandas RFM模型应用实例详解
Nov 20 Python
win10安装tensorflow-gpu1.8.0详细完整步骤
Jan 20 Python
python读取图片的几种方式及图像宽和高的存储顺序
Feb 11 Python
基于Pytorch SSD模型分析
Feb 18 Python
Python多线程操作之互斥锁、递归锁、信号量、事件实例详解
Mar 24 Python
pandas中read_csv、rolling、expanding用法详解
Apr 21 Python
Django与pyecharts结合的实例代码
May 13 Python
利用Python实现Json序列化库的方法步骤
Sep 09 Python
python 无损批量压缩图片(支持保留图片信息)的示例
Sep 22 Python
详解BeautifulSoup获取特定标签下内容的方法
Dec 07 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
浅析memcache启动以及telnet命令详解
2013/06/28 PHP
php引用传值实例详解学习
2013/11/06 PHP
优化WordPress的Google字体以加速国内服务器上的运行
2015/11/24 PHP
php面向对象的用户登录身份验证
2017/06/08 PHP
Yii中特殊行为ActionFilter的使用方法示例
2020/10/18 PHP
jquery实现简单易懂的图片展示小例子
2013/11/21 Javascript
将HTML的左右尖括号等转义成实体形式的两种实现方式
2014/05/04 Javascript
jQuery焦点控制图层展示延迟隐藏的方法
2015/03/09 Javascript
js通过iframe加载外部网页的实现代码
2015/04/05 Javascript
javascript图片切换综合实例(循环切换、顺序切换)
2016/01/13 Javascript
学习JavaScript事件流和事件处理程序
2016/01/25 Javascript
JS中多种方式创建对象详解
2016/03/22 Javascript
小程序开发实战:实现九宫格界面的导航的代码实现
2017/01/19 Javascript
webpack打包单页面如何引用的js
2017/06/07 Javascript
VueJS 集成 Medium Editor的示例代码 (自定义编辑器按钮)
2017/08/24 Javascript
Nuxt.js SSR与权限验证的实现
2018/11/21 Javascript
swiper4实现移动端导航切换
2020/10/16 Javascript
详解Nuxt内导航栏的两种实现方式
2020/04/16 Javascript
vue 子组件和父组件传值的示例
2020/09/11 Javascript
JS时间戳与日期格式互相转换的简单方法示例
2021/01/30 Javascript
Vue仿Bibibili首页的问题
2021/01/21 Vue.js
python单例模式实例分析
2015/04/08 Python
Python金融数据可视化汇总
2017/11/17 Python
使用python爬虫实现网络股票信息爬取的demo
2018/01/05 Python
matplotlib 纵坐标轴显示数据值的实例
2018/05/25 Python
python根据list重命名文件夹里的所有文件实例
2018/10/25 Python
Python字典对象实现原理详解
2019/07/01 Python
3种适用于Python的疯狂秘密武器及原因解析
2020/04/29 Python
HTML5之SVG 2D入门8—文档结构及相关元素总结
2013/01/30 HTML / CSS
Tostadora意大利:定制T恤
2019/04/08 全球购物
Hammitt官网:设计师手袋
2020/05/23 全球购物
业务员岗位职责
2013/11/16 职场文书
物理课外活动总结
2014/08/27 职场文书
离婚协议书范文2015
2015/01/26 职场文书
法律意见书范文
2015/05/20 职场文书
车间安全生产管理制度
2015/08/06 职场文书