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中关键字is与==的区别简述
Jul 31 Python
跟老齐学Python之字典,你还记得吗?
Sep 20 Python
Python中一些自然语言工具的使用的入门教程
Apr 13 Python
python获取一组数据里最大值max函数用法实例
May 26 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 Python
python 实现网上商城,转账,存取款等功能的信用卡系统
Jul 15 Python
Golang与python线程详解及简单实例
Apr 27 Python
python3实现SMTP发送邮件详细教程
Jun 19 Python
Python 编程速成(推荐)
Apr 15 Python
Python整数与Numpy数据溢出问题解决
Sep 11 Python
Python字符串格式化f-string多种功能实现
May 07 Python
利用Python实现自动扫雷小脚本
Dec 17 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
给初学者的30条PHP最佳实践(荒野无灯)
2011/08/02 PHP
PHP中使用FFMPEG获取视频缩略图和视频总时长实例
2014/05/04 PHP
ioncube_loader_win_5.2.dll的错误解决方法
2015/01/04 PHP
PHP串行化与反串行化实例分析
2016/12/27 PHP
JavaScript实现维吉尼亚(Vigenere)密码算法实例
2013/11/22 Javascript
jQuery中delegate和on的用法与区别详细解析
2014/01/26 Javascript
javascript生成随机颜色示例代码
2014/05/05 Javascript
使用原生js实现页面蒙灰(mask)效果示例代码
2014/06/20 Javascript
JavaScript中提前声明变量或函数例子
2014/11/12 Javascript
Jquery插件实现点击获取验证码后60秒内禁止重新获取
2015/03/13 Javascript
原生js实现class的添加和删除简单代码
2016/07/12 Javascript
AngularJS API之copy深拷贝详解及实例
2016/09/14 Javascript
node内置调试方法总结
2018/02/22 Javascript
你应该了解的JavaScript Array.map()五种用途小结
2018/11/14 Javascript
基于vue框架手写一个notify插件实现通知功能的方法
2019/03/31 Javascript
layui使用templet格式化表格数据的方法
2019/09/16 Javascript
微信小程序canvas分享海报功能
2019/10/31 Javascript
在vue中利用v-html按分号将文本换行的例子
2019/11/14 Javascript
Javascript实现单选框效果
2020/12/09 Javascript
python使用正则表达式提取网页URL的方法
2015/05/26 Python
Python实现将不规范的英文名字首字母大写
2016/11/15 Python
深入理解Python对Json的解析
2017/02/14 Python
python中的常量和变量代码详解
2018/07/25 Python
在Mac中PyCharm配置python Anaconda环境过程图解
2020/03/11 Python
Europcar西班牙:全球汽车租赁领域的领导者
2018/09/17 全球购物
台湾三立电视电商平台:电电购
2019/09/09 全球购物
SQL里面如何插入自动增长序列号字段
2012/03/29 面试题
中药专业毕业自荐书范文
2014/02/08 职场文书
商家认证委托书格式
2014/10/16 职场文书
2015年学习部工作总结范文
2015/03/31 职场文书
穆斯林的葬礼读书笔记
2015/06/26 职场文书
企业廉洁教育心得体会
2016/01/20 职场文书
python实现过滤敏感词
2021/05/08 Python
ORACLE查看当前账号的相关信息
2021/06/18 Oracle
elasticSearch-api的具体操作步骤讲解
2021/06/28 Java/Android
青岛市的收音机研制与生产
2022/04/07 无线电