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中几种重定向方法
Apr 28 Python
深入理解 Python 中的多线程 新手必看
Nov 20 Python
利用python批量给云主机配置安全组的方法教程
Jun 21 Python
python timestamp和datetime之间转换详解
Dec 11 Python
OpenCV 轮廓检测的实现方法
Jul 03 Python
python基于json文件实现的gearman任务自动重启代码实例
Aug 13 Python
解决python gdal投影坐标系转换的问题
Jan 17 Python
python3操作注册表的方法(Url protocol)
Feb 05 Python
jupyter notebook中美观显示矩阵实例
Apr 17 Python
tensorflow2.0的函数签名与图结构(推荐)
Apr 28 Python
如何在windows下安装Pycham2020软件(方法步骤详解)
May 03 Python
matplotlib.pyplot.plot()参数使用详解
Jul 28 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
ob_start(),ob_start('ob_gzhandler')使用
2006/12/25 PHP
基于php常用正则表达式的整理汇总
2013/06/08 PHP
PHP封装返回Ajax字符串和JSON数组的方法
2017/02/17 PHP
thinkphp实现附件上传功能
2017/05/26 PHP
PHP扩展类型及安装方式解析
2020/04/27 PHP
TNC vs BOOM BO3 第一场2.13
2021/03/10 DOTA
javascript中使用css需要注意的地方小结
2010/09/01 Javascript
jQuery EasyUI API 中文文档 - Spinner微调器使用
2011/10/21 Javascript
javascript中验证大写字母、数字和中文
2014/01/15 Javascript
jquery地址栏链接与a标签链接匹配之特效代码总结
2015/08/24 Javascript
javascript中类的定义方式详解(四种方式)
2015/12/22 Javascript
JS实现复制内容到剪贴板功能兼容所有浏览器(推荐)
2016/06/17 Javascript
详解vue跨组件通信的几种方法
2017/06/15 Javascript
js排序与重组的实例讲解
2017/08/28 Javascript
一个简易时钟效果js实现代码
2020/03/25 Javascript
bing Map 在vue项目中的使用详解
2018/04/09 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
2018/08/25 Javascript
vue首次赋值不触发watch的解决方法
2018/09/11 Javascript
使用Taro实现小程序商城的购物车功能模块的实例代码
2020/06/05 Javascript
Nuxt.js nuxt-link与router-link的区别说明
2020/11/06 Javascript
深入了解Vue3模板编译原理
2020/11/19 Vue.js
[04:02]2014DOTA2国际邀请赛 BBC每日综述中国战队将再度登顶
2014/07/21 DOTA
跟老齐学Python之for循环语句
2014/10/02 Python
python基于Tkinter库实现简单文本编辑器实例
2015/05/05 Python
pygame播放音乐的方法
2015/05/19 Python
python基础教程之Filter使用方法
2017/01/17 Python
python 读取.csv文件数据到数组(矩阵)的实例讲解
2018/06/14 Python
python bmp转换为jpg 并删除原图的方法
2018/10/25 Python
python获取微信小程序手机号并绑定遇到的坑
2018/11/19 Python
django中的数据库迁移的实现
2020/03/16 Python
Jupyter Notebook的连接密码 token查询方式
2020/04/21 Python
用Python 爬取猫眼电影数据分析《无名之辈》
2020/07/24 Python
纯CSS3实现鼠标悬停提示气泡效果
2014/02/28 HTML / CSS
医院办公室主任岗位职责
2015/04/01 职场文书
毕业生学校组织意见
2015/06/04 职场文书
演讲开头怎么书写?
2019/08/06 职场文书