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 相关文章推荐
Django自定义分页与bootstrap分页结合
Feb 22 Python
Python解决N阶台阶走法问题的方法分析
Dec 28 Python
Python进阶之尾递归的用法实例
Jan 31 Python
Win8下python3.5.1安装教程
Jul 29 Python
python在TXT文件中按照某一字符串取出该字符串所在的行方法
Dec 10 Python
python使用knn实现特征向量分类
Dec 26 Python
使用Django连接Mysql数据库步骤
Jan 15 Python
Python实现直方图均衡基本原理解析
Aug 08 Python
python实现广度优先搜索过程解析
Oct 19 Python
python中with语句结合上下文管理器操作详解
Dec 19 Python
Pandas时间序列基础详解(转换,索引,切片)
Feb 26 Python
Python基于Dlib的人脸识别系统的实现
Feb 26 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基础知识:控制结构
2006/12/13 PHP
php下几个常用的去空、分组、调试数组函数
2009/02/22 PHP
PHP性能优化准备篇图解PEAR安装
2011/12/05 PHP
PHP中require和include路径问题详解
2014/12/25 PHP
php抽象类用法实例分析
2015/07/07 PHP
PHP 中TP5 Request 请求对象的实例详解
2017/07/31 PHP
基于PHP的微信公众号的开发流程详解
2020/08/07 PHP
使用prototype.js进行异步操作
2007/02/07 Javascript
JQuery 学习笔记 选择器之四
2009/07/23 Javascript
Jquery Validation插件防止重复提交表单的解决方法
2010/03/05 Javascript
js数组去重的常用方法总结
2014/01/24 Javascript
jquery自动填充勾选框即把勾选框打上true
2014/03/24 Javascript
Web表单提交之disabled问题js解决方法
2015/01/13 Javascript
关于vue.js过渡css类名的理解(推荐)
2017/04/10 Javascript
解决iview打包时UglifyJs报错的问题
2018/03/07 Javascript
vue通过数据过滤实现表格合并
2020/11/30 Javascript
Vue中的transition封装组件的实现方法
2019/08/13 Javascript
js设计模式之代理模式及订阅发布模式实例详解
2019/08/15 Javascript
javascript实现移动端触屏拖拽功能
2020/07/29 Javascript
js+css实现扇形导航效果
2020/08/18 Javascript
angular8.5集成TinyMce5的使用和详细配置(推荐)
2020/11/16 Javascript
Python挑选文件夹里宽大于300图片的方法
2015/03/05 Python
python实现的守护进程(Daemon)用法实例
2015/06/02 Python
Python中列表和元组的使用方法和区别详解
2020/12/30 Python
python读取excel表格生成erlang数据
2017/08/26 Python
如何用Python制作微信好友个性签名词云图
2019/06/28 Python
用Python画一个LinkinPark的logo代码实例
2019/09/10 Python
Laravel框架表单验证格式化输出的方法
2019/09/25 Python
Flask处理Web表单的实现方法
2021/01/31 Python
Champion官网:美国冠军运动服装
2017/01/25 全球购物
Jacques Lemans德国:奥地利钟表品牌
2019/12/26 全球购物
元旦晚会邀请函
2014/02/01 职场文书
数控技校生自我鉴定
2014/04/19 职场文书
学生考试舞弊检讨书
2015/01/01 职场文书
2015年勤工助学工作总结
2015/04/29 职场文书
python3 hdf5文件 遍历代码
2021/05/19 Python