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类定义和类继承详解
May 08 Python
python插入排序算法实例分析
Jul 03 Python
Python  unittest单元测试框架的使用
Sep 08 Python
python文件转为exe文件的方法及用法详解
Jul 08 Python
Django的Modelforms用法简介
Jul 27 Python
python Django编写接口并用Jmeter测试的方法
Jul 31 Python
python基于K-means聚类算法的图像分割
Oct 30 Python
python从内存地址上加载python对象过程详解
Jan 08 Python
Python HTMLTestRunner测试报告view按钮失效解决方案
May 25 Python
QML用PathView实现轮播图
Jun 03 Python
Python调用.net动态库实现过程解析
Jun 05 Python
使用OpenCV去除面积较小的连通域
Jul 05 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
JAVA/JSP学习系列之六
2006/10/09 PHP
PHP中删除变量时unset()和null的区别分析
2011/01/27 PHP
php中JSON的使用与转换
2015/01/14 PHP
php解决DOM乱码的方法示例代码
2016/11/20 PHP
php封装的mongodb操作类代码
2017/08/06 PHP
Laravel Validator自定义错误返回提示消息并在前端展示
2019/05/09 PHP
jquery 问答知识整理
2010/02/11 Javascript
javascript cookies 设置、读取、删除实例代码
2010/04/12 Javascript
CSS和Javascript简单复习资料
2010/06/29 Javascript
鼠标拖拽移动子窗体的JS实现
2014/02/25 Javascript
js获取select默认选中的Option并不是当前选中值
2014/05/07 Javascript
Js可拖拽放大的层拖动特效实现方法
2015/02/25 Javascript
EasyUI实现第二层弹出框的方法
2015/03/01 Javascript
Jquery实现仿腾讯娱乐频道焦点图(幻灯片)特效
2015/03/06 Javascript
浅谈js基础数据类型和引用类型,深浅拷贝问题,以及内存分配问题
2017/09/02 Javascript
Node.js中你不可不精的Stream(流)
2018/06/08 Javascript
详解angular应用容器化部署
2018/08/14 Javascript
JavaScript布尔运算符原理使用解析
2020/05/06 Javascript
vue递归获取父元素的元素实例
2020/08/07 Javascript
[56:18]VGJ.S vs Secret 2018国际邀请赛小组赛BO2 第二场 8.16
2018/08/17 DOTA
python正常时间和unix时间戳相互转换的方法
2015/04/23 Python
Python实现代码统计工具(终极篇)
2016/07/04 Python
Python实现的各种常见分布算法示例
2018/12/13 Python
Python代理IP爬虫的新手使用教程
2019/09/05 Python
python3 常见解密加密算法实例分析【base64、MD5等】
2019/12/19 Python
pyinstaller打包单文件时--uac-admin选项不起作用怎么办
2020/04/15 Python
Python 程序报错崩溃后如何倒回到崩溃的位置(推荐)
2020/06/23 Python
python 动态渲染 mysql 配置文件的示例
2020/11/20 Python
以下的初始化有什么区别
2013/12/16 面试题
函授本科毕业自我鉴定
2013/10/09 职场文书
社区活动策划方案
2014/08/21 职场文书
委托书的写法
2014/09/16 职场文书
煤矿安全保证书
2015/02/27 职场文书
品质保证书格式
2015/02/28 职场文书
项目战略合作意向书
2015/05/08 职场文书
我爱我班主题班会
2015/08/13 职场文书